我想在我的backButton
中使用navigationController
的图片。我只想要使用的图像,根本没有显示“后退”文字,所以我做了:
UIImage *backButtonImage = [[UIImage imageNamed:@"ZSSBackButton.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 25, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
这很好用,但似乎有副作用使长navigationItem
个标题看起来偏离中心,即使它真的只是留下白色空间的背景文本应该是:
有没有办法可以让标题更左边?
答案 0 :(得分:0)
您可以使用 titleView 属性管理UINavigationbar标题的位置。
UIView *backView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];// Here you can set View width and height as per your requirement for displaying titleLbl position in navigationbar
[backView setBackgroundColor:[UIColor clearColor]];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
[backView addSubview:titleLbl];
titleLbl.text = @"YOUR TITLE";
titleLbl.textAlignment = NSTextAlignmentCenter;
self.navigationItem.titleView = backView;
编辑:您可以创建自定义按钮并将其指定为左按钮:
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
btnBack.frame = CGRectMake(0,0,51,31);
[btnBack setBackgroundImage:[UIImage imageNamed:@"ZSSBackButton"] forState:UIControlStateNormal];
[btnBack addTarget:self action:@selector(btnBackProfile_TouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
[self.navigationItem setLeftBarButtonItem:leftBtn];
希望它对你有所帮助。