skip to Main Content

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

2

Answers


    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)
      
    Login or Signup to reply.
  1. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search