简体   繁体   English

快速自动布局多个视图/标签

[英]Swift autolayout multiple views/labels

I want to programmatically divide my screen into three parts. 我想以编程方式将屏幕分为三个部分。 Each part shall be a label with a backgroundColor and the width is set to be: 每个部分应为带有backgroundColor的标签,并且宽度设置为:

// Get the device width
self.view.bounds.width  

The height must be dynamically. 高度必须是动态的。 An example is in the image below. 下图是一个示例。 Each color is one part. 每种颜色都是一个部分。 The first part (the red part) must be attached to the top and the last part (the orange) must be attached to the bottom. 第一部分(红色部分)必须连接到顶部,而最后一部分(橙色部分)必须连接到底部。 And dependent on the label size inside each part the height shall be adjusted. 并根据每个部分内部的标签尺寸来调整高度。 图片

The blue part is always 100% and dependent of the red and orange part it subtracts from the blue part. 蓝色部分始终为100%,并取决于从蓝色部分中减去的红色和橙色部分。

Anyone have any ideas of how to achieve this? 有人对如何实现这一目标有任何想法吗? Appreciate help! 感谢帮助!

To do this is best to use AutoLayout, using NSLayoutConstraint s. 为此,最好使用带有NSLayoutConstraint Here's some sample code (at the moment this is all in viewDidLoad , which probably isn't the best thing to do, but I'll leave it up to you to organise the code.) 这是一些示例代码(目前,这一切都在viewDidLoad ,这可能不是最好的选择,但我viewDidLoad自行组织代码。)

override func viewDidLoad() {
    super.viewDidLoad()

    //  1.
    var topView: UIView = self.view

    // Have as many labels as you want!
    for i in 0..<numberOfLabels {
        let label = UILabel()

        //  2.
        label.numberOfLines = 0
        label.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(label)

        //  3.
        if i == 0 || i == numberOfLabels - 1 {
            label.setContentHuggingPriority(UILayoutPriority.abs(1000), forAxis: UILayoutConstraintAxis.Vertical)
        }

        let widthConstraint = NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1.0, constant: 0.0)
        //  4.
        let heightConstraint = NSLayoutConstraint(item: label, attribute: .Height, relatedBy: .GreaterThanOrEqual, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 20.0)

        //  5.
        let attribute: NSLayoutAttribute = i == 0 ? .Top : .Bottom
        let topConstraint = NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: topView, attribute: attribute, multiplier: 1, constant: 0)

        //  6.
        if i == numberOfLabels - 1 {
            let bottomConstraint = NSLayoutConstraint(item: label, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1, constant: 0)

            self.view.addConstraint(bottomConstraint)
        }

        label.addConstraint(heightConstraint)

        self.view.addConstraint(widthConstraint)
        self.view.addConstraint(topConstraint)

        topView = label
    }
}

1. topView is the view that the label 's NSLayoutAttribute.Top will be related to. 1. topViewlabelNSLayoutAttribute.Top将与之相关的视图。 For example: the first label's top is related to its container view; 例如:第一个标签的顶部与其容器视图相关; the second label's top is related to the first label; 第二个标签的顶部与第一个标签有关; the third label's top is related to the second view, and so on. 第三个标签的顶部与第二个视图相关,依此类推。

2. Set the number of lines to zero to allow for as many lines as required by label.text . 2.将行数设置为零,以允许label.text所需的label.text

3. Set the content hugging priority of the top and bottom labels to 1000 . 3.将顶部和底部标签的内容包含优先级设置为1000 This will cause the top and bottom labels to 'hug' the text because middle label(s), by default, have a hugging priority of 250 . 这将导致顶部和底部标签“拥抱”文本,因为默认情况下,中间标签的拥抱优先级为250

4. Create an NSLayoutConstraint that sets the label to be higher than 20 points because for preferredMaxLayoutWidth to be set automatically the width and height of the label must be explicitly defined by NSLayoutConstraint s. 4.创建一个NSLayoutConstraint ,将label设置为高于20点,因为要自动设置preferredMaxLayoutWidth ,必须由NSLayoutConstraint显式定义标签的宽度和高度。

5. This selects the attribute on topView that will pin the label to the topView . 5.这将选择topView上的属性,该属性会将labeltopView For example: if creating the first label , its top is pinned to the top of the container view. 例如:如果创建第一个label ,则其顶部将固定在容器视图的顶部。 Otherwise, the top of the label is pinned to the bottom of the label above it. 否则, label的顶部将固定在其上方的label的底部。

6. If it's the last label, pin its bottom edge to the bottom of the container view. 6.如果是最后一个标签,则将其底部边缘固定在容器视图的底部。

Here's the result: 结果如下:

在此处输入图片说明

Hope that answers your question. 希望这能回答你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM