ios - 程序化自动布局 UI 元素

标签 ios uiview swift3 autolayout programmatically-created

我正在尝试按照图像中显示的顺序自动布局 3 个 UI 元素。 UIView 中的 UITextfield、UIDatePicker 和 UIButton。

enter image description here

我避免使用 Storyboard ,因为我想更好地理解编程约束并最终为它们使用动画。

到目前为止,我已经尝试过一些限制:

enter image description here

这是我正在开发的代码:

override func viewDidLoad() {
    super.viewDidLoad()


    picker.translatesAutoresizingMaskIntoConstraints = false
    picker.backgroundColor = UIColor.red


    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = UIColor.blue
    button.setTitle("Button", for: .normal)

    self.view.addSubview(picker)
    self.view.addSubview(button)

    let PickercenterX = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)

    let PickercenterBottom = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.button, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -30)

    let Pickerheight = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 150)

    let Pickerwidth = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -5)


    // Centers it on the x axis. Pushes it it right if co constant has a value > 0
    let centerX = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.centerX, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.centerX, multiplier: 1, constant: 0)


    let centerBottom = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: -15)

    let height = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 50)

    let width = NSLayoutConstraint(item: self.button, attribute: NSLayoutAttribute.width, relatedBy: .equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: -15)


    self.view.addConstraints([centerX, centerBottom, height, width, PickercenterX, PickercenterBottom, Pickerheight, Pickerwidth])

}

我正在尝试先使用按钮和日期选择器,然后再移动到文本字段。我怎样才能以编程方式实现这一目标?

最佳答案

问题就在这里:- 你将选择器底部设置为等于按钮底部,常数为 -30,虽然我知道你想做什么,但你试图在选择器和按钮之间留出垂直空间。所以它应该像这样链接,选择器的底部等于按钮的顶部,常数为 -30。

此外,您还没有在末尾添加 isActive 来激活约束。

另一种同时激活所有约束的方法是使用 NSLayoutConstraint.activate() 方法

let PickercenterBottom = NSLayoutConstraint(item: self.picker, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.button, attribute: NSLayoutAttribute.top, multiplier: 1, constant: -30).isActive = true

关于ios - 程序化自动布局 UI 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44864999/

相关文章:

ios - UIView 重复 animateWithDuration CPU 100%

ios - 如何在 swift 3 中使用 NSPredicate 过滤数组

ios - 无法在 UILabel 中显示我的文本

ios - UITextView 中的控制选择

ios - 将一对一外键连接添加到 RestKit 映射时出现问题

ios - iOS 11 AVCaptureMetadataOutput 黑色条码上的反转/反转白色

ios - 如何显示下面的UIView?

ios - 从自定义 UIView 呈现 View Controller (xib 文件)

ios - 如何在后台下载后将属性传递给 swift 3 urlsession didFinishDownloadingTo 委托(delegate)?

ios - SpriteKit Sprite 未正确缩放