iOS – 更改UIBarButtonItem的高度

UIToolbar有一个很好的selectresize(self.navigationController.toolbar.frame)我想知道是否有人知道的方式来更改UIBarButtonItem的高度?

我有一个高度为117像素的自定义工具栏,到目前为止,我还没有find一种方法来修改工具栏上的button。 此外,我需要它是一个工具栏,因为显示的视图被另一个animation视图(剪切场景样式)覆盖,而我在第一个视图中设置资产,工具栏需要保持在所有的顶部。

我实际上碰到了这个问题,我唯一能想到的就是利用initWithCustomView并传入一个定义好的框架的UIButton。

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; /* * Insert button styling */ button.frame = CGRectMake(0, 0, width, height); UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

否则UIBarButtonItem只有一个可以设置的宽度属性,但不幸的是不是一个高度属性。 我用initWithCustomView完成的另一件漂亮的事情是在工具栏中传递一个button和其他事物,如活动指示器。 希望这可以帮助。

创build一个UIButton与您首选的框架和图像和目标和select器等,然后使用UIBarButtonIteminitWithCustomView:方法,并使用UIButton作为customView。

使这个简单的三个步骤

1写属性:

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

2合成:

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

3写实现:

 -(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; } 

现在,当你在uibarbutomom中有uibutton时,你可以为uibutton定义所有的属性,如图像,动作等。