更新时间:2023-02-13 12:41:01
我发现的一个解决方案是重载addConstraint:
方法并在添加约束之前对其进行修改:
The one solution I found is to overload the addConstraint:
method and modify constraints before they'll be added:
- (void)addConstraint:(NSLayoutConstraint *)constraint
{
if(constraint.firstItem == self || constraint.secondItem == self) {
if(constraint.firstAttribute == NSLayoutAttributeLeading) {
constraint.constant += self.leftBorderWidth;
} else if (constraint.firstAttribute == NSLayoutAttributeTrailing) {
constraint.constant += self.rightBorderWidth;
} else if (constraint.firstAttribute == NSLayoutAttributeTop) {
constraint.constant += self.topBorderWidth;
} else if (constraint.firstAttribute == NSLayoutAttributeBottom) {
constraint.constant += self.bottomBorderWidth;
}
}
[super addConstraint:constraint];
}
然后还要在xxxBorderWidth
设置器中处理此约束.
And then also handle this constraints in xxxBorderWidth
setters.