swift UINavigationBar 导航条

importUIKit

classViewController:UIViewController{

varcount = 0

//声明导航条

varnavigationBar:UINavigationBar?

overridefuncviewDidLoad() {

super.viewDidLoad()

// Do any additional setup after loading the view, typically from a nib.

//实例化导航条

navigationBar =UINavigationBar(frame:CGRectMake(0, 20, 320, 44))

self.view.addSubview(navigationBar!)

onAdd()

}

overridefuncdidReceiveMemoryWarning() {

super.didReceiveMemoryWarning()

// Dispose of any resources that can be recreated.

}

//增加导航项函数

funconAdd(){

count++

//给导航条增加导航项

navigationBar?.pushNavigationItem(onMakeNavitem(), animated:true)

}

//删除导航项函数

funconRemove(){

ifcount > 1{

//减少导航项数量

count--

//从导航条中移除最后一个导航项

navigationBar?.popNavigationItemAnimated(true)

}

}

//创建一个导航项

funconMakeNavitem()->UINavigationItem{

varnavigationItem =UINavigationItem()

//创建左边按钮

varleftBtn =UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Add,

target:self, action:"onAdd")

//创建右边按钮

varrightBtn =UIBarButtonItem(barButtonSystemItem:UIBarButtonSystemItem.Cancel,

target:self, action:"onRemove")

//设置导航栏标题

navigationItem.title ="第\(count)个导航项"

//设置导航项左边的按钮

navigationItem.setLeftBarButtonItem(leftBtn, animated:true)

//设置导航项右边的按钮

navigationItem.setRightBarButtonItem(rightBtn, animated:true)

returnnavigationItem

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容