我如何自定义导航返回按钮在iOS 7及以上没有标题?(即只使用箭头)

self.navigationItem.leftBarButtonItem = self.editButtonItem;

我只是想知道它们是否有self。backbuttonitem;

OR

像这样的东西?

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]
                   initWithBarButtonSystemItem:UIBarButtonSystemItemBACK 
                   target:self action:@selector(back)];

当前回答

如果你有两个ViewController(FirstVC, seconddvc)嵌入在导航控制器中,你想在seconddvc中只有后退箭头。

你可以试试这个 在FirstVC的ViewDidLoad中

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}

然后当你推到seconddvc,你会看到只有后退箭头

其他回答

iOS6的简单破解也适用于iOS7:

[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];

编辑: 不要使用这种方法。详情见评论。

你不能以你想要的方式访问导航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];
}

我在viewDidLoad中应用了以下代码,它可以工作:

  // this will set the back button title
self.navigationController.navigationBar.topItem.title = @"Test";

 // this line set the back button and default icon color  

//[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];

this line change the back default icon to your custom icon
[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"menuicon"]]];

只是更新我使用矢量图标

改变backItem。title = "" to使用topItem。标题= "" 设置navigationItem。hidesBackButton = true & navigationItem。leftBarButtonItem将失去后退手势 记住,我们必须创建2个back映像的实例

我的解决方案将改变图像和保持后面的手势:

navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back")
navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back")
navigationController?.navigationBar.topItem?.title = ""

唯一对我有效的方法是:

navigationController?.navigationBar.backItem?.title = ""

更新:

当我改变segue动画标志为真(之前是假的),唯一适合我的方法是:

navigationController?.navigationBar.topItem?.title = ""