如何更改 Uipickerview 的位置? [英] How can i change Uipickerview position?

查看:39
本文介绍了如何更改 Uipickerview 的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是故事板

我创建了一个这样的选择器视图;

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 ?

推荐答案

如果您以编程方式添加内容(调用 addSubView:),您必须以编程方式进行自动布局,以便以正确的顺序定位您的视图.

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

有很多关于这个的教程.我只是给你一些例子.假设你想把你的 PickerView

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

我正在使用锚定.因为代码大大减少了.感谢 Apple.

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

当然要在添加subView之后再添加这个.顺便从您的代码中删除此行.您可以使用自动布局居中对齐.:)

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

这篇关于如何更改 Uipickerview 的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆