1

This is my first attempt at using constraints, so please excuse the elementary question.

I want to be able to set a series of constraints so that a subView is either less than 75% of the width of my view OR the width of my view. So if my subView's width is greater than 75% of the view's width then set it to be the full width. I want to do this to ensure the subView displays well on an iPhone and iPad.

Is this possible using constraints? I have set up two constraints, but how do I tell the constraints system which one to choose based on the width of my view, as I am not sure that this can be solved just using priorities?

// Ensure it's width is either the less than 3 quarters of the page width or the full page width (including the gutter margins)
NSLayoutConstraint *constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0.75 constant:0];
[self.constraints addObject:constraint];

constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
[self.constraints addObject:constraint];

// Add the constraints to the page
[self addConstraints:self.constraints];

Any help greatly appreciated as getting rather bogged down.

Dave

4

2 回答 2

1

有什么理由不能只根据平台应用适当的约束?

NSLayoutConstraint *constraint;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:0.75 constant:0];
}
else {
  constraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
}
[self addConstraint:constraint];

另外,根据您的评论,不清楚您是否尝试过使用优先级……那行得通吗?

于 2012-12-13T20:13:34.590 回答
0

如前所述,在 MacOS 10.8 版本中,自动布局似乎还不能处理这个问题。因此我建议在标准代码中实现它。

于 2012-12-13T08:40:42.097 回答