问题
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.
回答1:
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.
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/7350494/ios-changing-uibarbuttonitems-height