最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift中设置导航栏中间的标题按钮

iOS crifan 1129浏览 0评论

之前已经通过:

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)

        SingletonMainVC().title = "我的客户"

        //navi items

        //1.left filter item

        let filterBarItem:UIBarButtonItem = UIBarButtonItem(image: UIImage(named:"customer_navi_filter"), style: UIBarButtonItemStyle.Plain, target: self, action: #selector(self.showFilterOptions(_:)))

        SingletonMainVC().navigationItem.setLeftBarButtonItem(filterBarItem, animated: false)

    }

去设置了,导航栏:

中间的标题

左边的bar item

效果是:

现在想要:

把中间的标题,换成可以点击的按钮,点击后,可以出现下拉选择菜单:

swift navigation bar middle

ios – Swift Navigation Bar Image Title – Stack Overflow

ios – Make the navigationbar title clickable swift – Stack Overflow

uinavigationbar – Swift Navigation Bar Button and Title don’t appear – Stack Overflow

可以设置titleView即可。

然后创建一个button

改为:

        //2. middle clickable button title

//        SingletonMainVC().title = "我的客户"

        //let myCustomerButtonBarView = UIView(frame: CGRectMake(0.0, 0.0, 60, 44.0))

        let myCustomerButtonBarView = UILabel(frame: CGRectMake(0.0, 0.0, 60, 44.0))

        myCustomerButtonBarView.text = "我的客户"

        myCustomerButtonBarView.textColor = UIColor.whiteColor()

        myCustomerButtonBarView.font = UIFont.systemFontOfSize(20)

        myCustomerButtonBarView.backgroundColor = UIColor.clearColor()

        SingletonMainVC().navigationItem.titleView = myCustomerButtonBarView

即可显示出,自定义的view作为中间的navi bar的title了。

然后再去自定义出可以点击的button

最终用代码:

    override func viewWillAppear(animated: Bool) {

        super.viewWillAppear(animated)

        let myCustomerButtonBarView = UIView(frame: CGRectMake(0.0, 0.0, 80, 44.0))

        SingletonMainVC().navigationItem.titleView = myCustomerButtonBarView

        let myCustomerButton:UIButton = TitleImageButton(title: "我的客户", titleFont: UIFont.systemFontOfSize(20), titleTextColor: UIColor.whiteColor(), titleWidth: 80, titlePaddingLeft: 0)

//        myCustomerButton.backgroundColor = UIColor.clearColor()

//        myCustomerButton.backgroundColor = UIColor.yellowColor()

        myCustomerButtonBarView.addSubview(myCustomerButton)

        constrain(myCustomerButton) {myCustomerButton in

            myCustomerButton.top == myCustomerButton.superview!.top

            myCustomerButton.left == myCustomerButton.superview!.left

            myCustomerButton.right == myCustomerButton.superview!.right

            myCustomerButton.bottom == myCustomerButton.superview!.bottom

        }

        myCustomerButton.addTarget(self, action: #selector(self.toggleSwitchCustomer(_:)), forControlEvents: UIControlEvents.TouchUpInside)

}

    func toggleSwitchCustomer(sender:UIButton) {

        gLog.verbose("sender=\(sender)")

    }

//

//  TitleImageButton.swift

//  SalesApp

//

//  Created by licrifan on 16/5/24.

//  Copyright © 2016年 licrifan. All rights reserved.

//

import UIKit

import Cartography

class TitleImageButton: UIButton {

    var isDownView:UIImageView

    var leftTitleLabel:UILabel

    init(title:String, titleFont:UIFont = UIFont.systemFontOfSize(18), titleTextColor:UIColor = UIColor.blackColor(), titleWidth:CGFloat, titlePaddingLeft:CGFloat = 20) {

        self.leftTitleLabel = UILabel()

        self.isDownView = UIImageView()

        super.init(frame: CGRectZero)

        self.backgroundColor = UIColor.clearColor()

        self.addSubview(self.leftTitleLabel)

        self.leftTitleLabel.text = title

        self.leftTitleLabel.textAlignment = .Center

//        self.leftTitleLabel.textAlignment = .Left

        self.leftTitleLabel.textColor = titleTextColor

        self.leftTitleLabel.font = titleFont

        constrain(leftTitleLabel) {leftTitleLabel in

            leftTitleLabel.centerY == leftTitleLabel.superview!.centerY

            leftTitleLabel.left == leftTitleLabel.superview!.left + titlePaddingLeft

            leftTitleLabel.width == titleWidth

        }

        //for debug

//        self.typeLabel.backgroundColor = UIColor.cyanColor()

        self.isDownView.image = UIImage(named: "down_indicator")!

        self.addSubview(self.isDownView)

        constrain(isDownView, leftTitleLabel) {isDownView, leftTitleLabel in

            isDownView.centerY == isDownView.superview!.centerY

            isDownView.left == leftTitleLabel.right + 2

            isDownView.width == self.isDownView.image!.size.width

            isDownView.height == self.isDownView.image!.size.height

        }

        //for debug

//        self.isDownView.backgroundColor = UIColor.yellowColor()

    }

    required init?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

}

效果:

转载请注明:在路上 » [已解决]swift中设置导航栏中间的标题按钮

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.110 seconds, using 18.81MB memory