【问题标题】:iOS - Changing UIBarButtonItem's heightiOS - 更改 UIBarButtonItem 高度
【发布时间】:2011-09-08 15:22:44
【问题描述】:

UIToolbar 有一个很好的调整大小的选项 (self.navigationController.toolbar.frame) 我想知道是否有人知道改变 UIBarButtonItem 高度的方法?

我有一个高度为 117 像素的自定义工具栏,但到目前为止我还没有找到修改工具栏上按钮的方法。我还需要它作为工具栏,因为当我在第一个视图中设置资产时,显示的视图会被另一个动画视图(过场动画样式)覆盖,并且工具栏需要在所有过程中保持在顶部。

【问题讨论】:

    标签: ios ipad uibarbuttonitem uitoolbar


    【解决方案1】:

    实际上我自己也遇到过这个问题,我唯一能想到的就是利用 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 完成的另一件漂亮的事情是传递一个带有按钮和其他东西(如活动指示器)的工具栏。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      使用您喜欢的框架和图像以及目标和选择器等创建一个UIButton,然后使用UIBarButtonIteminitWithCustomView: 方法并将UIButton 用作customView。

      【讨论】:

      • 您确实先发帖,但 Octo 的回答更完整。我希望我可以拆分复选标记。但我确实发现您的一些帖子很有见地,并给予了他们应得的支持。
      【解决方案3】:

      让这个简单的三个步骤

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

      现在,当您在 uibarbuttomitem 上有 uibutton 时,您可以定义 uibutton 的所有属性,例如图像、操作等。

      【讨论】:

        猜你喜欢
        • 2016-12-14
        • 1970-01-01
        • 2012-01-28
        • 2012-06-14
        • 1970-01-01
        • 2016-04-05
        • 2015-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多