I implement a custom container that is very similar to the UINavigationController, except that it does not contain the entire controller stack. It has a UINavigationBar, which is limited to the topLayoutGuide container controller, which turned out to be 20px on top, which is good.
When I add a child view controller and put its view in the hierarchy, I want its topLayoutGuide to be seen in IB and used to highlight the child elements of the child view controller's view that appear at the bottom of my navigation bar. It should be noted what should be done in the relevant documentation:
The value of this property is, in particular, the length value of the property of the object returned when this property is requested. This value is limited either by the view controller or by closing the container controller (for example, the navigation bar or the controller tabs):
- A view controller that is not in the container view controller restricts this property to indicate the bottom of the status bar, if one is visible,
or specify the top edge of the view controller view. - The view controller in the container view controller does not set this property value. Instead, the container view controller limits the value to:
- Bottom of the navigation bar if the navigation bar is displayed
- The bottom of the status bar, if only the status bar is visible
- The top edge of the view controller view if neither the status bar nor the navigation bar is displayed.
But I don’t quite understand how to “limit its value”, since both the topLayoutGuide properties and its lengths are readonly.
I tried this code to add a child view controller:
[self addChildViewController:gamePhaseController]; UIView *gamePhaseControllerView = gamePhaseController.view; gamePhaseControllerView.translatesAutoresizingMaskIntoConstraints = NO; [self.contentContainer addSubview:gamePhaseControllerView]; NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"|-0-[gamePhaseControllerView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(gamePhaseControllerView)]; NSLayoutConstraint *topLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.topLayoutGuide attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.navigationBar attribute:NSLayoutAttributeBottom multiplier:1 constant:0]; NSLayoutConstraint *bottomLayoutGuideConstraint = [NSLayoutConstraint constraintWithItem:gamePhaseController.bottomLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1 constant:0]; [self.view addConstraint:topLayoutGuideConstraint]; [self.view addConstraint:bottomLayoutGuideConstraint]; [self.contentContainer addConstraints:horizontalConstraints]; [gamePhaseController didMoveToParentViewController:self]; _contentController = gamePhaseController;
In IB, I define "Under Top Bars" and "Under Bottom Bars" for the game PhaseController. One of the views is specially tied to the top guide of the layout, one way or another on the device it looks 20px from the bottom of the container’s navigation bar ...
What is the correct way to implement a custom container controller with this behavior?
ios objective-c autolayout uiviewcontroller ios7
Danchoys Oct 25 '13 at 11:09 2013-10-25 11:09
source share