1

我需要在导航栏中自定义“设置”按钮。但是,不幸的是,我无法正确进入情节提要编辑器。我只需要查看我的图片,没有按钮矩形。

在此处输入图像描述

有没有办法正确地使用某种按钮?

谢谢。

4

1 回答 1

2

-viewDidLoad您可以在 ViewController方法中添加此代码:

对于左栏按钮:

UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"NavBarMapBtn.png"];
[leftButton setImage:buttonImage forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[leftButton addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];

或右栏按钮:

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:@"NavBarMapBtn.png"];
[rightButton setImage:buttonImage forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[rightButton addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

无需在 xib 或情节提要中将 BarButtonItem 添加到您的视图中。

这是它的样子:

在此处输入图像描述

于 2013-04-15T15:10:19.863 回答