美文网首页
swift 自定义navigationbar

swift 自定义navigationbar

作者: 剌小胥 | 来源:发表于2016-05-14 16:23 被阅读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
    }

    相关文章

      网友评论

          本文标题:swift 自定义navigationbar

          本文链接:https://www.haomeiwen.com/subject/joomrttx.html