How can i change Uipickerview position?

戏子无情 提交于 2019-12-11 00:25:48

问题


This is the storyboard

I create a picker view like that ;

    pickerView = UIPickerView()
    pickerView.center = view.center
    view.addSubview(pickerView)
    pickerView.dataSource = self
    pickerView.delegate = self

When i run the app picker view show up like that .

But i want the change position pickerview.How can i do that ?


回答1:


If you add something programmatically(calling addSubView:) you have to do autolayout programmatically in order to position your view in correct order.

There are lots of tutorials about this.I am just giving you some example about it.Suppose if you want to put your PickerView

Left position : 20px

Top position : 100px

You have to first write below line.Why? just google it. :)

pickerView.translatesAutoresizingMaskIntoConstraints = false

I am using anchoring.Because code is very much reduced.Thanks to Apple.

 pickerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 20.0).isActive = true
 pickerView.topAnchor.constraint(equalTo:self.view.topAnchor, constant: 100.0).isActive = true

Of course you must add this after you add subView. remove this line from your code by the way.You can center align using autolayout. :)

pickerView.center = view.center



回答2:


You have to set layout constraint to change the position of pickerview as per your requirements.

if you set the below line it will show center of the view

pickerView.center = view.center

you have to set the constraint value in respect of superview or as your required position.



来源:https://stackoverflow.com/questions/44069112/how-can-i-change-uipickerview-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!