UINavigationBar的烦恼
在我的应用程序中,我在 NavigationController 中使用自定义 UINavigationBar 和自定义 UIBarButtonItems。我的(自定义)NavigationBar 看起来不错,但是当我想放置自己的 backBarButtonItem 和 RightBarButtonItem 时,它就会出错。
安装我自己的 backBarButtonItem 根本不起作用(在 PushViewController 方法之后)。我使用这段代码:
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
home.frame = CGRectMake(0, 0, 51, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithCustomView:home];
[self.navigationItem setBackBarButtonItem:backButton];
我也有自己的 rightBarButtonItem。我可以在我的导航栏上获取它,但它没有调用我想要执行的方法。这是我的代码:
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
home.frame = CGRectMake(0, 0, 51, 30);
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithCustomView:home];
// self.navigationItem.backBarButtonItem = cancelButton;
[self.navigationItem setBackBarButtonItem:cancelButton];
在互联网上寻找解决方案几个小时后没有结果,我希望你们能帮助我!
谢谢!
编辑:抱歉,上一期发布了错误的代码:
UIButton *home2 = [UIButton buttonWithType:UIButtonTypeCustom];
[home2 setTitle:@"Sort" forState:UIControlStateNormal];
[home2.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
UIImage *homeImage2 = [UIImage imageNamed:@"Topbarbutton"];
[home2 setBackgroundImage:homeImage2 forState:UIControlStateNormal];
home2.frame = CGRectMake(0, 0, 77, 30);
UIBarButtonItem *cancelButton2 = [[UIBarButtonItem alloc]
initWithCustomView:home2];
[cancelButton2 setTarget:self];
[cancelButton2 setAction:@selector(sorteren)];
self.navigationItem.rightBarButtonItem = cancelButton2;
In my app, i'm using a custom UINavigationBar and custom UIBarButtonItems in a NavigationController. My (custom) NavigationBar looks fine, but when I want to place my own backBarButtonItem and RightBarButtonItem, it goes wrong.
Installing my own backBarButtonItem doesn't work at all (after a PushViewController method). I use this code:
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
home.frame = CGRectMake(0, 0, 51, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithCustomView:home];
[self.navigationItem setBackBarButtonItem:backButton];
I also have my own rightBarButtonItem. I am able to get it on my NavigationBar, but it isn't calling the method I want to do it. Here's my code for that one:
UIButton *home = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *homeImage = [UIImage imageNamed:@"Topbarback"];
[home setBackgroundImage:homeImage forState:UIControlStateNormal];
home.frame = CGRectMake(0, 0, 51, 30);
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithCustomView:home];
// self.navigationItem.backBarButtonItem = cancelButton;
[self.navigationItem setBackBarButtonItem:cancelButton];
After a couple of hours looking on the internet for a solution with no result, I hope that you guys can help me!
Thanks!
EDIT: Sorry posted the wrong code for the last issue:
UIButton *home2 = [UIButton buttonWithType:UIButtonTypeCustom];
[home2 setTitle:@"Sort" forState:UIControlStateNormal];
[home2.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:15]];
UIImage *homeImage2 = [UIImage imageNamed:@"Topbarbutton"];
[home2 setBackgroundImage:homeImage2 forState:UIControlStateNormal];
home2.frame = CGRectMake(0, 0, 77, 30);
UIBarButtonItem *cancelButton2 = [[UIBarButtonItem alloc]
initWithCustomView:home2];
[cancelButton2 setTarget:self];
[cancelButton2 setAction:@selector(sorteren)];
self.navigationItem.rightBarButtonItem = cancelButton2;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
嗯,看来您想在底部工具栏添加一个按钮?这就是你可以这样做的方法:
Hm, seems you want to add add a Button to the Bottom toolbar? That's how you could do that:
您在 UIViewController 上设置的
backBarButtonItem
有一个问题,但是当该视图可见时它不会显示,只有当该视图是要返回的视图时它才会显示。对于未调用的方法,您需要设置要调用的 @selector
您可以使用此方法来创建按钮
编辑
对于后退按钮
不要创建 UIButton,直接创建 UIBarButton。
以下是我创建 backBarButtonItem 的方法。
There is a catch with the
backBarButtonItem
you set it on the a UIViewController, but it won't show up when that view is visible, it will show up only when that view is the one to go back to.And for the method not being called you need to set the @selector to be called
You could use this method to create your button
Edit
For the back button
Don't create a UIButton, go for the UIBarButton directly.
Here is how I create a backBarButtonItem.