Search code examples
iosuitableviewuipicker

Arrange picker to be in center of cell


I have an UITableView to which has a cell to which I add a custom picker ([[cell contentView] addSubview:picker]) with a custom size CGRectMake(0,0,320,180). On an iphone screen everything looks fine but on an ipad the picker is on the left side of the screen. How do I arrange the picker/subview in the cell so its in the center and not on the left side?


Solution

  • Your UIPickerView will center in its own size. So if you manage to make it resizing with the cell, it should be good.

    Problem here is that you define a fixed width for your picker view. If you want it to work , you need to set the good width in each situation.

    You can manage to do it by :

    1. looking about the device (so you set the width of the screen) ; It's the easy but dirty solution

    2. set a resizing mask to your UIPickerView so it resize with the cell ; It's a hard solution, but correct

    3. use AutoLayout so your UIPickerView will be constraint to the cell width ; It's the nice solution, but restricted to iOS6 and more