UINavigationBar的烦恼

发布于 2024-12-23 13:11:08 字数 2046 浏览 3 评论 0原文

在我的应用程序中,我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2024-12-30 13:11:08

嗯,看来您想在底部工具栏添加一个按钮?这就是你可以这样做的方法:

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Title" target:self action:@selector(myButtonClicked:)];
    [toolbar setItems:[NSArray arrayWithObjects:myButton, nil]];

Hm, seems you want to add add a Button to the Bottom toolbar? That's how you could do that:

UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:@"Title" target:self action:@selector(myButtonClicked:)];
    [toolbar setItems:[NSArray arrayWithObjects:myButton, nil]];
眼眸里的那抹悲凉 2024-12-30 13:11:08

您在 UIViewController 上设置的 backBarButtonItem 有一个问题,但是当该视图可见时它不会显示,只有当该视图是要返回的视图时它才会显示。

backBarButtonItem
当导航栏上需要后退按钮时要使用的栏按钮项目。
@property(非原子,保留) UIBarButtonItem *backBarButtonItem
讨论
当此导航项紧邻堆栈中的顶部项下方时,导航控制器从此导航项派生导航栏的后退按钮。当此属性为零时,导航项使用其 title 属性中的值来创建适当的后退按钮。如果要为后退按钮指定自定义图像或标题,则可以将自定义栏按钮项(带有自定义标题或图像)分配给此属性。配置栏按钮项目时,不要为其分配自定义视图;无论如何,导航项都会忽略后栏按钮中的自定义视图。
该属性的默认值为 nil。


对于未调用的方法,您需要设置要调用的 @selector

您可以使用此方法来创建按钮

- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

编辑
对于后退按钮
不要创建 UIButton,直接创建 UIBarButton。
以下是我创建 backBarButtonItem 的方法。

UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Maison.png"]
                                                        style:UIBarButtonItemStylePlain
                                                       target:nil
                                                       action:nil];
self.navigationItem.backBarButtonItem = bbi;
[bbi release], bbi = nil;

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.

backBarButtonItem
The bar button item to use when a back button is needed on the navigation bar.
@property(nonatomic, retain) UIBarButtonItem *backBarButtonItem
Discussion
When this navigation item is immediately below the top item in the stack, the navigation controller derives the back button for the navigation bar from this navigation item. When this property is nil, the navigation item uses the value in its title property to create an appropriate back button. If you want to specify a custom image or title for the back button, you can assign a custom bar button item (with your custom title or image) to this property instead. When configuring your bar button item, do not assign a custom view to it; the navigation item ignores custom views in the back bar button anyway.
The default value of this property is nil.


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

- (id)initWithImage:(UIImage *)image style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action

Edit
For the back button
Don't create a UIButton, go for the UIBarButton directly.
Here is how I create a backBarButtonItem.

UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Maison.png"]
                                                        style:UIBarButtonItemStylePlain
                                                       target:nil
                                                       action:nil];
self.navigationItem.backBarButtonItem = bbi;
[bbi release], bbi = nil;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文