简体   繁体   中英

hide TextFields Programmatically in Swift?

I have list of UITextFields with dropdown menu based on the option after UILabel need to show.

UITextField
DropdownMenu
UITextField
UITextField

Like above mentioned its showing starting application. after changing the dropdown menu need to show label.

UITextField
DropdownMenu
UILabel
UITextField
UITextField

I know how to hide the UILabel by using label.hidden = true . But after hiding, UILabel still occupies the space. After that only its showing that two UITextField . If any way to dynamically change position after hide the label.

I have used programmatically for creating the TextFields and Labels:

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))

From your requirements it looks like you are in need of using UIStackView control to fix this problem:

so after hide but that place has empty

UIStackView adjust itself if one or more of the child controls adds/removes, depending on your parameters you set to UIStackView. Have a look at these links on how to use it:

iOS 9: Getting Started with UIStackView

UIStackView Tutorial: Introducing Stack Views

@NeverHopeless is right, UIStackView is the perfect solution, here's how you can use it programmatically:

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)

}

For more about adding UIStackView programmatically , you can read about here: https://makeapppie.com/2015/11/11/how-to-add-stack-views-programmatically-and-almost-avoid-autolayout/

StackView is a very good way to solve your problem.

But if you don't want to change your code much. You can change the height of your label = 0

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

But I still recommend to use stackView because it's more dynamical

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM