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
Related
I have a UIPickerView with 2 components:
The hidden rows however are shifted towards the center. I think it has to do with the space that UIPickerView places between the 'wheels'.
I don't see anything in iOS' rather brief documentation discussing how to prevent this.
Here's my code:
func pickerView(_ pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat
{
return self.currenciesPicker.width / 2
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
{
return 50.0
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView
{
var cell: CurrencyPickerCell
if view == nil
{
let width = self.pickerView(pickerView, widthForComponent: component)
let height = self.pickerView(pickerView, rowHeightForComponent: component)
cell = CurrencyPickerCell(frame: CGRect(x: 0.0, y: 0.0, width: width, height: height))
}
else
{
cell = view as! CurrencyPickerCell
}
let code = self.codes[row]
cell.nameLabel.text = Currency.names[code]
cell.symbolLabel.text = Currency.symbols[code]
return cell
}
When I let widthForComponent return half the picker's size minus 20, the cells just show up 20 point smaller. This creates empty borders on the left and right ends of the picker view. It does not change this weird 'parallax'.
Does anyone have an idea what's causing this and how to prevent it?
How should I calculate widthForComponent (is half the width of the picker correct)?
Unfortunately this is how the UIPickerView control is designed. It's supposed to look like a couple of wheels sitting side by side that you are looking at from directly above. It's a bit of a strange one considering all the push towards a flat design style Apple has made but there is no way to change it.
The only real options are to do it yourself, either completely manually or possibly subclassing UIPickerView and handling the drawing yourself, or to use a 3rd party control.
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?
I'm using CoreData and I'm trying to have 2 different columns in my UIPickerView with Stores and the Item Type
When I run it, the items are "there" but they appear as a "?"
here is my code for my titleForRow function:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> (String?, String?) {
//title
let store = stores[row]
let itemType = itemTypes[row]
// select store out of array of stores
return (store.name, itemType.type)
}
something to note is Xcode is giving me a warning saying:
Instance method 'pickerVIew(titleForRow:forComponent:)' nearly matches
optional requirement 'pickerView(titleForRow:forComponent:)' of
protocol UIPickerView
You can return only String type, not tuple. If you want to add two columns, you need to set
pickerView.numberOfComponents = 2
and check component parameter like:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return component == 0 ? stores[row].name : itemTypes[row].type
}
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!
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).