当文字极其长时返回文字就会变成“back”:
一,要修改“返回按钮”的文字,有如下两种方式:
1,在父页面中设置
将 navigationItem.backBarButtonItem设为自定义的 UIBarButtonItem
这种方法所有的子界面返回时都变成了我们定义的文字,同时文字前面任然保留返回箭头。
或者也可以直接把文字设为空字符串,这样就只有一个箭头了。
""
2,在子页面中设置
将 navigationItem.leftBarButtonItem为自定义的 UIBarButtonItem
这种方式可以给各个子页面返回按钮单独设置不同的文字,但文字前面是没有小箭头的。
2
一,要修改“返回按钮”的文字,有如下两种方式:
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
|
2,在子页面中设置
将 navigationItem.leftBarButtonItem为自定义的 UIBarButtonItem
这种方式可以给各个子页面返回按钮单独设置不同的文字,但文字前面是没有小箭头的。
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()
}
}
|
从上面最后一个例子可以看到,在子页面修改返回按钮的话只有文字没有图片。如果想要使用自定义图片,或者图片文字都需要的话可以进行如下操作:
比如我们想要用左侧这个图片(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() {
button.frame =
CGRect
(x:0,y:0,width:65,height:30)
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()
}
}
|