1

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))
Cœur
  • 37,241
  • 25
  • 195
  • 267
vara
  • 816
  • 3
  • 12
  • 29

3 Answers3

0

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
  • 11,077
  • 4
  • 35
  • 56
0

@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/

Bright
  • 5,699
  • 2
  • 50
  • 72
0

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