我有两种看法:toastView
和view
。toastView
是的子视图view
。我想toastView
在y轴上定位view
高度的80%。如何使用代码中的常量来做到这一点?
我假设有一个类似的方法:
[toastView.topAnchor constraintEqualToAnchor:view.heightAnchor multiplier:0.8].active = YES;
但我不能混合NSLayoutDimension
(宽度和高度)和NSLayoutYAxisAnchor
(X和Y)
这是它在设计中的外观:
这里的关键是要设定最高的toastView
等于底部的self.view
一个乘数的0.8
:
目标C:
[NSLayoutConstraint constraintWithItem: toastView attribute: NSLayoutAttributeTop
relatedBy: NSLayoutRelationEqual toItem: self.view
attribute: NSLayoutAttributeBottom multiplier: 0.8 constant: 0].active = YES;
迅速:
NSLayoutConstraint(item: toastView, attribute: .top, relatedBy: .equal,
toItem: self.view, attribute: .bottom, multiplier: 0.8, constant: 0).isActive = true