How to adjust space between two UIBarButtonItem in rightBarButtonItems

前端 未结 14 2074
悲&欢浪女
悲&欢浪女 2020-12-08 06:44

I am using the following codes to add two button to self.navigationItem.rightBarButtonItems, and I think in iOS7, the space between two buttons are too wide, is there a way

14条回答
  •  春和景丽
    2020-12-08 07:18

    Swift 5

    If you want to add space between two Bar Button items then add a flexible space in between, the two buttons will be pushed to the left and right edge as the flexible space expands to take up most of the toolbar.

    For Example:

    let toolBar = UIToolbar()
    
    var items = [UIBarButtonItem]()
    
    let backBarButton =  UIBarButtonItem(image: UIImage(named: "icon-back.png"), style: .done, target: self, action: #selector(backButtonTapped))
    
    let nextBarButton =  UIBarButtonItem(image: UIImage(named: "icon-next.png"), style: .done, target: self, action: #selector(nextButtonTapped))
    
    let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
    items.append(backBarButton)
    items.append(spacer)
    items.append(nextBarButton)
    
    toolBar.setItems(items, animated: true)
    

提交回复
热议问题