iOS - Changing UIBarButtonItem's height

2019-05-18 22:50发布

UIToolbar has a nice option for resizing (self.navigationController.toolbar.frame) I'm wondering if anyone knows of a way to change the height of a UIBarButtonItem?

I have a custom toolbar with a height of 117 pixels and so far I haven't found a way of modifying the buttons on the toolbar. Also I need it to be a toolbar because the displayed view gets covered with another animated view (cut scene style) while I setup assets in the first view and the toolbar needs to stay on top during all of it.

3条回答
forever°为你锁心
2楼-- · 2019-05-18 23:07

Make this simple three steps

1 Write property:

@property (nonatomic, strong) IBOutlet UIToolbar *toolBarActive;
@property (nonatomic, strong) IBOutlet UIButton *uiButton;
@property (nonatomic, strong) IBOutlet UIBarButtonItem *uiButtonItem;

2 Synthesize:

@synthesize toolBarActive = _toolBarActive;
@synthesize uiButton = _uiButton;
@synthesize uiButtonItem = _uiButtonItem;

3 Write implementation:

-(UIToolbar *) toolBarActive {
    if( _toolBarActive == nil ) {
        _toolBarActive = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
        _toolBarActive.tintColor = [UIColor blackColor];  
        NSMutableArray *arrayItem = [[NSMutableArray alloc] init];
        [arrayItem addObject:self.uiButtonItem];
        [_toolBarActive setItems:arrayItem animated:YES];
    }
    return _toolBarActive;
}

- (UIButton *) uiButton {
    if( _uiButton == nil ) {
        _uiButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 50, 50)];
        [_uiButton addTarget:self action:@selector(deletePointFromTrip) forControlEvents:UIControlEventTouchUpInside];
        [_uiButton setImage:[UIImage imageNamed:@"editable.png"] forState:UIControlStateNormal];
    }
    return _uiButton;
}

- (UIBarButtonItem *) uiButtonItem {
    if( _uiButtonItem == nil ) {
        _uiButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.uiButton];
        [_uiButtonItem setEnabled:YES];
    }
    return _uiButtonItem;
}

Now when you have uibutton on uibarbuttomitem you can define all property for uibutton like, image, action etc.

查看更多
男人必须洒脱
3楼-- · 2019-05-18 23:13

I've actually run into this myself and the only thing I could come up with was leveraging initWithCustomView and passing in a UIButton with a defined frame.

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

/*
* Insert button styling
*/

button.frame = CGRectMake(0, 0, width, height);

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

Otherwise UIBarButtonItem only has a width property that can be set but unfortunately not a height property. Another nifty thing I've done with initWithCustomView is to pass in a toolbar with a button and other things like activity indicators. Hope this helps.

查看更多
Summer. ? 凉城
4楼-- · 2019-05-18 23:29

create a UIButton with your preferred frame and image and target and selector and etc. then use UIBarButtonItem's initWithCustomView: method and use the UIButton as the customView.

查看更多
登录 后发表回答