Skip to content

Commit 2147db1

Browse files
committed
Use autolayout constraints to set size of custom bar button item
This fixes an issue where the frame for the custom view can be set to the incorrect y-offset upon setting the custom frame. In iOS 11, this behavior changed, as UIBarButtonItem went from being using springs-and-struts for sizing, to using a UIStackView, and thus using Autolayout. This lead to the superview of having a frame of (0, 22, 0, 0) at the first layout pass. By moving to using NSLayoutConstaints, we can now properly size our custom view. See also: https://gist.github.com/niw/569b49648fcab22124e1d12c195fe595 See also: https://stackoverflow.com/questions/10988918/change-width-of-a-uibarbuttonitem-in-a-uinavigationbar
1 parent 8ba9796 commit 2147db1

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

lib/ios/RNNUIBarButtonItem.m

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
#import "RNNUIBarButtonItem.h"
44
#import "RCTConvert+UIBarButtonSystemItem.h"
55

6+
@interface RNNUIBarButtonItem ()
7+
8+
@property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
9+
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;
10+
11+
@end
12+
613
@implementation RNNUIBarButtonItem
714

815
-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
@@ -26,21 +33,37 @@ -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView
2633
reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
2734
reactView.delegate = self;
2835
reactView.backgroundColor = [UIColor clearColor];
36+
self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
37+
attribute:NSLayoutAttributeWidth
38+
relatedBy:NSLayoutRelationEqual
39+
toItem:nil
40+
attribute:NSLayoutAttributeNotAnAttribute
41+
multiplier:1.0
42+
constant:1.0];
43+
self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
44+
attribute:NSLayoutAttributeHeight
45+
relatedBy:NSLayoutRelationEqual
46+
toItem:nil
47+
attribute:NSLayoutAttributeNotAnAttribute
48+
multiplier:1.0
49+
constant:1.0];
50+
[NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
2951
self.buttonId = buttonId;
3052
return self;
3153
}
32-
33-
-(instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
54+
55+
- (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
3456
UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
3557
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
3658
self.buttonId = buttonId;
3759
return self;
3860
}
3961

4062
- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
41-
CGSize size = rootView.intrinsicContentSize;
42-
rootView.frame = CGRectMake(0, 0, size.width, size.height);
43-
self.width = size.width;
63+
self.widthConstraint.constant = rootView.intrinsicContentSize.width;
64+
self.heightConstraint.constant = rootView.intrinsicContentSize.height;
65+
[rootView setNeedsUpdateConstraints];
66+
[rootView updateConstraintsIfNeeded];
4467
}
4568

4669
- (void)onButtonPressed {

0 commit comments

Comments
 (0)