【发布时间】: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