导航栏titleView返回到vc后,按钮不起作用

时间:2018-11-16 04:26:48

标签: swift uinavigationbar

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    setupNavItems(navigationItem, day: "Today", address: "Madiwla Bangalore", type: 1, isMenuBtn: true, rightBtnType: 1, title: "")
}
func setupNavItems(_ navigationCustonItem : UINavigationItem,
                   day:String = "",address:String = "",type:Int,isMenuBtn:Bool,rightBtnType:Int,title:String = "") {
    self.navigationController?.isNavigationBarHidden = false
    navigationController?.navigationBar.backgroundColor = .green
    navigationController?.navigationBar.isTranslucent = false

    let height = navigationController?.navigationBar.frame.height ?? 0
    let topButtonsWidth: CGFloat = 90 // left padding
    let width2 = 300 - topButtonsWidth
    let estimatedLeftViewWidth = min( width2/2  ,100)
    let rightSapacing : CGFloat = 5.0
    if type == 1 {
        let Titleview = UIView(frame: CGRect(x: 0, y: 0, width: width2, height: height))
        Titleview.backgroundColor = .clear
        navigationCustonItem.titleView = Titleview

        let leftView = UILabel()
        leftView.text = day
        leftView.textColor = .white
        leftView.textAlignment = .center
        leftView.backgroundColor = .clear
        Titleview.addSubview(leftView)
        leftView.translatesAutoresizingMaskIntoConstraints = false
        leftView.leadingAnchor.constraint(equalTo: Titleview.leadingAnchor, constant: rightSapacing).isActive = true
        leftView.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
        leftView.widthAnchor.constraint(equalToConstant: estimatedLeftViewWidth).isActive = true

        let dottedLine = UIView()
        Titleview.addSubview(dottedLine)
        dottedLine.translatesAutoresizingMaskIntoConstraints = false
        dottedLine.leadingAnchor.constraint(equalTo: leftView.leadingAnchor, constant: rightSapacing).isActive = true
        dottedLine.topAnchor.constraint(equalTo: leftView.bottomAnchor).isActive = true
        dottedLine.widthAnchor.constraint(equalTo: leftView.widthAnchor).isActive = true
        dottedLine.heightAnchor.constraint(equalToConstant: 2).isActive = true

        let DirectImag = UIImageView()
        DirectImag.contentMode = .scaleAspectFit

        Titleview.addSubview(DirectImag)
        DirectImag.translatesAutoresizingMaskIntoConstraints = false
        DirectImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
        DirectImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
        DirectImag.leadingAnchor.constraint(equalTo: leftView.trailingAnchor, constant:0).isActive = true
        DirectImag.centerYAnchor.constraint(equalTo: leftView.centerYAnchor).isActive = true



        let dayBtn = UIButton()
        dayBtn.addTarget(self, action: #selector(self.btnClickASAPType), for: .touchUpInside)
        dayBtn.backgroundColor = .clear
        Titleview.addSubview(dayBtn)
        dayBtn.translatesAutoresizingMaskIntoConstraints = false
        dayBtn.leadingAnchor.constraint(equalTo: Titleview.leadingAnchor, constant: rightSapacing).isActive = true
        dayBtn.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
        dayBtn.widthAnchor.constraint(equalToConstant: estimatedLeftViewWidth).isActive = true
        dayBtn.heightAnchor.constraint(equalToConstant: height).isActive = true


        let rightView = UIView()
        rightView.backgroundColor = .clear
        Titleview.addSubview(rightView)
        rightView.translatesAutoresizingMaskIntoConstraints = false
        rightView.centerYAnchor.constraint(equalTo: Titleview.centerYAnchor).isActive = true
        rightView.leadingAnchor.constraint(equalTo: DirectImag.trailingAnchor, constant: 5).isActive = true
        rightView.heightAnchor.constraint(equalToConstant: height).isActive = true
        rightView.trailingAnchor.constraint(equalTo: Titleview.trailingAnchor).isActive = true



        let downImag = UIImageView()
        downImag.contentMode = .scaleAspectFit
        rightView.addSubview(downImag)
        downImag.translatesAutoresizingMaskIntoConstraints = false
        downImag.widthAnchor.constraint(equalToConstant: 15).isActive = true
        downImag.heightAnchor.constraint(equalToConstant: 15).isActive = true
        downImag.trailingAnchor.constraint(equalTo: rightView.trailingAnchor, constant:0).isActive = true
        downImag.centerYAnchor.constraint(equalTo: rightView.centerYAnchor).isActive = true


        let addLbl = UILabel()
        addLbl.text = address
        addLbl.textColor = .white
        addLbl.textAlignment = .center
        rightView.addSubview(addLbl)
        addLbl.translatesAutoresizingMaskIntoConstraints = false
        addLbl.leadingAnchor.constraint(equalTo: rightView.leadingAnchor).isActive = true
        //addLbl.heightAnchor.constraint(equalToConstant: height).isActive = true
        addLbl.centerYAnchor.constraint(equalTo: rightView.centerYAnchor).isActive = true
        addLbl.trailingAnchor.constraint(equalTo: downImag.leadingAnchor, constant: 0).isActive = true
        let addressBtn = UIButton(type: .system)
        addressBtn.backgroundColor = .clear
        addressBtn.addTarget(self, action: #selector(self.btnClickAddress), for: .touchUpInside)
        rightView.addSubview(addressBtn)
        addressBtn.translatesAutoresizingMaskIntoConstraints = false
        addressBtn.centerYAnchor.constraint(equalTo: rightView.centerYAnchor).isActive = true
        addressBtn.centerXAnchor.constraint(equalTo: rightView.centerXAnchor, constant: 0).isActive = true
        addressBtn.heightAnchor.constraint(equalToConstant: height).isActive = true
        addressBtn.widthAnchor.constraint(equalToConstant: width2 - estimatedLeftViewWidth).isActive = true
        // addressBtn.backgroundColor = .black


        let dottedAddLine = UIView()
        Titleview.addSubview(dottedAddLine)
        dottedAddLine.translatesAutoresizingMaskIntoConstraints = false
        dottedAddLine.leadingAnchor.constraint(equalTo: addLbl.leadingAnchor, constant: rightSapacing).isActive = true
        dottedAddLine.topAnchor.constraint(equalTo: addLbl.bottomAnchor).isActive = true
        dottedAddLine.widthAnchor.constraint(equalTo: addLbl.widthAnchor).isActive = true
        dottedAddLine.heightAnchor.constraint(equalToConstant: 2).isActive = true

    }
    else if type == 2{
        let dayLbl = UILabel()
        dayLbl.text = title
        dayLbl.textColor = .white
        dayLbl.textAlignment = .center
        navigationItem.title = title
    }else if type == 3 {
        let Titleview = UIView()
        Titleview.backgroundColor = .clear
        navigationItem.titleView = Titleview
        let textFieldView = UITextField()
        textFieldView.attributedPlaceholder = NSAttributedString(string: "Search in Pizza Max", attributes: [NSAttributedString.Key.foregroundColor : UIColor.white])
        Titleview.addSubview(textFieldView)
        textFieldView.translatesAutoresizingMaskIntoConstraints = false
        textFieldView.leadingAnchor.constraint(equalTo: Titleview.leadingAnchor).isActive = true
        textFieldView.trailingAnchor.constraint(equalTo: Titleview.trailingAnchor).isActive = true
        textFieldView.topAnchor.constraint(equalTo: Titleview.topAnchor).isActive = true
        textFieldView.bottomAnchor.constraint(equalTo: Titleview.bottomAnchor,constant: -5).isActive = true
        textFieldView.heightAnchor.constraint(equalToConstant: height - 5).isActive = true
        textFieldView.widthAnchor.constraint(equalToConstant: width2 - 20).isActive = true
     ///   textFieldView.addTarget(self, action: #selector(self.textChanged(_:)), for: .editingChanged)

        let viewBorder = UIView()
        viewBorder.backgroundColor = .white
        Titleview.addSubview(viewBorder)
        viewBorder.translatesAutoresizingMaskIntoConstraints = false
        viewBorder.leadingAnchor.constraint(equalTo: textFieldView.leadingAnchor).isActive = true
        viewBorder.trailingAnchor.constraint(equalTo: textFieldView.trailingAnchor).isActive = true
        viewBorder.topAnchor.constraint(equalTo: textFieldView.bottomAnchor,constant: 0).isActive = true
        viewBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true
        viewBorder.widthAnchor.constraint(equalTo: textFieldView.widthAnchor).isActive = true

    }

    reightMenuItems(isMenuBtn, navigationCustonItem)
    leftMenuItems(rightBtnType, navigationCustonItem)

}

fileprivate func reightMenuItems(_ isMenuBtn: Bool, _ navigationItem: UINavigationItem) {
    //reigtButtons
    if isMenuBtn {
        let leftView = UIView()
        leftView.backgroundColor = .clear
        leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
        let menuImage = UIImageView()
        menuImage.contentMode = .scaleAspectFit
        menuImage.frame = CGRect(x: 4, y: 8, width: 32, height: 28)
        leftView.addSubview(menuImage)



        let followButton = UIButton(type: .system)
        followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)
        followButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: -20, bottom: 0, right: 0)
        followButton.addTarget(self, action: #selector(self.btnClickMenu), for: .touchUpInside)

        leftView.addSubview(followButton)
        navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
    }else{
        let leftView = UIView()
        leftView.backgroundColor = .clear
        leftView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)

        let menuImage = UIImageView()
        menuImage.contentMode = .scaleToFill
        menuImage.frame = CGRect(x: 5, y: 10.5, width: 15, height: 25)
        leftView.addSubview(menuImage)

        let followButton = UIButton(type: .system)
        followButton.frame = CGRect(x: 0, y: 0, width: 40, height: 44)

        leftView.addSubview(followButton)
        followButton.addTarget(self, action: #selector(self.btnClickBack), for: .touchUpInside)
        navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: leftView)]
    }
}

fileprivate func leftMenuItems(_ rightBtnType: Int, _ navigationItem: UINavigationItem) {
    //lecftButtons
    switch rightBtnType {
    case 1:
        let reightView = UIView()
        reightView.backgroundColor = .clear
        reightView.frame = CGRect(x: 0, y: 0, width: 40, height: 44)

        let menuImage = UIImageView()
        menuImage.contentMode = .scaleToFill
        menuImage.frame = CGRect(x: 4.5, y: 7, width: 31, height: 30)
        reightView.addSubview(menuImage)

        let cartBtn = UIButton(type: .system)
        cartBtn.frame = CGRect(x: 0, y: 0, width: 40, height: 44)

        reightView.addSubview(cartBtn)
        cartBtn.addTarget(self, action: #selector(self.btnClickCart), for: .touchUpInside)
        navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: reightView)]
    case 2:
        let composeButton = UIButton()
        composeButton.setTitle("Done", for: .normal)
        composeButton.titleLabel?.textColor = .white
        composeButton.frame = CGRect(x: 0, y: 00, width: 34, height: 44)
        composeButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 0)
        composeButton.addTarget(self, action: #selector(self.btnclickDone), for: .touchUpInside)
        navigationItem.rightBarButtonItems = [UIBarButtonItem(customView: composeButton)]
    default:
        break
    }
}

@IBAction  func btnClickASAPType(_ sender : UIButton)  {
    print("DayExt")
    // self.overriderFunc()
}

@objc func btnClickAddress()  {
    print("AddressExt")
}

@objc func handleAddressTap(_ sender: UITapGestureRecognizer) {
    print("Hello World")
}

@objc  public func btnClickCart()  {
    print("CartExt")
  self.performSegue(withIdentifier: "q", sender: self)
}

@objc func btnClickMenu()  {
    print("MenuExt")
    self.performSegue(withIdentifier: "q", sender: self)
}

@objc func btnClickBack()  {
    print("backExt")

}

@objc func btnclickDone(_ sender:UIButton){

}
   }

我正在以编程方式创建导航栏。第一次加载屏幕时,标题视图中添加的标题按钮正常工作。当执行推入或演示然后关闭或弹出操作时,回到上一屏幕,这些按钮不起作用。但是左右导航按钮均正常工作。

0 个答案:

没有答案
相关问题