【问题标题】:UIBarButtonItem created with UIButton not showing up in UIToolbar使用 UIButton 创建的 UIBarButtonItem 未显示在 UIToolbar 中
【发布时间】:2017-11-18 15:09:01
【问题描述】:

我正在尝试使用 UIButton 作为其自定义视图来制作 UIBarButtonItem。当我制作它时,它不会出现。这是我声明按钮的方式:

self.optionsButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.optionsButton setFrame:CGRectMake(0, 0, 30, 20)];
[self.optionsButton setTitle:@"Options" forState:UIControlStateNormal];
[self.optionsButton addTarget:self
                       action:@selector(presentOptions)
             forControlEvents:UIControlEventTouchUpInside];
[self.optionsButton setTintColor:[UIColor clearColor]];
[self.optionsButton setBackgroundColor:[[UIColor blueColor] colorWithAlphaComponent:0.1] forUIControlState:UIControlStateSelected];
[self.optionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.optionsButton setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
self.optionsButton.layer.cornerRadius = 5;
self.optionsButton.clipsToBounds = YES;
[self.optionsButton sizeToFit];

这就是我声明 UIToolbar 的方式

self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 44)];
NSMutableArray *items = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpaceBefore = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:flexSpaceBefore];
[items addObject:[[UIBarButtonItem alloc] initWithCustomView:self.optionsButton]];
UIBarButtonItem *flexSpaceAfter = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:flexSpaceAfter];
[self.toolbar setItems:items];

[self.view addSubview:self.toolbar];
CGRect toolbarFrame = self.toolbar.frame;
toolbarFrame.origin.y = self.view.frame.size.height - toolbarFrame.size.height - self.view.frame.origin.y;
toolbarFrame.size.width = self.view.frame.size.width;
self.toolbar.frame = toolbarFrame;

以下是我根据其他帖子尝试过的事情列表没有用:

  • One Two Three Four:使用导航控制器工具栏和[NavController setToolbarItems - 它向我展示了工具栏(我确保不再显示我的工具栏)但没有按钮
  • One: 确保按钮有一个有效的框架 - 从我的代码中你可以清楚地看到按钮有一个适合文本的有效框架

因此,如果有人对为什么这不起作用有任何想法,我将非常感谢您的帮助。在这一点上,我基本上希望这是我忽略的一些非常愚蠢的事情,因为我不想继续研究这么长时间:(

编辑:
工具栏和按钮最初声明为

@property (nonatomic, strong) UIButton *optionsButton;
@property (nonatomic, strong) UIToolbar *toolbar;

【问题讨论】:

  • @property(strong,nonatomic) UIButton *optionsButton 尝试将您的按钮属性设置为 (strong,nonatomic)

标签: ios objective-c uibutton uibarbuttonitem uitoolbar


【解决方案1】:

查看您的代码后。我发现如果您将按钮属性保持为(弱),按钮将不会添加到工具栏中。所以保持你的按钮属性为(强,非原子)

@property(strong,nonatomic) UIButton *optionsButton

As Strong 表示只要该属性指向一个对象,该对象就不会被自动释放。在非 ARC 中,它是保留的同义词

what is the difference between strong and weak property?

【讨论】:

  • 对不起,应该在我的问题中包含声明。工具栏和按钮都声明为(nonatomic, strong)。
【解决方案2】:

我想通了!就像我希望的那样,这是一件愚蠢的事情。我直接在viewDidLoad 中设置了按钮,并在辅助函数中设置了工具栏。我原以为我在声明按钮之前调用了辅助函数,但事实证明我在声明按钮之前就调用了它……经验教训:更多地使用断点和 Xcode 调试器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 2012-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2014-09-01
    相关资源
    最近更新 更多