Home > Mobile >  Wrong positioning of UIBarButtonItem
Wrong positioning of UIBarButtonItem

Time:12-22

I have this code for my UIBarButtonItem. When i'm trying with title, everything is ok, but with image it shows a little bit left then it should be. I don't know how to fix it, what can be wrong? Same problem if i do leftBarButtonItem.

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "123", style: .done, target: self, action: #selector(toggleInterfaceStyle))

navigationItem.rightBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "day"), style: .done, target: self, action: #selector(toggleInterfaceStyle))

enter image description here

enter image description here

enter image description here

CodePudding user response:

  1. This code could help you in terms of padding:

    let negativeSeperator = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) 
    negativeSeperator.width = 12
    

Width must be positive in Swift version

  1. Also you can use insets:

    barButtonItem.imageInsets = .init(top: 0, left: 10, bottom: 0, right: 10)
    

CodePudding user response:

I had the same issue, and there's a neat trick you can do with a UIBarButtonSystemItemFixedSpace, add one of these with a negative width before your first button and after your last button and it will move the button to the edge

let negativeSeperator = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil) negativeSeperator.width = 12

  • Related