【问题标题】:setting the frame of a customView in UIBarButtonItem在 UIBarButtonItem 中设置 customView 的框架
【发布时间】:2015-06-29 08:30:10
【问题描述】:

我在做

mapButton *topMapButton = [[mapButton alloc] init];
    [topMapButton setSize:CGSizeMake(70, 30)];
    topMapButton.frame = CGRectMake(0, STATUS_HEIGHT +7, 70, 30);

    UIBarButtonItem *barIcon = [[UIBarButtonItem alloc] initWithCustomView: topMapButton];
    [topMapButton addTarget:self action:@selector(openMap:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = barIcon;

其中 mapButton 是 UIButton 子类,具有预设外观,仅此而已

无论我为topMapButton 设置什么框架,它一直在 (0,0) 中,而不是在 rightBarButtonItem 通常的位置。

请注意,使用自动布局以编程方式设置位置适用于放置,但是当我将另一个控制器推入堆栈时崩溃...

我做错了吗?

【问题讨论】:

  • 是的,你不能那样做。无法指定其他来源。

标签: ios uinavigationbar customization


【解决方案1】:

看看这个answer

很快,您可以设置边缘插图。覆盖您的 uibutton 子类的 alignmentRectInsets 并更改顶部和左侧插图。

- (UIEdgeInsets)alignmentRectInsets {
    UIEdgeInsets insets =  UIEdgeInsetsMake(top_value, left_value, bottom_value, right_value);

    return insets;
}

【讨论】:

    【解决方案2】:

    OK 找到问题了..

    似乎 UIBarButtonItem 并不真正关心 customView 有什么框架,因为它无论如何都会设置它。

    但是,我的自定义按钮必须具有自定义类型,否则它会弄乱框架并使其 (0,0) 保留在应用程序屏幕上。

    self = [UIButton buttonWithType:UIButtonTypeCustom];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多