Skip to content

Commit

Permalink
Use autolayout constraints to set size of custom bar button item
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eliperkins committed Feb 13, 2019
1 parent 8ba9796 commit 2147db1
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/ios/RNNUIBarButtonItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#import "RNNUIBarButtonItem.h"
#import "RCTConvert+UIBarButtonSystemItem.h"

@interface RNNUIBarButtonItem ()

@property (nonatomic, strong) NSLayoutConstraint *widthConstraint;
@property (nonatomic, strong) NSLayoutConstraint *heightConstraint;

@end

@implementation RNNUIBarButtonItem

-(instancetype)init:(NSString*)buttonId withIcon:(UIImage*)iconImage {
Expand All @@ -26,21 +33,37 @@ -(instancetype)init:(NSString*)buttonId withCustomView:(RCTRootView *)reactView
reactView.sizeFlexibility = RCTRootViewSizeFlexibilityWidthAndHeight;
reactView.delegate = self;
reactView.backgroundColor = [UIColor clearColor];
self.widthConstraint = [NSLayoutConstraint constraintWithItem:reactView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:1.0];
self.heightConstraint = [NSLayoutConstraint constraintWithItem:reactView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:1.0];
[NSLayoutConstraint activateConstraints:@[self.widthConstraint, self.heightConstraint]];
self.buttonId = buttonId;
return self;
}

-(instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
- (instancetype)init:(NSString*)buttonId withSystemItem:(NSString *)systemItemName {
UIBarButtonSystemItem systemItem = [RCTConvert UIBarButtonSystemItem:systemItemName];
self = [super initWithBarButtonSystemItem:systemItem target:nil action:nil];
self.buttonId = buttonId;
return self;
}

- (void)rootViewDidChangeIntrinsicSize:(RCTRootView *)rootView {
CGSize size = rootView.intrinsicContentSize;
rootView.frame = CGRectMake(0, 0, size.width, size.height);
self.width = size.width;
self.widthConstraint.constant = rootView.intrinsicContentSize.width;
self.heightConstraint.constant = rootView.intrinsicContentSize.height;
[rootView setNeedsUpdateConstraints];
[rootView updateConstraintsIfNeeded];
}

- (void)onButtonPressed {
Expand Down

0 comments on commit 2147db1

Please sign in to comment.