0

我对此进行了很多研究,并使用了几种不同的方法。此代码位于我的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;

同上,没有错误,但没有按钮。请帮助我哪里出错了。谢谢。

4

2 回答 2

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的远程应用程序)而不是默认颜色更改。

于 2013-03-01T21:06:27.190 回答
0

您应该尝试ImageView在按钮后面加上图片,它会成功的

如果您没有使用任何 NavigationController 并且想查看如何将 NavigationItem 添加到 NavigationBar,那么您应该确保在 xib 上添加 NavigationBar 并在其 Connection Inspector 中连接 IBOutlet。这里你的类和代码会是什么样子......

在你的.h文件中

@property(strong,nonatomic)UIImageView *navigationImageView;
@property(strong,nonatomic)UIButton *navigationButton;
@property (strong,nonatomic) IBOutlet UINavigationBar *navBar;

.m文件中

@synthesize navigationButton;
@synthesize navigationImageView;
@synthesize navBar;

如果您使用带图像的按钮,请首先创建一个 ImageView 并在其中添加自定义图像

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作为自定义方法的自定义视图

  UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.navigationButton];
    self.navigationItem.leftBarButtonItem = customBarItem;
    [self.navigationButton addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];

最后你的自定义 backMethod 无论你想在这做什么......为了测试你可以显示警报

-(void)backMethod{
    UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Info" message:@"custom navigation" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];

}
于 2013-02-28T20:56:22.907 回答