Swift 导航栏设置

 

    let width = UIScreen.mainScreen().bounds.size.width

    let height = UIScreen.mainScreen().bounds.size.height

 

    override func viewDidLoad() {

        super.viewDidLoad()

        self.title = "这是标题"

        self.view.backgroundColor = UIColor.yellowColor()

        

        //自定义标题视图

//        let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 200, height: 44))

//        titleLabel.textAlignment = NSTextAlignment.Center

//        titleLabel.backgroundColor = UIColor.grayColor()

//        titleLabel.font = UIFont.boldSystemFontOfSize(25)

//        titleLabel.textColor = UIColor.redColor()

//        titleLabel.text = "hehe"

//        self.navigationItem.titleView = titleLabel

        let button = UIButton(type: UIButtonType.System)

        button.setTitle("按钮", forState: UIControlState.Normal)

        button.addTarget(self, action: "titleViewBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)

        button.frame = CGRect(x: 0, y: 0, width: 100, height: 30)

        self.navigationItem.titleView = button

        

        /*----------------------UINavigationItem两侧按钮----------------*/

        //第一种方式

        let leftItem1 = UIBarButtonItem(title: "anniu", style: UIBarButtonItemStyle.Done, target: self, action: "leftAnniuClick")

//        self.navigationItem.leftBarButtonItem = leftItem1

        

        //第一种方式

        let leftItem2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Bookmarks, target: self, action: "btnClick:")

//        self.navigationItem.leftBarButtonItem = leftItem2

        self.navigationItem.leftBarButtonItems = [leftItem1, leftItem2]

        

        //第三种方式(自定义按钮)

        let rightButton = UIButton(type: UIButtonType.Custom)

        rightButton.frame = CGRect(x: 0, y: 0, width: 33, height: 32)

        rightButton.addTarget(self, action: "rightBtnClick:", forControlEvents: UIControlEvents.TouchUpInside)

        rightButton.setImage(UIImage(named: "btn_search.png"), forState: UIControlState.Normal)

        let rightItem = UIBarButtonItem(customView: rightButton)

        self.navigationItem.rightBarButtonItem = rightItem

 

        /*----------------UINavigationBar导航栏背景设置------------------*/

 

        //隐藏导航栏

//        self.navigationController?.navigationBarHidden = true

        //设置导航栏背景风格

        self.navigationController?.navigationBar.barStyle = UIBarStyle.Default

        //设置导航栏的半透明效果,默认是yes

//        self.navigationController?.navigationBar.translucent = false

        //设置导航栏的背景颜色(必须有半透明效果)

//        self.navigationController?.navigationBar.backgroundColor = UIColor.redColor()

        //将导航上所有的Label的字体变色

        self.navigationController?.navigationBar.tintColor = UIColor.redColor()

        //设置图片作为导航栏的背景,设置了背景图片,导航栏就不透明

        var img = UIImage(named: "navbar_bg_normal.png")

        UIGraphicsBeginImageContext(CGSizeMake(width, 64))

        img?.drawInRect(CGRect(x: 0, y: 0, width: width, height: 64))

        img = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()

        self.navigationController?.navigationBar.setBackgroundImage(img, forBarMetrics: UIBarMetrics.Default)

        

        //设置提示文本,通常用不上

//        self.navigationItem.prompt = "正在保存";

        

    }

    

    func titleViewBtnClick(btn: UIButton) {

        print("标题被点击了")

    }

    

    func leftAnniuClick() {

        print("anniu被点击了")

    }

    

    func btnClick(btn: UIButton) {

        print(btn)

    }

    

    func rightBtnClick(btn: UIButton) {

        print("rightBtnClick")

    }

 

转载于:https://www.cnblogs.com/ZGSmile/p/5695357.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值