【问题标题】:NSLayoutConstraints on objects swift 3.0对象上的 NSLayoutConstraints swift 3.0
【发布时间】:2017-04-15 18:03:17
【问题描述】:

目前我正在使用这行代码在左上角的 UILabel 上设置原点...

addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

但是,如果有意义的话,我希望原点距离左边缘 H 0 像素和高度的一半。

有什么建议吗?我试过更改乘数无济于事。

我要做的是移动原点,使 (0,0) 不是左上角,而是标签的最左侧和标签高度的一半,以便我可以正确居中标签。

【问题讨论】:

  • 我建议你修改你的代码,这样人们就可以阅读它而无需荒谬的滚动。我拒绝按原样阅读。

标签: ios swift xcode nslayoutconstraint


【解决方案1】:

澄清一下,您需要将标签左对齐并从上到下居中吗?

你看过NSLayoutAnchor吗?

messageLabel = UILabel(/* Initialized somehow */)
messageLabel.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(messageLabel)
view.centerYAnchor.constraint(equalTo: messageLabel.centerYAnchor).isActive = true
view.leadingAnchor.constraint(equalTo: messageLabel.leadingAnchor).isActive = true

【讨论】:

    【解决方案2】:

    尝试设置标签的来源:

    messageLabel.horizontalAlignmentMode = .Left
    messageLabel.position = CGPoint(x:0.0, y:self.size.height)
    

    不需要约束!!

    希望能帮到你:)

    【讨论】:

    • 没有需要约束如果您的应用程序被旋转锁定。这意味着您的应用只有一个 方向设置,横向或纵向。如果您想在您的应用程序中允许方向更改,您确实需要使用自动布局。唯一的其他选择是监听来自系统的方向变化并手动调整你的 UI 元素的位置。这非常乏味,在转换过程中不如自动布局优雅,而且在当今 iOS 开发时代,不使用自动布局是一个重大错误。
    • 由于不需要乘数或常数,因此不需要约束。 stefan 只是想将按钮设置为顶部让角落就可以了
    • 如果没有适当的约束,标签框架在方向改变时将保持不变。是的,您可以这样添加,但问题最初是关于 NSLayoutConstraint。
    【解决方案3】:

    首先,您必须将视图添加为其容器的子视图。完成此操作后,您需要将 translatesAutoResizingMaskToConstraints 设置为 false。然后是时候添加你的NSLayoutConstraints

        let labelWidth:CGFloat = 320
        let labelHeight:CGFloat = 30
        // Container is what you are adding label too
        let container = UIView(frame: CGRect(x: 0, y: 0, width: 500, height: 500))
        let label = UILabel(frame: CGRect(x: 0, y: (container.frame.size.height / 2) - (labelHeight / 2), width: labelWidth, height: labelHeight))
        container.addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
    
        // Add Width and Height (IF is required,
        // if not required, anchor your label to appropriately)
        label.addConstraints([
            NSLayoutConstraint.init(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: labelWidth),
            NSLayoutConstraint.init(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: labelHeight),
        ])
    
        label.layoutIfNeeded()
    
        // Add left constraint and center in container constraint
        container.addConstraints([
            NSLayoutConstraint.init(item: label, attribute: .left, relatedBy: .equal, toItem: .container, attribute: .left, multiplier: 1.0, constant: 0.0),
            NSLayoutConstraint.init(item: label, attribute: .centerX, relatedBy: .equal, toItem: .container, attribute: .left, multiplier: 1.0, constant: 0.0)
        ])
    
        container.layoutIfNeeded()
    

    编辑

    是的,当您使用点语法时,Xcode 会自动为您提供文档。如果您转到属性的参数并输入一个句点,您将看到所有可能选项的下拉列表。

    【讨论】:

    • 你指的是哪个常量?如果您指的是标签的宽度和高度,我将其设置在代码的最顶部。
    • 文档中是否有任何地方可以看到 NSLayoutConstraints 的所有可能属性?
    • 我已经更新了我的问题,以便更好地传达我到底想要做什么
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多