微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swift - 修改导航栏“返回”按钮文字,图标

(本文代码升级至Swift3)

项目中常常会使用 UINavigationController 对各个页面进行导航,导航栏左侧的返回按钮标题文字是上级页面 title

但如果上级页面标题很长,那么这个返回按钮字很多就会很丑:


文字极其长时返回文字就会变成“back”:


一,要修改“返回按钮”的文字,有如下两种方式:

1,在父页面中设置
navigationItem.backBarButtonItem设为自定义UIBarButtonItem
这种方法所有的子界面返回时都变成了我们定义的文字,同时文字前面任然保留返回箭头。
1
2
let item = UIBarButtonItem (title: "返回" ,style: .plain,target: self ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,action: nil )
.navigationItem.backBarButtonItem = item
或者也可以直接把文字设为空字符串,这样就只有一个箭头了。
"".navigationItem.backBarButtonItem = item

2,在子页面中设置
navigationItem.leftBarButtonItem自定义UIBarButtonItem
这种方式可以给各个子页面返回按钮单独设置不同的文字,但文字前面是没有小箭头的。
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import UIKit
class DetailViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
leftBarBtn = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action: #selector(backToPrevIoUs))
.navigationItem.leftBarButtonItem = leftBarBtn
}
//返回按钮点击响应
backToPrevIoUs(){
.navigationController!.popViewController(animated: true )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

二,修改“返回按钮”图标
从上面最后一个例子可以看到,在子页面修改返回按钮的话只有文字没有图片。如果想要使用自定义图片,或者图片文字都需要的话可以进行如下操作:
1,如果只需要图片,不需要文字
比如我们想要用左侧这个图片(back@2x.png)作为返回图标
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import UIKit
class DetailViewController : UIViewController {
override func viewDidLoad() {
super .viewDidLoad()
let leftBarBtn = UIBarButtonItem (title: "" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,target: self ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action: #selector(backToPrevIoUs))
leftBarBtn.image = UIImage (named: "back" )
//用于消除左边空隙,要不然按钮顶不到最前面
spacer = (barButtonSystemItem: .fixedspace,153)!important; background:none!important">nil ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action: )
spacer.width = -10;
.navigationItem.leftBarButtonItems = [spacer,leftBarBtn]
}
//返回按钮点击响应
backToPrevIoUs(){
.navigationController!.popViewController(animated: true )
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

2,既需要图片也需要文字

这个时候就要通过创建 UIButton来实现了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import UIKit
class DetailViewController : UIViewController {
override func viewDidLoad() {
let button = UIButton (type: .system)
button.frame = CGRect (x:0,y:0,width:65,height:30)
button.setimage( UIImage (named: "back" ), for : .normal)
button.setTitle( "返回" : .normal)
button.addTarget( self : .touchUpInside)
leftBarBtn = UIBarButtonItem (customView: button)
//用于消除左边空隙,要不然按钮顶不到最前面
spacer = ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,target: nil ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
action: )
spacer.width = -10;
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:0px 1em!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; min-height:auto!important; background:none rgb(249,leftBarBtn]
}
//返回按钮点击响应
backToPrevIoUs(){
.navigationController!.popViewController(animated: true )
}
didReceiveMemoryWarning() {
super .didReceiveMemoryWarning()
}
}

三,自定义leftBarButtonItems导致左边缘滑动返回失效问题解决
通常使用 navigationController 跳转到另一页面时,除了可以点击左上角的返回按钮,还可以通过在屏幕左侧向右滑动来返回到上一层。但如果像上面样例一样,自定义 self.navigationItem.leftBarButtonItems 后会发现,滑动返回( swipe back )失效了。
解决办法: Swift - 自定义导航栏leftBarButtonItems导致滑动返回失效问题解决

原文出自: www.hangge.com 转载请保留原文链接 http://www.hangge.com/blog/cache/detail_957.html

原文地址:https://www.jb51.cc/swift/321577.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐