navigation bar right bar button items spacing

前端 未结 10 2079
执念已碎
执念已碎 2021-01-31 16:47

I have created a with left bar button item added from storyboard, titleView and three right bar button items from code.

Here is the code:

override func         


        
10条回答
  •  清歌不尽
    2021-01-31 17:36

    I solved my problem in this way:

    var searchImage = UIImage(named: "search-selected")!
    var clipImage = UIImage(named: "clip")!
    var pencilImage = UIImage(named: "pencil")!
    
    let searchBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    searchBtn.setImage(searchImage, forState: UIControlState.Normal)
    searchBtn.addTarget(self, action: "searchBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
    searchBtn.frame = CGRectMake(0, 0, 30, 30)
    let searchBarBtn = UIBarButtonItem(customView: searchBtn)
    
    let clipBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    clipBtn.setImage(clipImage, forState: UIControlState.Normal)
    clipBtn.addTarget(self, action: "clipBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
    clipBtn.frame = CGRectMake(0, 0, 30, 30)
    let clipBarBtn = UIBarButtonItem(customView: clipBtn)
    
    let pencilBtn: UIButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton
    pencilBtn.setImage(pencilImage, forState: UIControlState.Normal)
    pencilBtn.addTarget(self, action: "pencilBtnPressed", forControlEvents: UIControlEvents.TouchUpInside)
    pencilBtn.frame = CGRectMake(0, 0, 30, 30)
    let pencilBarBtn = UIBarButtonItem(customView: pencilBtn)
    
    self.navigationItem.setRightBarButtonItems([pencilBarBtn, clipBarBtn, searchBarBtn], animated: false)
    

    Now it looks good,

    Update for Swift 4.1

    let testButton : UIButton = UIButton.init(type: .custom)
    testButton.setImage(editImage, for: .normal)
    testButton.addTarget(self, action: #selector(didTapCameraButton), for: .touchUpInside)
    testButton.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    let addButton = UIBarButtonItem(customView: testButton)
    

提交回复
热议问题