After reading this chapter, test your knowledge with these exercises:
- A label is constrained with 8-point offsets from its superview’s leading and trailing edges. It is 22 points high. Is this label’s layout ambiguous? If so, how can you remove the ambiguity?
- You create a system-style button and assign it the title Continue. The button’s center is constrained to a point (150, 150) from its superview’s top and leading edges. Is this view’s layout ambiguous? If so, how can you remove the ambiguity?
In viewWillAppear: you create a new test view and add it to your view controller:
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 30)]; view.backgroundColor = [UIColor blueColor]; [self.view addSubview:view]; view.translatesAutoresizingMaskIntoConstraints = NO;
After these lines, you add constraints that center the test view within its superview. What size will the view be when the app runs? Why?
- A 54-by-54-point image consists of a 50-by-50-point square, with a drop shadow offset 4 points to the right and 4 points down. (a) Show code that assigns alignment insets to this image. (b) When the image is added to an image view and center-aligned to its superview on both axes, what geometric point within the image lies at the center of the superview?
- You add a button to your view and constrain it to stretch from side to side at a priority of 500. Will it stretch? Why or why not?