Reputation: 9493
Desired Scenario: create a custom Alert View via iOS6's AutoLayout vs Frames:
1) Create an empty UIView upon the host view (UIController.view):
alertView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[alertView(==300)]" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[alertView]-|" options:0 metrics:nil views:viewsDict]];
This works: I see my alert UIView upon (subview) the host view.
However, attempting to add UILabel to the Alert view bombs.
Seeing that the UIView (1) was drawn, I merely substituted the UILabel in its stead to see if I can get something:
- (UILabel *)createLabelWithMessage:(NSString *)message {
if (!message) return nil;
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0,0)];
myLabel.text = message;
[myLabel setFont:[UIFont systemFontOfSize:12.0]];
[myLabel setAutoresizingMask:UIViewAutoresizingNone];
myLabel.backgroundColor = [UIColor clearColor];
return myLabel;
}
...
titleLabel = [self createLabelWithMessage:@"Danger"];
...
// ...replacing 'alertView' (UIView) with 'titleView' (UILabel):
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[titleLabel(==300)]" options:0 metrics:nil views:viewsDict]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[titleLabel]-|" options:0 metrics:nil views:viewsDict]];
Question: Why would the UILabel bomb but the UIView appears to be drawing okay?
Here's the hint from Xcode:
AutoLayoutContraints[3828:11303] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. Try this:(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) ( "", "", "" )Will attempt to recover by breaking constraint (Names: '|':UIView:0x128727a0 )>
There must be a hidden property within UILabel that's screwing up my 'Visual-Formatting' language.
..I created another subview of "alertView"; and I get the same error. So apparently I'm only getting a 'good' result when I merely display one (1) UIView (the 'alertView') upon (subview) of the UIController's view; nothing more.
Something hidden is conflicting the simple constraints. And I don't know what.
BTW: I'm using a NIB as the host UIView, with 'use autoLayout' 'ON'. However, I'll be using this within larger code without 'autolayout' (iOS 4.3+), within an iOS6-checked routine.
Upvotes: 1
Views: 623
Reputation: 9493
Solution: (per my comment) I had missed setting the 'setTranslatesAutoresizingMaskIntoConstraints' flag to 'NO' for the particular subview; albeit I had done it for its super container.
Example:
[testView setTranslatesAutoresizingMaskIntoConstraints:NO];
Don't forget to use 'setTranslatesAutoresizingMaskIntoConstraints' for ALL member UIViews!
Upvotes: 1