我对此进行了很多研究,并使用了几种不同的方法。此代码位于我的ViewController.m
:
1)第一种方法
UIImage *settingsimage = [UIImage imageNamed:@"gearicon"];
UIBarButtonItem *settingsBarButton =[[UIBarButtonItem alloc] initWithImage:settingsimage style:(UIBarButtonItemStylePlain) target:self action:@selector(switchpages)];
self.navigationItem.leftBarButtonItem = settingsBarButton;
此代码执行时没有任何错误,但没有按钮出现!
2)第二种方法
UIButton *settingbtn = [UIButton buttonWithType:UIButtonTypeCustom];
[settingbtn addTarget:self action:@selector(switchpages) forControlEvents:UIControlEventTouchUpInside];
[settingbtn setImage:[UIImage imageNamed:@"gearicon.png"] forState:UIControlStateNormal];
UIBarButtonItem *newBarItem = [[UIBarButtonItem alloc] initWithCustomView:settingbtn];
self.navigationItem.leftBarButtonItem = newBarItem;
同上,没有错误,但没有按钮。请在我出错的地方帮忙。感谢。
答案 0 :(得分:1)
所以,这是我的最终解决方案。这会在NavigationBar的右上角放置一个“齿轮”图标,没有边框:
·H
@property (strong, nonatomic) IBOutlet UINavigationBar *navBar;
@property (strong, nonatomic) IBOutlet UINavigationItem *settingsbtn;
@property(strong, nonatomic)UIButton *navigationButton;
的.m
@synthesize navigationButton;
@synthesize settingsbtn;
@synthesize navBar;
ViewDidLoad
//custom settings button
navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[navigationButton addTarget:self action:@selector(swithpages) forControlEvents:UIControlEventTouchUpInside];
[navigationButton setImage:[UIImage imageNamed:@"gearicon.png"]forState:UIControlStateNormal];
[navigationButton setFrame:CGRectMake(0, 0, 30, 35)];
UIBarButtonItem *settingsbutton = [[UIBarButtonItem alloc] initWithCustomView:navigationButton];
settingsbtn.rightBarButtonItem = settingsbutton;
我唯一缺少的是按下“发光”按钮(如Apple的远程应用程序),而不是默认情况下更换颜色。
答案 1 :(得分:0)
您应该尝试使用按钮后面的ImageView
图像,它会发挥作用
如果您没有使用任何NavigationController并希望了解如何将NavigationItem添加到NavigationBar,那么您应确保在xib上添加NavigationBar并在其连接检查器中连接IBOutlet。在这里你的课程&代码看起来像......
在您的.h
文件中
@property(strong,nonatomic)UIImageView *navigationImageView;
@property(strong,nonatomic)UIButton *navigationButton;
@property (strong,nonatomic) IBOutlet UINavigationBar *navBar;
<{1>}文件中的
.m
如果您使用带图像的按钮
,首先创建一个ImageView并在里面添加自定义图像@synthesize navigationButton;
@synthesize navigationImageView;
@synthesize navBar;
现在将图像视图的帧设置为图像
UIImage *backImage = [UIImage imageNamed:@"back.png"];
self.navigationImageView = [[UIImageView alloc] initWithImage:backImage];
创建自定义按钮并指定图像
[self.navigationImageView setFrame: CGRectMake(5, 7, backImage.size.width, backImage.size.height)];
将图片视图添加到导航栏
self.navigationButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.navigationButton setImage:backImage forState:UIControlStateNormal];
使用按钮创建[self.navBar addSubview:self.navigationImageView];
[self.navBar addSubview:self.navigationButton];
[self.navBar bringSubviewToFront:self.navigationButton];
作为自定义方法的自定义视图
UIBarButtonItem
最后你的自定义backMethod你想做什么...为了测试你可以显示警告
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.navigationButton];
self.navigationItem.leftBarButtonItem = customBarItem;
[self.navigationButton addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];