2016-08-22 59 views
0

我知道这个问题已被问过无数次,但我似乎无法完全解决这个问题。根据子视图自动设置容器视图的高度

使用自动布局,我想根据它的子视图自动设置我的容器UIView的高度。我已经看过使用sizeToFit和其他各种方法来总结我的子视图的高度,但从我读过的高度来看,使用自动布局时,我的容器高度应该是自动的,因为子视图“内在”内容大小。

下面是我遇到的一个简化案例。我会很感激任何指导!

概述:

  1. 创建容器的UIView,销到左,上海华盈的右侧,没有明确的高度,对准其centerY与它的父centerY
  2. 创建300宽度100高度的UIView,添加它作为容器视图的子视图,将其centerX与容器视图的centerX对齐,将其与容器视图的顶部边缘对齐
  3. 重复步骤2,除了这次将其顶部固定到#2的底部边缘
  4. 容器视图的预期高度是200,除了它的高度是a ctually仍为0(为此centerY对齐关闭)

Issue visual

代码:

class ViewController: UIViewController { 

    let redView = RedView() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     view.addSubview(redView) 
     view.setNeedsUpdateConstraints() 
    } 

} 

class RedView: UIView { 

    let greenView = GreenView() 
    let blueView = BlueView() 

    init() { 
     super.init(frame: CGRect.zero) 

     translatesAutoresizingMaskIntoConstraints = false 
     backgroundColor = UIColor.red() 

     addSubview(greenView) 
     addSubview(blueView) 
     setNeedsUpdateConstraints() 
    } 

    override func updateConstraints() { 
     super.updateConstraints() 

     NSLayoutConstraint(item: greenView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: blueView, attribute: .top, relatedBy: .equal, toItem: greenView, attribute: .bottom, multiplier: 1, constant: 0).isActive = true 

     NSLayoutConstraint(item: self, attribute: .left, relatedBy: .equal, toItem: superview, attribute: .left, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: self, attribute: .right, relatedBy: .equal, toItem: superview, attribute: .right, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: superview, attribute: .centerY, multiplier: 1, constant: 0).isActive = true 
     NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200).isActive = true 
    } 

} 

class GreenView: UIView { 

    init() { 
     super.init(frame: CGRect.zero) 

     translatesAutoresizingMaskIntoConstraints = false 
     backgroundColor = UIColor.green() 
    } 

    override func updateConstraints() { 
     super.updateConstraints() 

     NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 300).isActive = true 
     NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100).isActive = true 
     NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: superview, attribute: .centerX, multiplier: 1, constant: 0).isActive = true 
    } 

} 

class BlueView: UIView { 

    init() { 
     super.init(frame: CGRect.zero) 

     translatesAutoresizingMaskIntoConstraints = false 
     backgroundColor = UIColor.blue() 
    } 

    override func updateConstraints() { 
     super.updateConstraints() 

     NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 300).isActive = true 
     NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 100).isActive = true 
     NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: superview, attribute: .centerX, multiplier: 1, constant: 0).isActive = true 
    } 

} 
+0

题外话:我不喜欢RedView如何为superview添加约束。如果只有父视图知道如何布置它的子视图,这会容易得多。在你的情况下,ViewController会在调用'view.addSubview(redView)'后立即向RedView添加约束。 – chuthan20

回答

1

你需要引脚蓝景的底部redView的底部,只是加入这一行redView的updateConstraints

NSLayoutConstraint(item: blueView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0).active = true 
+0

啊,这很有道理。谢谢! –

+0

@EricQian,请你检查一下我的问题bro https://stackoverflow.com/questions/45690065/auto-height-does-not-work-in-uiview-in-swift-3?noredirect=1#comment78337799_45690065? –

相关问题