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
  • IMHO this is not possible with auto layout, it sound like a collapse feature of a split view, what is your requirement? Commented Dec 11, 2012 at 19:43
  • Hi Stephan, thanks for the reply. My requirement is to create a single layout in code that will work for iPhone & iPad. I am creating page layouts with text and images (the data is held in a PLIST). For certain layouts where the image is almost the full width there is very little space for text, so would like it to snap to the full width in these cases. Any ideas would be appreciate. Cheers Dave. Commented Dec 12, 2012 at 7:37
  • well i see and the framework shall handle as much as possible. Well i still think you should not use auto-layout for this requirements. Auto-layout is nice to keep a static UI in shape under different conditions but if the layout depends on the content I think you have to go back to regular code. Commented Dec 13, 2012 at 8:03
  • Cheers @Stephan, have gone back to good old fashioned layoutSubViews. Thanks for the comments though, helpful. Dave. Commented Dec 13, 2012 at 8:21

2 Answers 2

1

Any reason you couldn't just apply the appropriate constraint based on the platform?

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];

Also, based on your comments it wasn't clear if you had tried using priorities or not... did that work?

Sign up to request clarification or add additional context in comments.

1 Comment

Hi MobileVet, I don't think there is a solvable set of priorities as the priority would be based on the relationship between the width of the image and the width of the view it is placed in. I might be wrong but I couldn't get it to work. Additionally it isn't the platform that will dictate rule, rather with width of the image in relation to it's superView. Hope that makes sense. Think you might be on to something though in conditionally applying constraints. Dave.
0

So as discussed, it seems auto-layout can not handle this (yet) in version MacOS 10.8. Therefore i suggest to implement it in standard code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.