UINavigation设置后退按钮图像而不是<back text =“”swift =“”2.0 =“”

时间:2015-12-29 12:41:31

标签: ios swift uinavigationcontroller uiimage

=“”

我正在尝试更改UINavigationController内的后退按钮。这是我到目前为止尝试的和输出:

  

1

ViewDidLoad

内的代码
self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "navBarBackButton")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "navBarBackButton")
    self.navigationItem.backBarButtonItem?.image = UIImage(named: "navBarBackButton")

输出:

Nav Back Button

我尝试添加以下内容,但ID没有帮助:

self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.Plain, target:nil, action:nil)
  

2

AppDelegate

内的代码
let backImg: UIImage = UIImage(named: "navBarBackButton")!
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

输出:

Nav Back Button

  

3

我尝试在Storyboard中添加后退按钮,但没有改变

Nav Back Button

  

4 - 感谢sanandiya vipul。仍然需要它而不是&#39; Back&#39;。这只会删除“后退”,但会将图像保持在右下方而不是整个“后退”。

UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), forBarMetrics: .Default)

这是输出:

Back Button

  

这是我希望实现的结果

Back Button

4 个答案:

答案 0 :(得分:5)

  1. 在Code下面添加ViewDidLoad方法。它的工作很好。
  2. Swift

      

    UIBarButtonItem.appearance()。setBackButtonTitlePositionAdjustment(UIOffsetMake(0,-80.0),forBarMetrics:.Default)

    Objective-c

      

    [[UIBarButtonItem外观] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, - 80.f)forBarMetrics:UIBarMetricsDefault];

答案 1 :(得分:1)

Swift 4版本

将标题文字颜色设置为清除

let barButtonItemAppearance = UIBarButtonItem.appearance()        
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)                
barButtonItemAppearance.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .highlighted)

答案 2 :(得分:0)

Swift 3版本

  

UIBarButtonItem.appearance()。setBackButtonTitlePositionAdjustment(UIOffsetMake(0,   -80.0),用于:.default)

答案 3 :(得分:-1)

刚刚添加了UIBarButtonItem并将其与行动相关联。

    @IBAction func backTapped(sender: AnyObject) {
    self.navigationController?.popViewControllerAnimated(true)
}