我有以下设置:
(50,50) 的固定 intrinsicContentSize正如预期的那样,这为我提供了一个 50x50 的 View ,以应用程序窗口为中心。现在,如果我都:
...为什么自动布局系统不抛出异常?
我希望这样的系统要求 View 宽度为 50pts(因为它的内在内容宽度为 50pts 并且需要它的拥抱优先级)和 100pts 宽(因为它在 100pts 有一个必需的显式宽度约束),因此不一致。相反,该 View 很高兴有 100pts 宽,(似乎)没有考虑其内容拥抱。
我用来重现此结果的整批代码(在新的空应用程序的应用程序委托(delegate) .m 文件中):
@interface TEFixedSizeView : UIView
@end
@implementation TEFixedSizeView
- (CGSize)intrinsicContentSize;
{
return (CGSize){.width = 50.0f, .height = 50.0f};
}
@end
@implementation TEAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
TEFixedSizeView *view = [[TEFixedSizeView alloc] init];
view.translatesAutoresizingMaskIntoConstraints = NO;
view.backgroundColor = [UIColor redColor];
[self.window addSubview:view];
[self.window addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:self.window
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0f]];
[self.window addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:self.window
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0f]];
[view setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[view addConstraint:[NSLayoutConstraint constraintWithItem:view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:0.0f
constant:100.0f]];
[self.window makeKeyAndVisible];
return YES;
}
@end
最佳答案
在发布关于这个问题的推文后,我得到了一位 Auto Layout 原作者的回复:
internal optimization causing odd effects; ends up treating hugging as "optional with priority 1000." i.e., "required" is kind of a special case, and optimized path for intrinsic size constraints isn't checking for it.
看起来这应该 抛出异常,但实际上没有。我已经提交了 rdar://problem/15106765 ( OpenRadar )。
关于ios - 自动布局 : why do conflicting required hugging priorities & fixed-width constraints not throw an exception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19064345/