swift: setting back button image in nav bar

2019-02-06 22:30发布

I'm trying to set the back button image in nav bar in my controller, here's my code in viewDidLoad():

        var backImg: UIImage? = UIImage(named: "back_btn.png")
    println(backImg)
    if var back_img = backImg  {
        println("GET IT")
        println(back_img)
        println(UIControlState.Normal)
        println(UIBarMetrics.Default)
    self.navigationController.navigationBar.backItem.backBarButtonItem.setBackButtonBackgroundImage(back_img, forState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
    }

I tried to put them to viewWillLoad, but getting the same error

Console with error message:

<UIImage: 0x7ff37bd85750>
GET IT
<UIImage: 0x7ff37bd85750>
VSC14UIControlState (has 1 child)
(Enum Value)
fatal error: unexpectedly found nil while unwrapping an Optional value

I don't know which part went wrong. Seems like the back_img is not nil, but I got error saying it's nil

Thanks!

7条回答
你好瞎i
2楼-- · 2019-02-06 22:42

Swift 4+ version

let backImage = UIImage(named: "back-icon").withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: 0, vertical: -80.0), for: .default)
查看更多
啃猪蹄的小仙女
3楼-- · 2019-02-06 22:51

alternative way: click on the navigation controller in storyboard (not the navigation controller in the UIViewController itself). Then, in the attributes section on the RHS you'll see back image and back mask. Set back image and you're done.

查看更多
狗以群分
4楼-- · 2019-02-06 22:52
    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back-icon")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back-icon")
查看更多
We Are One
5楼-- · 2019-02-06 23:00

If you want to change the back button in every controller you can add this to app delegate in didFinishLaunchingWithOptions

    let backImg: UIImage = UIImage(named: "back_button")!
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)
查看更多
Deceive 欺骗
6楼-- · 2019-02-06 23:00

//Here is the perfect solution To Set back button with Image and Action in default Navigation Bar

First add UIBarButton in Navigation bar

enter image description here

Then Go to property in File inspector in storyboard and add space to hide back button title text

Set image in Ui Bar button image

enter image description here

/Write on click action method/

To enable swipe to pop (back to previous controller), write two line code in ViewDidLoad method

enter image description here

And you will get perfect Back Button with Swipe to back animation

enter image description here

//Note:- To disable back button previous viewcontroller title , add one space in title text in back button in storyboard file inspector

查看更多
Root(大扎)
7楼-- · 2019-02-06 23:03

In Swift 3.0 + put below code in appdelegate didFinishLaunchingWithOptions method, it will work perfectly

let backImage = UIImage(named: "BackNavigation")?.withRenderingMode(.alwaysOriginal)
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -80.0), for: .default)

The last line will remove the title of Navigation Back Button if you don't want to remove title then just remove last line

查看更多
登录 后发表回答