That is not possible. If you need differently styled "UIPickerView" you will to roll your own.
Decrease the margin of content of UIPickerView ios
-
28-11-2021 - |
Question
Modification due to the first 2 answers : what I want to modify is not the height of the UIPickerview. What I want is to make the content of UIPicker begins from the upper side of UIPicker. Here is an example:
I want to delete the margin shown in the image here attached.
Any idea?
With a toolbar, the UIPickerView is embedded in a UIView. So the view hierarchy is : View(parent)->Toolbar(child), UIPickerView(child)
The View is declared as customPicker in my viewController. Here is the code: In viewController.h :
@interface myViewController:UIViewController<UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBoutlet UIView *customPicker;
@end
In viewController.m :
- (void) viewDidLoad{
self.customPicker.frame = CGRectMake(0, CGRectGetMaxY(self.view.frame), CGRectGetWidth(self.customPicker.frame), CGRectGetHeight(self.customPicker.frame));
[self.view addSubview:self.customPicker];
}
Then I use a setPickerHidden method to animate the View in order to show or hide it.
Thanks in advance for your help!
Solution
OTHER TIPS
You can't change the UIPickerView's height. In fact, you can only modify the picker's width. More information here.
You can make the picker start at a specific row (e.g. row 2 or 3) so that the margin does not initially show. However the user could still scroll it down and when it reaches the bounds of the picker contents it would still look like your example above.
Alternatively you could create the effect of an infinite picker view (although in reality it is actually just a picker view with a lot of rows).
See here: How do you make an UIPickerView component wrap around?
Answered at 2021
For now we can use UIPickerViewDelegate to create a picker with no margin.
Let picker fill the container(blue border), then implement rowHeight delegate method and return a value close to container height. Full code goes here:
class PickerViewWrapper: UIView, UIPickerViewDataSource, UIPickerViewDelegate {
// Make the return value of this delegate method object close to view height
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 60
}
let customPickerView = UIPickerView()
let labelTexts = ["Day","Week","Month","Year"]
init() {
super.init(frame: .zero)
customPickerView.dataSource = self
customPickerView.delegate = self
self.addSubview(customPickerView)
// Let picker fill container view.
customPickerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
customPickerView.leadingAnchor.constraint(equalTo: self.leadingAnchor),
customPickerView.trailingAnchor.constraint(equalTo: self.trailingAnchor),
customPickerView.topAnchor.constraint(equalTo: self.topAnchor),
customPickerView.bottomAnchor.constraint(equalTo: self.bottomAnchor)
])
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return labelTexts.count
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
let pickerLabel = UILabel()
pickerLabel.text = labelTexts[row]
pickerLabel.sizeToFit()
return pickerLabel
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}