[英]UIBarButtonItem with UIButton as custom view doesn't show up
I am trying to display a custom UIButton
as a UINavigationItem
's rightBarButtonItem
. 我试图将自定义
UIButton
显示为UINavigationItem
的rightBarButtonItem
。
I wrote the following code: 我写了以下代码:
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 30, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
viewController.navigationItem.rightBarButtonItem = inviteButtonContainer;
Nothing shows up in my navigation bar. 我的导航栏中没有显示任何内容。
However, with the following code, I can see a basic button: 但是,使用以下代码,我可以看到一个基本按钮:
UIBarButtonItem *inviteButton = [[UIBarButtonItem alloc] initWithTitle:@"Invite"
style:UIBarButtonItemStyleDone
target:self
action:nil];
viewController.navigationItem.rightBarButtonItem = inviteButton;
But it's obviously not what I am after, since I am looking to play with the button appearance.
UIBarButtonItem
( inviteButtonContainer
) to many navigation bars.
UIBarButtonItem
( inviteButtonContainer
)实例添加到许多导航栏。
Doing that, it's visible on the last navigation bar only.
UIBarButtonItem
(without custom view), it's displayed on every navigation bars.
UIBarButtonItem
的相同实例(没有自定义视图),它将显示在每个导航栏上。
Any idea why?
Your code is fine, just change this line: 你的代码很好,只需改变这一行:
viewController.navigationItem.rightBarButtonItem = inviteButtonContainer;
to 至
self.navigationItem.rightBarButtonItem = inviteButtonContainer;
From the Documentation: 从文档:
The navigation item used to represent the view controller in a parent's navigation bar.
用于表示父级导航栏中视图控制器的导航项。
self
itself represents that it belongs to view controller , so there is no need to mention the view controller instance to add the navigation item. self
本身表示它属于视图控制器,因此无需提及视图控制器实例来添加导航项。
-> To display custom button in a view controller in which custom bar button is crated (ie in self) : - >在视图控制器中显示自定义按钮,其中自定义栏按钮是自定义的(即自我):
- (void)viewDidLoad {
[super viewDidLoad];
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 70, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
controller.navigationItem.rightBarButtonItem = inviteButtonContainer;
}
-> To display custom bar button in a view controller that is about to present - >在即将出现的视图控制器中显示自定义栏按钮
eg. 例如。 To display a view controller after a cell is selected in table view
在表视图中选择单元格后显示视图控制器
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DjDetailsViewController *controller = (DjDetailsViewController *)
[self.storyboard instantiateViewControllerWithIdentifier:@"DjDetailsViewController"];
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
[inviteButton setTitle:@"Invite" forState:UIControlStateNormal];
inviteButton.frame = CGRectMake(0, 0, 70, 30);
inviteButton.backgroundColor = [UIColor redColor];
inviteButton.layer.borderColor = [UIColor blueColor].CGColor;
inviteButton.layer.borderWidth = 1.0f;
UIBarButtonItem *inviteButtonContainer = [[UIBarButtonItem alloc] initWithCustomView:inviteButton];
controller.navigationItem.rightBarButtonItem = inviteButtonContainer;
[self.navigationController pushViewController:controller animated:YES];
}
I didn't done major. 我没有做过专业。 I just used your code and change frame of custom bar button.
我只是使用你的代码并更改自定义栏按钮的框架。 I hope this will help you.
我希望这能帮到您。
You issue is with your invite button initialiser: 您的问题是使用您的邀请按钮初始化:
UIButton *inviteButton = [UIButton UIButtonTypeCustom];
UIButtonTypeCustom
is an enum value not a class method/convenience constructor. UIButtonTypeCustom
是一个枚举值,而不是类方法/便利构造函数。
use 采用
UIButton *inviteButton = [UIButton buttonWithType:UIButtonTypeCustom];
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.