Reputation: 1020
I'm creating a UIToolBar programmatically, it works fine except for iPhone X where I'd like the toolbar to expand depending on phone's orientation. The expansion works if toolbar was created using the interface builder.
I'm not setting any height constraint, even implicitly via autolayout mask.
Code:
UIToolbar* toolbar = [[UIToolbar alloc] init];
toolbar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:toolbar];
[self.view addConstraints:@[
[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeLeft multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
[NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeRight multiplier:1 constant:0]]];
So, what should I do make the toolbar behave properly?
Upvotes: 2
Views: 2283
Reputation: 1020
Ok, the partial answer is to use the safe area self.view.safeAreaLayoutGuide
, or with context:
[NSLayoutConstraint constraintWithItem:self.view.safeAreaLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:toolbar attribute:NSLayoutAttributeBottom multiplier:1 constant:0],
The 'partial' part refers to the fact that IB toolbar has height of 49, and manual toolbar has 44.
It's good enough for me, but if someone is able to fix/explain the 44-49 difference, I'll mark it as a solution.
Edit: I've found the missing part.
I wasn't calling invalidateIntrinsicContentSize
. Adding a call from willTransitionToTraitCollection: withTransitionCoordinator:
fixed that. Somehow, for last year or so, I've missed that the toolbar was changing its size in compact mode.
Upvotes: 2