如何替换/自定义情节提要导航控件中的后退按钮图像 [英] how to replace/customize back button image in storyboard navigationcontroller

查看:91
本文介绍了如何替换/自定义情节提要导航控件中的后退按钮图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用自定义图像替换后退按钮中的文本.如何用快速代码做到这一点?我不想替换整个backbarbutton,因为我想保留返回到最后一个视图的默认操作行为.我还想根据返回目标位置(基于storyboardId)在哪里做一个switch语句,以便可以基于视图显示不同的图像.

I want to replace the text in the back button with a custom image. How can I do that in swift code? I don't want to replace the entire backbarbutton since I'd like to keep the default action behaviour of going back to the last view. I would also like to do a switch statement based on where the back destination(based on storyboardId) is so that I can show different images based on the view.

这会替换图像,但会抹掉默认的后退按钮行为,我需要区分后退目标是什么,以便我可以显示正确的后退图像.

this replaces the image but it wipes out the default back button behavior, and I need to discrimante on what the back destination is so that I can show the right back image.

self.navigationItem.leftBarButtonItem = 
 UIBarButtonItem(image:StyleKit.imageOfMap, style:.Plain, target:self, action:nil);

推荐答案

尝试更改您的leftBarButtonItem:

self.navigationItem.leftBarButtonItem = 
    UIBarButtonItem(image:StyleKit.imageOfMap, style:.Plain, target:self, action:nil);

backBarButtonItem:

self.navigationItem.backBarButtonItem = 
    UIBarButtonItem(image:StyleKit.imageOfMap, style:.Plain, target:self, action:nil);

,以便利用backBarButtonItem的默认操作.

in order to take advantage of backBarButtonItem's default action.

并将这行代码放在您希望自定义后退按钮出现的视图控制器的之前.

And put that line of code in the view controller preceding the one you'd like your custom back button to appear in.

编辑:如果您不希望使用<"符号出现在按钮上,实际上您必须使用leftBarButtonItem,然后使用单独的方法关闭视图控制器,例如:

Edit: If you don't want the "<" symbol to appear on your button, you'll have to in fact use a leftBarButtonItem then dismiss the view controller in a separate method, ex:

    self.navigationItem.leftBarButtonItem = 
        UIBarButtonItem(image:StyleKit.imageOfMap, style:.Plain, target:self, action:"backButtonPressed:");

}

func backButtonPressed(sender:UIButton) {
    navigationController?.popViewControllerAnimated(true)
}

这篇关于如何替换/自定义情节提要导航控件中的后退按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆