我知道当另一个视图控制器被推入堆栈时,会显示viewcontroller's
导航项的backBarButtonItem
,这是从顶部开始的第二个viewcontroller
。
我的viewcontroller A在viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBarHidden = NO;
[self.navigationItem setBackBarButtonItem:[UIBarButtonItem itemWithImageNamed:@"ic_header_slide" selectedImage:nil target:nil action:nil]];
}
当我按viewcontroller B
时,此自定义后退按钮未显示,而是显示iOS创建的默认后退按钮。
A展开UITableViewController
,B展开UIViewController
。我没有在任何这些navigationItem中设置leftBarButtonItem
,leftBarButtonItems
,rightBarButtonItem
,rightBarButtonItems
。
EDIT 我已经阅读了有关设置leftBarButtonItems的内容。在B上设置leftbarbuttonitems。但我认为在A上设置backBarButtonItem是正确的方法。它也在文档中提到但在我的情况下不起作用。我想问一下backBarButtonItem中是否有bug,或者我对它的工作方式有一些误解,我没有正确地做到这一点。
答案 0 :(得分:0)
要隐藏导航栏使用的默认后退按钮,
self.navigationItem.hidesBackButton=TRUE;
还可以使用以下方法添加自定义BarButtons
- (NSArray*)getLeftNavButtons:(NSString*)image andTarget:(id)target andFrame:(CGRect)frame andSpace:(int)fixedSpace
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = frame;
button.clipsToBounds = YES;
[button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[button addTarget:target action:@selector(leftNavBtnClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithCustomView:button];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7"))
{
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = fixedSpace;
return @[negativeSpacer,barButton];
}
else{
return @[barButton];
}
return @[barButton];
}
答案 1 :(得分:0)
只需覆盖默认
即可self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc]init];
back.title = @"Pick Me";
back.image = @"Your image";
[self.navigationItem setLeftBarButtonItem:back];
答案 2 :(得分:0)
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]init];
UIImage *img1=[UIImage imageNamed:@"edit"];
CGRect frameimg1 = CGRectMake(0, 0, img1.size.width, img1.size.height);
UIButton *signOut=[[UIButton alloc]initWithFrame:frameimg1];
[signOut setBackgroundImage:img1 forState:UIControlStateNormal];
[signOut addTarget:self action:@selector(btnEditClicked:)
forControlEvents:UIControlEventTouchUpInside];
// [signOut setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *barButton=[[UIBarButtonItem alloc]initWithCustomView:signOut];
self.navigationItem.rightBarButtonItem=barButton;
UIImage *img11=[UIImage imageNamed:@"home"];
CGRect frameimg11 = CGRectMake(0, 0, img11.size.width, img11.size.height);
UIButton *signOut1=[[UIButton alloc]initWithFrame:frameimg11];
[signOut1 setBackgroundImage:img11 forState:UIControlStateNormal];
[signOut1 addTarget:self action:@selector(showLeftMenuPressed:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButton1=[[UIBarButtonItem alloc]initWithCustomView:signOut1];
self.navigationItem.leftBarButtonItem=barButton1;
self.navigationController.navigationBar.barTintColor=ColorNav;
self.navigationController.navigationBar.translucent=FALSE;
self.title = titletext;
[[[self navigationController] navigationBar]setTitleTextAttributes:@{NSForegroundColorAttributeName: textColor}];