# 子元素隐藏
- 禁止子元素超出父元素高度
- 父元素设置:
clipsToBounds = YES
1
# 动态加约束
- 代码添加多个元素的相互约束需要添加:
// subview 需添加约束元素本身
blueBtn.translatesAutoresizingMaskIntoConstraints = false;
1
2
2
blueBtn.widthAnchor.constraint(equalToConstant: 200).isActive = true;
blueBtn.leadingAnchor.constraint(equalTo: blueView.leadingAnchor, constant: 100).isActive = true;
1
2
3
2
3
- 集体生效
let btnLeading = blueBtn.leadingAnchor.constraint(equalTo: blueView.leadingAnchor, constant: 100);
let btnTop = blueBtn.bottomAnchor.constraint(equalTo: blueView.bottomAnchor, constant: 100);
let btnBottom = blueBtn.topAnchor.constraint(equalTo: blueView.topAnchor, constant: 100);
NSLayoutConstraint.activate([btnLeading, btnTop, btnBottom]);
1
2
3
4
2
3
4
# 动态添加元素
- 子元素的x/y是依照在父元素的边界实现的
let btn1 = UIButton(frame: CGRect(x: 200, y: 100, width: 100, height: 100));
btn1.setTitle("哈哈哈", for: UIControl.State.normal);
blueView.addSubview(btn1);
btn1.backgroundColor = UIColor.blue;
1
2
3
4
2
3
4
# 设置动态添加元素附着点
// 设置附着view
pickerView?.popoverPresentationController?.sourceView = self.datePickView
// 设置附着元素
pickerView?.popoverPresentationController?.sourceRect = self.datePickerButton.frame
1
2
3
4
5
2
3
4
5
# 动态改变优先级
redHeight0.priority = redHeight0.priority.rawValue < 201 ? UILayoutPriority(rawValue: 1000.0) : UILayoutPriority(rawValue: 200.0)
1
# 动态改变滚动高度
ScrollView
控制顶部、底部距离为固定值,中间子盒子一个一个撑起ScrollView- 设置
ScrollView
与父盒子/兄弟上下左右的高度属性,priority
设置最高 scrollView
设置与子View的top相等,子View的bottom
相等ScrollView
与子View的bottom
的priority
设置低一点
- 设置
- tableView?
# 断点调试
po self.contentScrollView
1
# 获取指定元素距离顶部的距离
// 获取【相对于父元素】位置
let offsetY = deepBlueView.frame.origin.y
// scrollView 滚动到指定位置
self.contentScrollView.contentOffset = CGPoint.init(x: 0, y: offsetY)
1
2
3
4
5
2
3
4
5