swift 自定义navigationbar

2016-05-14  本文已影响2959人  剌小胥

1.定义topview
@IBOutlet weak var topBarView: UIView!

2.设置topbarview 背景色
self.topBarView.backgroundColor = NAVBARCOLOR(全局uicolor自己设置)

3.navigationBar参数设置

navigationBar = UINavigationBar(frame: CGRectMake(0, 20, SCREEN_WIDTH, 44))
navigationBar?.setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)
navigationBar?.shadowImage = UIImage()
navigationBar?.translucent = true
self.topBarView.addSubview(navigationBar!)

4.设置navbar上的按钮控件

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

// 给顶部导航增加导航项

func onMakeNavitem() -> UINavigationItem {
    let navigationItem = UINavigationItem()
    let rightBtn = UIBarButtonItem(title: "保存", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(EditViewController.savePost))
    let leftBtn = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(EditViewController.cancelPost))
    
    // 设置导航栏标题
    navigationItem.title = "编辑"
    // left按钮
    navigationItem.setLeftBarButtonItem(leftBtn, animated: true)
    // right按钮
    navigationItem.setRightBarButtonItem(rightBtn, animated: true)
    
    return navigationItem
}
上一篇下一篇

猜你喜欢

热点阅读