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

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

提交回复
热议问题