How to adjust space between two UIBarButtonItem in rightBarButtonItems

前端 未结 14 2044
悲&欢浪女
悲&欢浪女 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:12

    To accomplish this in code without adding an extra container view, use a UIBarButtonItem with the system item type set to FixedSpace. Then set the width of the fixed space to -10 and place it between the two buttons.

    0 讨论(0)
  • 2020-12-08 07:13

    Create a UIBarButtonItem with type flexible or fixed space. Set the width and add it to the array of barbuttonitems. Try using a negative width, see if that works.

    Or, you could maybe adjust your image. The system buttons i think have a fixed size, and might include some transparent part, so even when packed together the still seem spaced.

    0 讨论(0)
  • 2020-12-08 07:17

    First:

    For UIBarButtonItem you must use constructor init(customView: UIView)

    Second:

    Use fixedSpace for set space between buttons

    example:

    let firstButton = UIButton()
    let firstButtonItem = UIBarButtonItem(customView: firstButton)
    
    let secondButton = UIButton()
    let secondButtonItem = UIBarButtonItem(customView: secondButton)
    
    let space = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    space.width = WIDTH
    
    self.navigationItem.rightBarButtonItems = [firstButtonItem, space, secondButtonItem]
    
    0 讨论(0)
  • 2020-12-08 07:17

    Swift 5

    In your AppDelegate add this code:

    let stackViewAppearance = UIStackView.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
    stackViewAppearance.spacing = -10
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-08 07:19

    If you are looking to have 2 buttons on the top right with no space in between them or on the right, this has worked for me.

    let imgLeft = UIImage(named: "buttonLeft")?.imageWithRenderingMode(.AlwaysOriginal)
    let bLeft = UIBarButtonItem(image: imgLeft, style: UIBarButtonItemStyle.Done, target: self, action: "action1:")
    let space = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
    space.width = -16.0
    
    bLeft.imageInsets = UIEdgeInsetsMake(0, 0, 0, -25.0)
    
    
    let imgRight = UIImage(named: "buttonRight")?.imageWithRenderingMode(.AlwaysOriginal)
    let bRight = UIBarButtonItem(image: imgRight, style: UIBarButtonItemStyle.Done, target: self, action: "action2:")
    
    bRight.imageInsets = UIEdgeInsetsMake(0, -25, 0, 0)
    
    
    self.navigationItem.rightBarButtonItems = [space,bLeft,bRight ]
    
    0 讨论(0)
提交回复
热议问题