【问题标题】:hide TextFields Programmatically in Swift?在 Swift 中以编程方式隐藏 TextFields?
【发布时间】:2017-02-16 05:51:54
【问题描述】:

我有UITextFields 列表,其中包含基于UILabel 之后的选项需要显示的下拉菜单。

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

【问题讨论】:

  • 您使用的是自动布局还是自动调整大小
  • 仅使用自动布局@Anbu.Karthik
  • 以编程方式或在情节提要中使用自动布局?你能把你的代码贴在这里吗?
  • 使用UIStackView
  • @vara 我猜你需要这个stackoverflow.com/questions/31904573/…

标签: ios swift user-interface


【解决方案1】:

根据您的要求,您似乎需要使用UIStackView 控件来解决此问题:

所以隐藏之后,但那个地方是空的

如果添加/删除一个或多个子控件,UIStackView 会自行调整,具体取决于您设置为 UIStackView 的参数。请查看以下链接了解如何使用它:

iOS 9: Getting Started with UIStackView

UIStackView Tutorial: Introducing Stack Views

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      StackView 是解决问题的好方法。

      但如果您不想过多更改代码。您可以更改标签的高度 = 0

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

      但我仍然建议使用 stackView,因为它更具动态性

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-03
        • 1970-01-01
        • 1970-01-01
        • 2017-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多