UIPickerView Width in iOS8 Swift

I just can't find out how to get the width of a component in my UIPickerView. This does not allow something simple (Int), since it must be CGFloat. What exactly do I put in the <# code #> section?

func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { <#code#> } 

Thanks!

+2
width ios8 swift uipickerview
source share
2 answers

This method. doesn't "get the width of the component ...". The selector calls this method to ask you how wide the component should be.

Your method may be as simple as:

 func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return CGFloat(25.0) } 

(or any width is suitable for your kind of choice)

You just need to pass the result to an extended type.

+3
source share

The best way to return the width of a component is to use bounds.width:

 func pickerView(pickerView: UIPickerView, widthForComponent component: Int) -> CGFloat { return self.view.bounds.width / x } 

Where x will consist of several components that you want to use in PickerView.

+3
source share

All Articles