直接上代码,看ViewDidLoad方法中的注释。一共10个约束,UIScrollView 上下左右4个,其子试图contentView 上下左右宽高6个。
注意点:(以要求可以上下滚动为例,可以先看完代码再回头理解注意事项)
1、contentView的宽高根据需求设置优先级,哪边需要滚动,优先级就必须低,因此这里设置高度优先级为.low。
2、最底部试图必须和contentView试图必须有底部约束。
let contentView = UIView() lazy var bgScrollView : UIScrollView = { let view = UIScrollView() // view.backgroundColor = .blue view.showsHorizontalScrollIndicator = false view.showsVerticalScrollIndicator = false return view }()
bgView是最底部视图。
private let bgView = UIView().then { $0.backgroundColor = .white $0.layer.cornerRadius = 5 $0.layer.masksToBounds = true }
然后往bgView里,添加子视图即可。
view.addSubview(bgScrollView) bgScrollView.snp.makeConstraints { make in make.top.equalTo(searchView.snp.bottom).offset(10) make.left.right.bottom.equalToSuperview() } bgScrollView.addSubview(contentView) contentView.snp.makeConstraints { make in make.top.left.right.bottom.equalToSuperview() make.width.equalTo(kScreenWidth) make.height.equalToSuperview().priority(.low) } if #available(iOS 11.0, *) { bgScrollView.contentInsetAdjustmentBehavior = .never } else { automaticallyAdjustsScrollViewInsets = false } contentView.addSubview(bgView) bgView.snp.makeConstraints { maker in maker.left.equalToSuperview().offset(15) maker.right.equalToSuperview().offset(-15) maker.top.equalTo(0) maker.height.equalTo(1000) // maker.bottom.equalTo(-50) maker.bottom.equalToSuperview().offset(-30).priority(.high) }
作者:IMKel
链接:https://www.jianshu.com/p/f84c8241222f
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 标签:contentView,make,SnapKit,equalToSuperview,UIScrollView,使用,maker,view From: https://www.cnblogs.com/huangzs/p/17306929.html