Navigation bar with multiple buttons

后端 未结 3 1729
北荒
北荒 2020-12-15 05:00

I have a navigation bar with a left and right button, and I need to put another button next to the right button. Does anyone know how I can go about this? Here is some code

相关标签:
3条回答
  • 2020-12-15 05:48

    Instead of using self.navigationItem.rightBarButtonItem, use

    self.navigationItem.rightBarButtonItems //note the plural
    

    This allows you to set an array of buttons rather than a single one.

    See the UINavigationItem class reference for details.

    0 讨论(0)
  • 2020-12-15 05:52

    It's quite easy :)

    https://developer.apple.com/documentation/uikit/uinavigationitem/1624956-rightbarbuttonitems

    navigationItem.rightBarButtonItems = [rightA, rightB] // @[rightA, rightB] for ObjC
    
    0 讨论(0)
  • 2020-12-15 05:56
      let RightBarButton = UIButton()
            RightBarButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
            RightBarButton.frame = CGRectMake(30,0,30,30)
            RightBarButton.setImage(UIImage(named: "search-icon.png"), forState: .Normal)
            RightBarButton.addTarget(self, action: #selector(BaseViewController.OpenQuickLink), forControlEvents: .TouchUpInside)
    
            let RightBarButton2 = UIButton()
            RightBarButton2.setTitleColor(UIColor.blueColor(), forState: .Normal)
            RightBarButton2.frame = CGRectMake(0,0,30,30)
            RightBarButton2.setImage(UIImage(named: "share-icon.png"), forState: .Normal)
            RightBarButton2.addTarget(self, action: #selector(BaseViewController.Opensharelink), forControlEvents: .TouchUpInside)
            let barButtonItem1 = UIBarButtonItem(customView: RightBarButton2)
    
            let barButtonItem = UIBarButtonItem(customView: RightBarButton)
    
    
    navigationItem.rightBarButtonItems = [barButtonItem1, barButtonItem2]
    
    0 讨论(0)
提交回复
热议问题