How to add custom view on right of navigation bar Swift?

前端 未结 6 1546
猫巷女王i
猫巷女王i 2020-12-16 21:59

I am trying to add a custom view on right of UINavigationBar. What I tried is the following, but the view is not showing up! Pleas

相关标签:
6条回答
  • 2020-12-16 22:13

    I assume your rigthtBarButtonItem is nil. Instead init a new UIBarButtonItem with your custom view and set this as the rightBarButtonItem. Give it a try

    0 讨论(0)
  • 2020-12-16 22:14

    // creating uiview and add three custom buttons

     func addRightButton(){
    
        let viewFN = UIView(frame: CGRectMake(0, 0, 180,40))
            viewFN.backgroundColor = UIColor.yellowColor()
        let button1 = UIButton(frame: CGRectMake(0,8, 40, 20))
        button1.setImage(UIImage(named: "notification"), forState: UIControlState.Normal)
        button1.setTitle("one", forState: .Normal)
    
        button1.addTarget(self, action: #selector(self.didTapOnRightButton), forControlEvents: UIControlEvents.TouchUpInside)
    
        let button2 = UIButton(frame: CGRectMake(40, 8, 60, 20))
        button2.setImage(UIImage(named: "notification"), forState: UIControlState.Normal)
        button2.setTitle("tow", forState: .Normal)
        let button3 = UIButton(frame: CGRectMake(80, 8, 60, 20))
         button3.setImage(UIImage(named: "notification"), forState: UIControlState.Normal)
        button3.setTitle("three", forState: .Normal)
    
        button3.addTarget(self, action: #selector(self.didTapOnRightButton), forControlEvents: UIControlEvents.TouchUpInside)
    
        viewFN.addSubview(button1)
        viewFN.addSubview(button2)
        viewFN.addSubview(button3)
    
    
        let rightBarButton = UIBarButtonItem(customView: viewFN)
        self.navigationItem.rightBarButtonItem = rightBarButton
    
    }
    
    0 讨论(0)
  • 2020-12-16 22:16

    This is an example for a button:

    // Right Side
    let rubricButton  = UIButton.buttonWithType(UIButtonType.System) as! UIButton
    rubricButton.frame = CGRectMake(0, 0, 100, 32)  // Size
    rubricButton.backgroundColor = UIColor.clearColor()
    rubricButton.setTitle("Options", forState: UIControlState.Normal)
    rubricButton.titleLabel!.font = UIFont(name: "Lato-Regular", size: 18)
    rubricButton.addTarget(self, action: "showRubricList:", forControlEvents: UIControlEvents.TouchUpInside)  // Action
    var rubricBarButtonItem: UIBarButtonItem = UIBarButtonItem(customView: rubricButton)  // Create the bar button
    
    // Add the component to the navigation Bar
    self.navigationItem.setRightBarButtonItems([rubricBarButtonItem], animated: false)
    
    0 讨论(0)
  • 2020-12-16 22:19

    Swift 3/4/5 Version of the Accepted Answer

    let viewFN = UIView(frame: CGRect.init(x: 0, y: 0, width: 180, height: 40))
    viewFN.backgroundColor = .yellow
    let button1 = UIButton(frame: CGRect.init(x: 0, y: 0, width: 40, height: 20))
    button1.setImage(UIImage(named: "notification"), for: .normal)
    button1.setTitle("one", for: .normal)
    
    button1.addTarget(self, action: #selector(self.didTapOnRightButton), for: .touchUpInside)
    let button2 = UIButton(frame: CGRect.init(x: 40, y: 8, width: 60, height: 20))
    button2.setImage(UIImage(named: "notification"), for: .normal)
    button2.setTitle("tow", for: .normal)
    let button3 = UIButton(frame: CGRect.init(x: 80, y: 8, width: 60, height: 20))
    button3.setImage(UIImage(named: "notification"), for: .normal)
    button3.setTitle("three", for: .normal)
    
    button3.addTarget(self, action: #selector(self.didTapOnRightButton), for: .touchUpInside)
    
    viewFN.addSubview(button1)
    viewFN.addSubview(button2)
    viewFN.addSubview(button3)
    
    let rightBarButton = UIBarButtonItem(customView: viewFN)
    

    Hope it helps someone. Cheers!

    0 讨论(0)
  • 2020-12-16 22:19

    You can do like this, try it:

    var view = UIView(frame: CGRectMake(0, 0, 100, 44))
    view.backgroundColor = UIColor.yellowColor()
    var barButtonItem = UIBarButtonItem(customView: view)
    self.navigationItem.rightBarButtonItem = barButtonItem
    
    0 讨论(0)
  • 2020-12-16 22:19

    I add this code in viewWillAppear(), and work for me

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = false
        self.navigationItem.title = "Forgot Password?"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView:UIImageView(image: UIImage(named: "btn_back")))
    }
    
    0 讨论(0)
提交回复
热议问题