问题描述
我想用自定义图像替换后退按钮中的文本.我怎样才能在快速代码中做到这一点?我不想替换整个后退按钮,因为我想保留返回到最后一个视图的默认操作行为.我还想根据返回目的地(基于 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
的默认动作.
然后将那行代码放在视图控制器中在您希望自定义后退按钮出现的那个之前.
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)
}
这篇关于如何在情节提要导航控制器中替换/自定义后退按钮图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!