华为荣耀v9适合不适合打王者

SWIFT,苹果于2014年WWDC(苹果开发者大会)发布的新开发语言,可与Objective-C*共同运行于Mac OS和iOS平台,用于搭建基于苹果平台的应用程序。
项目中常常会使用&UINavigationController&对各个页面进行导航,导航栏左侧的返回按钮默认标题文字是上级页面的title。
但如果上级页面的标题很长,那么这个返回按钮字很多就会很丑:
当文字极其长时返回文字就会变成&back&:&
一,要修改&返回按钮&的文字,有如下两种方式:
1,在父页面中设置
将navigationItem.backBarButtonItem设为自定义的UIBarButtonItem
这种方法所有的子界面返回时都变成了我们定义的文字,同时文字前面任然保留返回箭头。
let&item =&UIBarButtonItem(title:&&返回&, style: .Plain, target:&self, action:&nil)
self.navigationItem.backBarButtonItem =
或者也可以直接把文字设为空字符串,这样就只有一个箭头了。
let&item =&UIBarButtonItem(title:&&&, style: .Plain, target:&self, action:&nil)
self.navigationItem.backBarButtonItem =
2,在子页面中设置
将navigationItem.leftBarButtonItem为自定义的UIBarButtonItem
这种方式可以给各个子页面返回按钮单独设置不同的文字,但文字前面是没有小箭头的。
import&UIKit
class&DetailViewController:&UIViewController&{
&&&&override&func&viewDidLoad() {
&&&&&&&&super.viewDidLoad()
&&&&&&&&let&leftBarBtn =&UIBarButtonItem(title:&&返回&, style: .Plain, target:&self,
&&&&&&&&&&&&action:&&backToPrevious&)
&&&&&&&&self.navigationItem.leftBarButtonItem = leftBarBtn
&&&&//返回按钮点击响应
&&&&func&backToPrevious(){
&&&&&&&&self.navigationController?.popViewControllerAnimated(true)
&&&&override&func&didReceiveMemoryWarning() {
&&&&&&&&super.didReceiveMemoryWarning()
二,修改&返回按钮&图标&
从上面最后一个例子可以看到,在子页面修改返回按钮的话只有文字没有图片。如果想要使用自定义图片,或者图片文字都需要的话可以进行如下操作:
1,如果只需要图片,不需要文字
比如我们想要用左侧这个图片(back@2x.png)作为返回图标
& & && & & & &&
import&UIKit
class&DetailViewController:&UIViewController&{
&&&&override&func&viewDidLoad() {
&&&&&&&&super.viewDidLoad()
&&&&&&&&let&leftBarBtn =&UIBarButtonItem(title:&&&, style: .Plain, target:&self,
&&&&&&&&&&&&action:&&backToPrevious&)
&&&&&&&&leftBarBtn.image =&UIImage(named:&&back&)
&&&&&&&&//用于消除左边空隙,要不然按钮顶不到最前面
&&&&&&&&let&spacer =&UIBarButtonItem(barButtonSystemItem: .FixedSpace, target:&nil, action:&nil)
&&&&&&&&spacer.width = -10;
&&&&&&&&self.navigationItem.leftBarButtonItems = [spacer, leftBarBtn]
&&&&//返回按钮点击响应
&&&&func&backToPrevious(){
&&&&&&&&self.navigationController?.popViewControllerAnimated(true)
&&&&override&func&didReceiveMemoryWarning() {
&&&&&&&&super.didReceiveMemoryWarning()
2,既需要图片也需要文字
这个时候就要通过创建UIButton来实现了
import&UIKit
class&DetailViewController:&UIViewController&{
&&&&override&func&viewDidLoad() {
&&&&&&&&let&button =&&&UIButton(type: .System)
&&&&&&&&button.frame =&CGRectMake(0, 0, 65, 30)
&&&&&&&&button.setImage(UIImage(named:&back&), forState: .Normal)
&&&&&&&&button.setTitle(&返回&, forState: .Normal)
&&&&&&&&button.addTarget(self, action:&&backToPrevious&, forControlEvents: .TouchUpInside)
&&&&&&&&let&leftBarBtn =&UIBarButtonItem(customView: button)
&&&&&&&&//用于消除左边空隙,要不然按钮顶不到最前面
&&&&&&&&let&spacer =&UIBarButtonItem(barButtonSystemItem: .FixedSpace, target:&nil, action:&nil)
&&&&&&&&spacer.width = -10;
&&&&&&&&self.navigationItem.leftBarButtonItems = [spacer,leftBarBtn]
&&&&//返回按钮点击响应
&&&&func&backToPrevious(){
&&&&&&&&self.navigationController?.popViewControllerAnimated(true)
&&&&override&func&didReceiveMemoryWarning() {
&&&&&&&&super.didReceiveMemoryWarning()
Copyright &
All Rights Reserved &&&&&&

我要回帖

 

随机推荐