UIScrollerView 与 AutoLayout 的狗血故事

image.png

一个常见的情景是:
用 AutoLayout 使用 UIScrollView,使得 UIScrollView 的子视图能撑开 UIScrollView,而不用手动设置 contentSize,因为有时候子视图的内容是不固定的,从而高度也不固定。

如何做?(以垂直滑动举例说明)

  1. 如果是 UILabel,宽度可以根据 scrollView 来设置。高度不必管,它会自己撑开。
NSLayoutConstraint.activate([
    contentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
    contentLabel.leftAnchor.constraint(equalTo: titleLabel.leftAnchor),
    contentLabel.rightAnchor.constraint(equalTo: titleLabel.rightAnchor),
    contentLabel.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -20),
])
  1. 所有子视图都必须设置 translatesAutoresizingMaskIntoConstraints 为 NO。因为一个 View 如果是用代码创建的,那它的translatesAutoresizingMaskIntoConstraints 默认为 YES,为 YES 的含义是:这个 View 现在使用AutoResizing 进行布局,AutoLayout会根据 autoresizing mask,给这个 View 创建一系列「同等行为」的约束(the system creates a set of constraints that duplicate the behavior specified by the view’s autoresizing mask),这时候开发者就不能写自己的 AutoLayout 约束了,冲突了。
    所以,要把translatesAutoresizingMaskIntoConstraints 设置为 NO,写自己的 AutoLayout 约束,这样才能动态地计算View 的位置和大小。

    鱼与熊掌不可兼得

  2. 最后一个子视图(最底或者最右,当然也可能是最左和最上,此处以最底示范)添加「跟 ScrollView底部的约束」,这样才能撑开ScrollView

bottomLabel.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -10)

完整可运行代码

import UIKit

class ViewController: UIViewController {
    
    let scrollView = UIScrollView(frame: .zero)
    let titleLabel = UILabel(frame: .zero)
    let contentLabel = UILabel(frame: .zero)
    let bottomLabel = UILabel(frame: .zero)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.white
        setupSubviews()
    }
}

extension ViewController {
    func setupSubviews() {
        // 1. 设置 scrollView 的约束
        view.addSubview(scrollView)
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
            scrollView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 20),
            scrollView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            scrollView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor)
            ])
        
        // 2.添加子控件,设置内部约束
        titleLabel.text = "===========titleLabel==========="
        scrollView.addSubview(titleLabel)
        
        titleLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            titleLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 10),
            titleLabel.leftAnchor.constraint(equalTo: scrollView.leftAnchor, constant: 10),
            titleLabel.rightAnchor.constraint(equalTo: scrollView.rightAnchor, constant: -10)
            ])
        
        contentLabel.text = "将此处替换为你自己的字符串(超过一屏),即可看见滑动效果"
        contentLabel.numberOfLines = 0
        scrollView.addSubview(contentLabel)
        
        contentLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            contentLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 10),
            contentLabel.leftAnchor.constraint(equalTo: titleLabel.leftAnchor),
            contentLabel.rightAnchor.constraint(equalTo: titleLabel.rightAnchor),
            contentLabel.widthAnchor.constraint(equalTo: scrollView.widthAnchor, constant: -20),
            ])
        
        scrollView.addSubview(bottomLabel)
        
        bottomLabel.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            bottomLabel.topAnchor.constraint(equalTo: contentLabel.bottomAnchor, constant: 10),
            bottomLabel.leftAnchor.constraint(equalTo: titleLabel.leftAnchor),
            bottomLabel.rightAnchor.constraint(equalTo: titleLabel.rightAnchor),
            // 3.最后一个 subview,添加跟 scrollView 的约束
            bottomLabel.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor, constant: -10)
            ])
    }
}



参考文档:

  1. 掘金文章
  2. Stack Overflow 大佬的回答
  3. Apple 文档:translatesAutoresizingMaskIntoConstraints
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容