ios swift:在导航栏中设置返回按钮图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24769770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:24:33  来源:igfitidea点击:

swift: setting back button image in nav bar

iosswiftuinavigationcontrolleruinavigationbaruinavigationitem

提问by fuiiii

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

我正在尝试在控制器的导航栏中设置后退按钮图像,这是我在 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

我试图把它们放到 viewWillLoad,但得到同样的错误

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

不知道是哪个环节出了问题。似乎 back_img 不是 nil,但我收到错误说它是 nil

Thanks!

谢谢!

回答by Shahid Aslam

In Swift 3.0 +put below code in appdelegatedidFinishLaunchingWithOptionsmethod, it will work perfectly

Swift 3.0 +将下面的代码放入appdelegatedidFinishLaunchingWithOptions方法中,它将完美运行

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

Or for Swift 4.0 +

或者Swift 4.0 +

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)

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

最后一行将删除标题,Navigation Back Button如果您不想删除标题,则只需删除最后一行

回答by Jaro

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

如果您想更改每个控制器中的后退按钮,您可以将其添加到 didFinishLaunchingWithOptions 中的应用程序委托中

    let backImg: UIImage = UIImage(named: "back_button")!
    UIBarButtonItem.appearance().setBackButtonBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

回答by fuiiii

I have figured out by looking into sample code. 1) Create a bar button item in storyboard. 2) Link that button to controller using IBOutlet 3) Add image to the button

我已经通过查看示例代码弄清楚了。1)在故事板中创建一个条形按钮项。2) 使用 IBOutlet 将该按钮链接到控制器 3) 将图像添加到按钮

 var backImg: UIImage = UIImage(named: "back_btn")
 backBtn.setBackgroundImage(backImg, forState: .Normal, barMetrics: .Default)

PS: image should be added to Images.xcassets folder, see sample code, UICatalog, for details.

PS:图片需要添加到Images.xcassets文件夹中,详见示例代码UICatalog

回答by Mantas Laurinavi?ius

    self.navigationController?.navigationBar.backIndicatorImage = UIImage(named: "back-icon")
    self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = UIImage(named: "back-icon")

回答by Mehul

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

//这是在默认导航栏中设置带有图像和操作的后退按钮的完美解决方案

First add UIBarButton in Navigation bar

首先在导航栏中添加 UIBarButton

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

在 Ui Bar 按钮图像中设置图像

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

要启用滑动弹出(回到上一个控制器),请在 ViewDidLoad 方法中编写两行代码

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

//注意:-要禁用后退按钮上一个视图控制器标题,请在故事板文件检查器的后退按钮的标题文本中添加一个空格

回答by zevij

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.

替代方法:单击情节提要中的导航控制器(而不是 UIViewController 本身中的导航控制器)。然后,在 RHS 的属性部分,您将看到背面图像和背面蒙版。设置回图像,你就完成了。

回答by Abhishek Jain

Swift 4+ version

斯威夫特 4+ 版本

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)