我如何自定义导航返回按钮在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];
}
编辑:2014-04-09:当我获得声誉时,我感到抱歉,因为我不再使用这个技巧了。我推荐凯尔的答案。还要注意self。navigationitem。backbarbuttonitem的self不是返回按钮显示的视图控制器,而是要返回的前一个视图控制器。
如果之前的视图控制器不需要标题文本,只需用空白字符串填充标题;
self.navigationItem.title = @"";
[self.navigationController pushViewController:viewController animated:YES];
这将防止在被推送的视图控制器上显示带雪佛龙的“返回”。
编辑:即使你使用非空白标题文本,在viewWillAppear中设置前一个视图控制器的标题:除了标题可以闪烁在闪烁时,视图控制器弹出。我认为“推特应用程序”似乎做了更微妙的hack来避免闪烁。
其实很简单,我是这么做的:
Objective - C
// Set this in every view controller so that the back button displays back instead of the root view controller name
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
斯威夫特2
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
斯威夫特3
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
把这一行放到推送到堆栈(前一个视图控制器)的视图控制器中。新按下的视图控制器返回按钮现在将显示你为initWithTitle所放的任何东西,在本例中是一个空字符串。
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];
}
希望这对你有所帮助!
在你的第一个ViewController的prepareForSegue:方法中,你设置视图标题为@"",所以当下一个视图被推送时,它将显示前一个ViewController标题为@""。
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
self.navigationItem.title = @" ";
}
唯一的问题是,当你点击返回按钮时,你之前的视图将没有标题,所以你可以在viewWillAppear上再次添加它:
-(void)viewWillAppear:(BOOL)animated{
self.navigationItem.title = @"First View Title";
}
我不太喜欢这个解决方案,但它是可行的,我没有找到其他方法来做到这一点。
我从iOS 5开始就一直在使用这个解决方案,没有任何问题。我在视图控制器中调用了一个实用函数。你需要在viewDidLoad或之后的任何点中做。
void updateBackButtonTextForViewController(UIViewController *viewController, NSString *text)
{
if(! viewController.navigationItem.backBarButtonItem)
{
viewController.navigationItem.backBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:text
style:UIBarButtonItemStylePlain
target:nil action:nil];
}
else
{
viewController.navigationItem.backBarButtonItem.title = text;
}
}
在某些情况下,导航项可能已经存在,在其他情况下需要创建它。这在不影响导航项标题的情况下解释了这两种情况。它允许你通过简单地传入@""来删除标题。
目标:
自定义UINavigationBar上的所有后退按钮为白色图标
步骤:
1. 在AppDelete的didFinishLaunchingWithOptions方法中:
UIImage *backBtnIcon = [UIImage imageNamed:@"navBackBtn"];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
[UINavigationBar appearance].tintColor = [UIColor whiteColor];
[UINavigationBar appearance].backIndicatorImage = backBtnIcon;
[UINavigationBar appearance].backIndicatorTransitionMaskImage = backBtnIcon;
}else{
UIImage *backButtonImage = [backBtnIcon resizableImageWithCapInsets:UIEdgeInsetsMake(0, backBtnIcon.size.width - 1, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:backButtonImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -backButtonImage.size.height*2) forBarMetrics:UIBarMetricsDefault];
}
2.在普通超级ViewController类的viewDidLoad方法中:
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[self.navigationItem setBackBarButtonItem:backItem];
}else{
//do nothing
}
虽然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
只用一张图片!
OBJ - c:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Icon-Back"]
style:UIBarButtonItemStylePlain
target:self.navigationController
action:@selector(popViewControllerAnimated:)];
self.navigationItem.leftBarButtonItem = backButton;
}
斯威夫特4:
let backBTN = UIBarButtonItem(image: UIImage(named: "Back"),
style: .plain,
target: navigationController,
action: #selector(UINavigationController.popViewController(animated:)))
navigationItem.leftBarButtonItem = backBTN
navigationController?.interactivePopGestureRecognizer?.delegate = self
Icon-Back.png
Icon-Back@2x.png
Icon-Back@3x.png
所有的答案都不能解决问题。在每个视图控制器中设置后退按钮标题,并且添加偏移量仍然会使下一个视图控制器标题向右移动,这是不可接受的。
这里是使用方法swizzling的方法,只需创建新的扩展UINavigationItem
import UIKit
extension UINavigationItem {
public override class func initialize() {
struct Static {
static var token: dispatch_once_t = 0
}
// make sure this isn't a subclass
if self !== UINavigationItem.self {
return
}
dispatch_once(&Static.token) {
let originalSelector = Selector("backBarButtonItem")
let swizzledSelector = #selector(UINavigationItem.noTitleBackBarButtonItem)
let originalMethod = class_getInstanceMethod(self, originalSelector)
let swizzledMethod = class_getInstanceMethod(self, swizzledSelector)
let didAddMethod = class_addMethod(self, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(self, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
// MARK: - Method Swizzling
struct AssociatedKeys {
static var ArrowBackButtonKey = "noTitleArrowBackButtonKey"
}
func noTitleBackBarButtonItem() -> UIBarButtonItem? {
if let item = self.noTitleBackBarButtonItem() {
return item
}
if let item = objc_getAssociatedObject(self, &AssociatedKeys.ArrowBackButtonKey) as? UIBarButtonItem {
return item
} else {
let newItem = UIBarButtonItem(title: " ", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
objc_setAssociatedObject(self, &AssociatedKeys.ArrowBackButtonKey, newItem as UIBarButtonItem?, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return newItem
}
}
}
如果你有两个ViewController(FirstVC, seconddvc)嵌入在导航控制器中,你想在seconddvc中只有后退箭头。
你可以试试这个
在FirstVC的ViewDidLoad中
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}
然后当你推到seconddvc,你会看到只有后退箭头
如果你设置了导航栏的tintColor,添加一个没有标题的自定义后退按钮图像,该图像的tintColor将反映图像的颜色。请点击苹果文档链接。
https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/UIKitUICatalog/index.html#//apple_ref/doc/uid/TP40012857-UIView-SW7
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navBar.tintColor = self.tintColor;
UIImage *myImage = [UIImage imageNamed:@"left_arrow.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:myImage style:UIBarButtonItemStylePlain target:self action:@selector(cancelButtonFunction:)];
navItem.leftBarButtonItem = leftButton;
navBar.items = @[ navItem ];
这就是我怎么做的,也是最简单,最有效,最清晰的方法。
如果嵌入在导航控制器上,这是有效的
斯威夫特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)