0

I'm going to get straight to the point. The below code adds two bar button items without a problem, however I was wondering how I can put a distance between these two bar button items? They are too close to each other, that's why I'm looking for a way to manually set their distance to each other. I'd appreciate any help! Thank you very much in advance!

let btnPlus = UIBarButtonItem()
        btnPlus.title = "+"

    btnPlus.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "Arial", size: 24)!], for: .normal)
    btnPlus.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "Arial", size: 24)!], for: .selected)
    btnPlus.target = self
    btnPlus.action = #selector(OnbtnPlusTouched)
    //btnPlus.setTitleTextAttributes([NSAttributedStringKey: UIFont(name: "Arial", size: 14)!], for: UIControlState.normal)

    let btnRating = UIBarButtonItem()
    btnRating.title = "Oy Ver"

    btnRating.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "Arial", size: 24)!], for: .normal)
    btnRating.setTitleTextAttributes([NSAttributedStringKey.font : UIFont(name: "Arial", size: 24)!], for: .selected)
    btnRating.target = self
    btnRating.action = #selector(OnbtnPlusTouched)
    //btnPlus.setTitleTextAttributes([NSAttributedStringKey: UIFont(name: "Arial", size: 14)!], for: UIControlState.normal)

    navigationItem.rightBarButtonItems = [btnPlus, btnRating]
Sinto
  • 3,915
  • 11
  • 36
  • 70
Ege Akkaya
  • 176
  • 1
  • 13

1 Answers1

1

You can add a UIBarButtonItem with a fixed space or a flexible space:

let btnPlus = UIBarButtonItem()
let btnRating = UIBarButtonItem()
let fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
fixedSpace.width = 15

navigationItem.rightBarButtonItems = [btnPlus, fixedSpace, btnRating]

There is also the possibility to use .flexibleSpace.

regina_fallangi
  • 2,080
  • 2
  • 18
  • 38