我如何自定义导航返回按钮在iOS 7及以上没有标题?(即只使用箭头)
self.navigationItem.leftBarButtonItem = self.editButtonItem;
我只是想知道它们是否有self。backbuttonitem;
OR
像这样的东西?
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBACK
target:self action:@selector(back)];
你不能以你想要的方式访问导航backButtonItem,你需要创建自己的返回按钮,如下所示:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 44.0f, 30.0f)];
[backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
}
当然了:
- (void) popVC{
[self.navigationController popViewControllerAnimated:YES];
}
虽然Kyle Begeman的回答完全做到了,但在每个视图控制器中都使用这种代码是相当烦人的。我最终得到了一个简单的UINavigationItem类别。小心,这里有龙!抱歉,我的意思是:
#import <objc/runtime.h>
@implementation UINavigationItem (ArrowBackButton)
static char kArrowBackButtonKey;
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Method m1 = class_getInstanceMethod(self, @selector(backBarButtonItem));
Method m2 = class_getInstanceMethod(self, @selector(arrowBackButton_backBarButtonItem));
method_exchangeImplementations(m1, m2);
});
}
- (UIBarButtonItem *)arrowBackButton_backBarButtonItem {
UIBarButtonItem *item = [self arrowBackButton_backBarButtonItem];
if (item) {
return item;
}
item = objc_getAssociatedObject(self, &kArrowBackButtonKey);
if (!item) {
item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:NULL];
objc_setAssociatedObject(self, &kArrowBackButtonKey, item, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return item;
}
@end
iOS7有新的界面规则,所以当你推送一个UIView时,最好至少保留后退箭头。
通过编程方式更改“返回”文本非常容易。只需要在推送视图之前添加这段代码(如果你使用的是StoryBoards,可以使用prepareForSegue):
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"NEW TITLE" style:UIBarButtonItemStylePlain target:nil action:nil];
}
这将改变默认的“后退”文本,但将保持iOS7风格的后退箭头。
你也可以在推送视图之前改变后退箭头的颜色:
- (void)viewDidLoad{
//NavBar background color:
self.navigationController.navigationBar.barTintColor=[UIColor redColor];
//NavBar tint color for elements:
self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
}
希望这对你有所帮助!
这就是我怎么做的,也是最简单,最有效,最清晰的方法。
如果嵌入在导航控制器上,这是有效的
斯威夫特3
在viewDidLoad中,我把这个添加到视图控制器中,你想让后退按钮只是箭头。
if let topItem = self.navigationController?.navigationBar.topItem {
topItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
这个和@Kyle Begeman的回答的不同之处在于你在视图控制器上调用这个你想让后退按钮只是箭头,而不是在push stack视图控制器上。
斯威夫特4
对于那些希望创建自定义后退按钮以及删除它们的标题的人,请在视图控制器中使用下面的一段代码来推送新的按钮:
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "close")
self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "close")
self.navigationItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
为了更普遍的使用,请执行以下操作:
Create a universal function as follows:
func addCustomizedBackBtn(navigationController: UINavigationController?, navigationItem: UINavigationItem?) {
navigationController?.navigationBar.backIndicatorImage = UIImage(named: "close")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "close")
navigationItem?.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
Then use it in the view controllers as follows:
addCustomizedBackBtn(navigationController: self.navigationController, navigationItem: self.navigationItem)