在我的应用程序中,我在 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。我可以在我的 NavigationBar 上得到它,但它没有调用我想要的方法。这是我的代码:
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;