【问题标题】:Autolayout Resize UIView Not Working自动布局调整 UIView 大小不起作用
【发布时间】:2016-02-28 11:16:54
【问题描述】:

我正在尝试让 UIView courseView 自动布局。我想让 UIView 的比例保持并填充,直到最外边缘距离超级视图边缘 15 点。 由于某种原因 courseView 填充了整个超级视图(减去 15 个点)并且不会调整大小以适应。所以有些不显示,被截断了。

    self.view.addSubview(courseView!)

    let aspectConstraint = NSLayoutConstraint(item: courseView,
        attribute: .Height,
        relatedBy: .Equal,
        toItem: courseView,
        attribute: .Width,
        multiplier: courseView.frame.size.height / courseView.frame.size.width,
        constant: 0.0)
    aspectConstraint.active = true

    let topConstraint = courseView.topAnchor.constraintGreaterThanOrEqualToAnchor(topLayoutGuide.bottomAnchor, constant: 15)
    topConstraint.active = true
    let leadingConstraint = courseView.leadingAnchor.constraintLessThanOrEqualToAnchor(view.leadingAnchor, constant: 15)
    leadingConstraint.active = true
    let trailingConstraint = courseView.trailingAnchor.constraintGreaterThanOrEqualToAnchor(view.trailingAnchor, constant: -15)
    trailingConstraint.active = true
    let bottomConstraint = courseView.bottomAnchor.constraintLessThanOrEqualToAnchor(view.bottomAnchor, constant: -15)
    bottomConstraint.active = true

有什么想法吗?谢谢!

【问题讨论】:

    标签: ios swift autolayout nslayoutconstraint


    【解决方案1】:

    在添加约束之前禁用 translatesAutoresizingMaskIntoConstraints,它应该可以正常工作。

    courseView.translatesAutoresizingMaskIntoConstraints = false;
    

    顺便说一句,您不需要 aspectConstraint,因为这很可能会破坏约束(当我尝试时,它对我来说确实如此)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多