大佬教程收集整理的这篇文章主要介绍了ios – 在约束依赖于框架的自定义视图中使用自动布局,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
- (void)updateConsTraints { [self.superview addConsTraint:[NSLayoutConsTraint consTraintWithItem:self attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeCenterX multiplier:1 constant:0]]; [self.superview addConsTraint:[NSLayoutConsTraint consTraintWithItem:self attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeTop multiplier:1 constant:0]]; [self.superview addConsTraint:[NSLayoutConsTraint consTraintWithItem:self attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.superview attribute:NSLayoutAttributeBottom multiplier:1 constant:0]]; // some more consTraints,you get the point self.bottomSpacingConsTraint = [NSLayoutConsTraint consTraintWithItem:self.imageView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:-(0.2 * CGRectGetHeight(self.bounds))]; [self addConsTraint:self.bottomSpacingconstraint]; [super updateConsTraints]; }
问题是self.bounds返回CGRectZero的等价物.我做了我的研究并根据这个objc.io article,这是预期的,因为在调用layoutSubviews之前框架不会被设置.它也提到了
但是,当我添加
[self setNeedsLayout]; [self layoutIfNeeded];
在updateConsTraints中设置self.bottomSpacingConsTraint之前,我仍然得到一个CGRectZero返回框架.根据objc.io@L_675_7@(和本SO answer),这些方法应该触发布局并更新框架.
任何人都可以对如何使这一切工作有所启发吗?我对解决方案感兴趣,并解释了导致调用布局相关方法的原因(例如,看起来在layoutSubviews中更改现有约束的常量会导致调用setNeedsupdateConsTraints,然后触发updateConsTraints并导致约束要多次添加).
以上是大佬教程为你收集整理的ios – 在约束依赖于框架的自定义视图中使用自动布局全部内容,希望文章能够帮你解决ios – 在约束依赖于框架的自定义视图中使用自动布局所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。