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

前端 未结 6 1567
猫巷女王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: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!

提交回复
热议问题