ios - 基于 subview 自动设置容器 View 的高度

标签 ios uiview uikit ios-autolayout

我知道这个问题已经被问过很多次了,但我似乎无法弄清这个问题的真相。

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

以下是我遇到的情况的简化案例。我真的很感激任何指导!

概述:

  1. 创建容器 UIView,固定到父 View 的左右两侧,没有明确的高度,将其 centerY 与其父 View 的 centerY 对齐
  2. 创建一个 300 宽 x 100 高的 UIView,将其作为 subview 添加到容器 View 中,将其 centerX 与容器 View 的 centerX 对齐,固定到容器 View 的顶部边缘
  3. 重复步骤 #2,除了这次将其顶部固定到 #2 的底部边缘
  4. 容器 View 的预期高度为 200,除了它的高度实际上仍为 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
    }

}

最佳答案

您需要将blueView的底部固定到redView的底部,只需将此行添加到redView的updateConstraints中:

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

关于ios - 基于 subview 自动设置容器 View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39090152/

相关文章:

ios - 如何使用 Swift 将带有部分的歌曲列表添加到 UITableView?

ios - 如何从 Xcode 中删除多余的 iOS 模拟器条目?

ios - 设置SIAlertView的宽度

ios - 构造具有唯一中心的 UIView 对象的 NSArray

objective-c - 如何在选择器方法中发送两个参数?

iphone - 图像缩放、 ScrollView 、裁剪

ios - 如何快速隐藏customView中的按钮

ios - 奇怪的 UIViewController hidesBottomBarWhenPushed 布局错误,仅在第一次推送 View Controller 时发生

uikit - CGRectMake、CGPointMake、CGSizeMake、CGRectZero、CGPointZero 在 Swift 中不可用

ios - 如何在 Alamofire 中使用相同的 API 函数 .PUT 请求更新 JSON