I just set up a UIPickerView with different images in each row using
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let rowImg = UIImage(named: array[row] + ".png")
let rowView = UIView(frame: CGRect(x:0,y:0,width:60,height:60))
rowView.backgroundColor = UIColor(patternImage: rowImg!)
return rowView
}
I noticed that the image was softened more than I like and the color went from a sharp red to almost a soft red-orange. Anyway around this?
Related
I tried to put a UIPickerView in a UIViewController and show a simple items in it with this code:
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 3
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 10
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.textColor = UIColor.black
pickerLabel.text = "10"
pickerLabel.textAlignment = NSTextAlignment.center
pickerLabel.sizeToFit()
return pickerLabel
}
everything is fine but when I changed numberOfComponents to 2 picker view didn't show correct. Why this is happening?(Xcode 8 iOS 10)
There are two things to notice here:
You have used: pickerLabel.sizeToFit() which would size the label to its content.
Next is that if you comment pickerLabel.sizeToFit() and put a background color to your label. You'd get the following result
As you can see in here that PickerView has a curvature and a distance in between cells which is causing this issue. So solution is you can give a width to your label & no size to fit and apply a background while testing your result and come to the best possible combination.
For instance, check the iOS Native clock timer:
"They have tried playing around with width of label and index"
Please note:
The curvature is causing the width to decrease and label is center aligned so it is taking it along the path of decreasing width, if you get what i'm trying to say here.
This should explain it.
Cheers!
I'm trying to get the first row data for pickerView.
I'm getting first data when I'm back the pickerView page, but I'm getting the wrong row on the screen. This is how I'm getting the first row :
pickerView(myPickerView!, didSelectRow: 0, inComponent: 0)
I'm using this code in viewDidLoad. How can I show the first row on the screen?
u may use the func pickerView(pickerView:
UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
have a look at this article
please use this code to select a particular row,
let picker = UIPickerView()
picker.selectRow(0, inComponent: 0, animated: true)
I give up to get first picker row data when I come back the page. I think it should be the selected row data I should get. I combined your solutions and find the solve.
Working code :
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
let titleData = newuser!.valueForKey("name").objectAtIndex(row) as! String
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Times New Roman", size: 25.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
pickerLabel.attributedText = myTitle
pickerLabel.textAlignment = .Center
selectedLog = (newuser?.valueForKey("name").objectAtIndex(row))! as! String
return pickerLabel
}
This viewForRow function provides me to get selected row data when I come back the pickerView page. selectedLog is my string variable. I show and save the selected row data from here. titleData variable provides me to get selected data. That's it! Thanks all of you.
Attempting to right/left align the text in two components in a UIPickerView. It's being displayed correctly inside of the selected zone, but the text is overlapping elsewhere.
Any idea what I'm doing wrong here?
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView?) -> UIView {
let pickerLabel = UILabel()
var titleData: String!
if component == 0 {
if pickerView.tag == 0 {
titleData = weightData[row].description
} else {
titleData = bodyFatData[row].description
}
pickerLabel.textAlignment = .Right
} else {
titleData = decimalData[row].description
pickerLabel.textAlignment = .Left
}
let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(22.0),NSForegroundColorAttributeName:
UIColor.blackColor()])
pickerLabel.attributedText = myTitle
return pickerLabel
}
EDIT:
When I add this line
pickerLabel.frame.size = CGSizeMake(165, 26)
It does have an effect, but the selected/unselected rows are still misaligned.
I think you are simply overlooking the possibilities of this function:
// Swift
func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat {
return CGFloat(50.0)
}
By default the components will center align, and it appears the whole picker will also center align. If you omit the textAlignment, all the values in a component (column) will line up.
I am implementing a custom picker view only with images. The problem is, the images are overlayed over each other like that:
This is the code configuring the images in the picker view:
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView {
let chosenImage: UIImageView = UIImageView(image: UIImage(named: iconArray[row]))
NSLog("choosen image \(iconArray[row])")
let workaroundImageView: UIImageView = UIImageView(frame: chosenImage.frame)
workaroundImageView.backgroundColor = UIColor(patternImage: chosenImage.image!)
return workaroundImageView
}
What am I missing? Any suggestions?
Utilize the rowHeightForComponent in the delegate for the UIPickerView.
pickerView:rowHeightForComponent:
Give the desired row height for component as such.
func pickerView(pickerView: UIPickerView!, rowHeightForComponent component: Int) -> CGFloat
So I have UIPickerView that isn't working the way I want it to. It works correctly about 3/4 of the time, but the other 1/4 it doesn't.
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return listsArray.count
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
activeQuizPlace = row
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.whiteColor()
pickerLabel.text = listsArray[row]
pickerLabel.font = UIFont(name: "Georgia", size: 22)
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
The listsArray contains 5 items(which are strings). Rows 0-2 work correctly 100% of the time. However when row 3 is selected it occasionally says that row 1 was, and when row 4 is selected it sometimes says that row 2 was. I think the problem has something to do with the last function I have that controls the UIPickerView. I just copied that code from this website as a way to change the color and font of the text. Before I was using this function I never had a problem with it, or at least never noticed one. Side note: I am using Xcode 6.3.2
I need a way for the picker view to work correctly, as well as to be able to change its font and color. I would love help with this I've been looking for answers online for 2 hours and can't figure out what I'm doing wrong, thanks in advance!
I think you need to remove the line activeQuizPlace = row from the code you have shown and then add this function below.
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
activeQuizPlace = row
}
The function you were using is not called with the index that the user selects, it is called with the index that the system needs for drawing the views.
The suggested function should be the one you are after (hopefully).