2017-02-16 127 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,因爲它是

相關問題