IOS: simple autostart with 1 tag and 3 buttons

I play with AutoLayout and I really bang my head against the wall because it looks like this should be an extremely simple solution.

  • I have a vertical column of controls: 1 label and 3 buttons.
  • I want the label to be 40 pixels (dots) high and automatically determine its width depending on the width of its supervisor (standard interval on the left, top and right).
  • I want the 3 buttons to line up vertically under this label.
  • I would like their width to be automatic as a label.
  • I would like their interval to be the standard (aqua?) Interval (8 points, right?).
  • I would like the 3 buttons to be the same height.

I can get what I want to work, but I constantly get errors in the console at runtime, and I would like to find out WHY I get them and HOW to avoid them. I watched a WWDC video on AutoLayout, and here is what I have tried so far:

UILabel *label = [self Label]; MMGlossyButton *button1 = ... MMGlossyButton *button2 = ... MMGlossyButton *button3 = ... [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[label]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button1]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(new)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button2]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(existing)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[button3]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(provider)]]; [[self view] addConstraints: [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[label(40)]-[button1(>=25)]-[button2(==button1)]-[button3(==button1)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(label, button1, button2, button3)]]; 

So, this works to display the view dynamically, but the following error appears on the console:

 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) // A whole bunch of constraint stuff that doesn't appear to be important... Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7554c40 V:[MMGlossyButton:0x7554b40(99)]> 

So, the last bit shows that the first button that I find in the view is statically up to 99 points in size.

What size does it look like.

This is completely arbitrary.

Which I do not want to appoint, but I can’t determine the way not to appoint it.

Although I get what I want (sort of, after all), it seems like this is a REALLY roundabout way to make something pretty simple. Am I missing something basic in AutoLayout, or does its power require such complexity?

+1
ios autolayout
Nov 16 '12 at
source share
1 answer

You encounter errors because you mix and match the constraints generated in the code with the constraints added by the interface constructor. The interface designer does not allow you to create an ambiguous layout, so by definition, if you add additional restrictions, you will get the "Unable to simultaneously satisfy" , which is the fall of many marriages.

To solve this problem, you either need to define all the restrictions that you need in the interface builder, or you need to mark certain objects as outputs and delete them in the code before adding your own.

In your case, the restrictions are simple enough to create in IB.

You can click on a specific height using this button in IB when your shortcut is selected:

enter image description here

The one in the middle looks like a beam. This gives you the following useful menu:

enter image description here

Selecting one of them allows you to create a new constraint for the label, which you can then edit by selecting it:

enter image description here

Then you can add your buttons, select all three of them and, using the same menu, create a restriction of equal heights.

Constraints created in IB are not particularly flexible, so if you decide that you need to create or modify them in your code, it is best to create output for certain constraints, and then either delete or recreate them, or change the constant value to lead time.

+7
Nov 16 '12 at 19:03
source share




All Articles