【问题标题】:UIToolbar precise sizeUIToolbar 精确大小
【发布时间】:2012-01-18 06:41:40
【问题描述】:

我正在尝试借助工具栏自定义我的 NavigationBar。 我以编程方式实现它如下:

UIToolbar* tools = [[UIToolbar alloc] initWithFrame: CGRectMake(0, 0, 100, 44.01)];

然后我将它添加到我的 NavigationBar。问题是我对边界产生了这种丑陋的影响:

我尝试更改 y 和 height 值,但没有结果。 您有什么想法可以避免这种情况吗?

提前致谢,亚萨

【问题讨论】:

  • 您正在向导航栏添加工具栏?
  • 是的,我在一些教程中发现了这种方法……这是错误的方法吗?
  • 是的,看 shannogans 的回答——这是“正确”的方式

标签: ios height uinavigationbar toolbar uitoolbar


【解决方案1】:

我不会这样做。 您可以通过向 navigationItem.rightBarButtonItem 添加一个带有 2 个按钮的视图来实现相同的效果。很简单:

// view that will hold the buttons
UIView* container = [[UIView alloc] init];

// create 1 button and add it to the container
UIButton* button = [[UIButton alloc] init........];
[container addSubview:button];


//create 2 button and add it to the container
button = [[UIButton alloc] init.........];
[container addSubview:button];


// now create a Bar button item
UIBarButtonItem* barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:container];

// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = barButtonItem;

【讨论】:

    【解决方案2】:

    我部分同意以前的答案和 cmets。 您建议的解决方案适用于自定义按钮。但是如果我想实现标准的编辑按钮呢? 对标准按钮/图标的访问是通过 UIBarButtonItem 类,而不是 UIButton。而且您不能将 UIBarButtonItem 对象添加到 UIView。

    在网络上进行了多次研究后,我找到了完全满足我要求的解决方案。工具栏必须按以下方式创建:

    UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 95.0f, 44.01f)];
    tools.tintColor = self.navigationController.navigationBar.tintColor;
    tools.barStyle = -1;
    

    结果如下:

    希望对您有所帮助! 亚萨

    【讨论】:

      【解决方案3】:

      或者你可以这样做。 只需像这样创建 UIToolbar 的新子类

         @interface MyToolbar : UIToolbar
      
          @end
      
          @implementation MyToolbar
      
          - (id)initWithFrame:(CGRect)frame {
      
              if ((self = [super initWithFrame:frame])) {
      
                  self.opaque = NO;
                  self.backgroundColor = [UIColor clearColor];
                  self.clearsContextBeforeDrawing = YES;      
              }
              return self;
          }
      
      
          - (void)drawRect:(CGRect)rect {
              // do nothing
          }
      
          - (void)dealloc {
              [super dealloc];
          }
      
      @end
      

      并将其用作普通的 UIToolbar。我不知道为什么,但它确实有效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-04-06
        • 2020-08-18
        • 1970-01-01
        • 1970-01-01
        • 2011-11-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多