2017-02-16 112 views
1

我是iOS开发新手。根据UILabel需要显示的选项,我列出了UITextFields的下拉菜单。以快速编程方式隐藏TextFields?

UITextField 
DropdownMenu 
UITextField 
UITextField 

像上面提到的它显示的启动应用程序。更改下拉菜单后需要显示标签。

UITextField 
DropdownMenu 
UILabel 
UITextField 
UITextField 

我知道如何使用label.hidden = true隐藏UILabel。但隐藏之后,UILabel仍占据着空间。之后,只有它显示出两个UITextField。如果有任何方式在隐藏标签后动态改变位置。

我已经编程用于创建文本字段和标签:

let textField1 = UITextField(frame: CGRect(x: 20, y: 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height:45)) 
let textField2 = UITextField(frame: CGRect(x: 20, y: textField1.frame.origin.y + textField1.frame.size.height + 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height: 45)) 
let button = UIButton(frame: CGRect(x: 20, y: textField2.frame.origin.y + textField2.frame.size.height + 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height: 45)) 
let label1 = UILabel(frame: CGRect(x: 20, y: button.frame.origin.y + button.frame.size.height + 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height: 45)) 
let textField3 = UITextField(frame: CGRect(x: 20, y: label1.frame.origin.y + label1.frame.size.height + 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height: 45)) 
let textField4 = UITextField(frame: CGRect(x: 20, y: textField3.frame.origin.y + textField3.frame.size.height + 10, width: UIApplication.shared.statusBarFrame.size.width - 40, height: 45)) 
+0

您使用自动布局或自动调整 –

+0

使用自动布局多个动态只有@ Anbu.Karthik – vara

+0

使用自动布局或编程在故事板?你可以在这里粘贴你的代码吗? –

回答

0

@NeverHopeless是正确的,UIStackView是完美的解决方案,这里的 如何以编程方式使用它:

override func viewDidAppear(_ animated: Bool) { 

    let subviewArray = [TextFields1, TextFields2, Button, Label1, TextFields3, TextFields4] 
    let stackView = UIStackView(arrangedSubviews: subviewArray) 
    stackView.axis = .Vertical // It can also align horizontally 
    stackView.distribution = .FillEqually // You can try other options 
    stackView.alignment = .Fill // You can try other options 
    stackView.spacing = 5 // Bigger spacing means larger distance between subviews 
    stackView.translatesAutoresizingMaskIntoConstraints = false 
    view.addSubview(stackView) 

} 

关于添加的更多信息以编程方式,你可以在这里阅读:https://makeapppie.com/2015/11/11/how-to-add-stack-views-programmatically-and-almost-avoid-autolayout/

0

StackView是一个很好的方式来解决你的问题。

但是,如果你不想更改你的代码太多。您可以更改标签= 0

label1.frame.size = CGSize(width: 0, height: 0) 

的高度,但我还是建议使用stackView,因为它是