Ivan Cantarino
Ivan Cantarino

Reputation: 3246

BarButtonItem EdgeInsets doesn't work

I'm trying to add a padding to the right of a navigationBar rightBarButtonItems in between the buttons but it's now working.

Here's the button creation code with the given inset:

lazy var previewBarButtonItem: UIBarButtonItem = {
    let buttonItem = UIBarButtonItem(title: "Preview", style: .plain, target: self, action: #selector(handlePreview))
    buttonItem.tintColor = UIColor.black
    buttonItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
    buttonItem.imageInsets = UIEdgeInsetsMake(0, -15, 0, 15)
    return buttonItem
}()

override func viewDidLoad() {
    super.viewDidLoad()
    setupRightNavItems()
}

fileprivate func setupRightNavItems () {
    navigationItem.rightBarButtonItems = [submitBarButtonItem, previewBarButtonItem]
}

Can you give a hint on why this isn't working?

Thanks

Upvotes: 1

Views: 149

Answers (1)

Ivan Cantarino
Ivan Cantarino

Reputation: 3246

I managed to fix this issue, just added a fixedSpace in-between the bar button items as follows:

1) remove any EdgeInsets 2) add the following fixed space:

fileprivate func setupRightNavItems () {
    let spacing = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
    spacing.width = 25 //change to your desired space width
    navigationItem.rightBarButtonItems = [submitBarButtonItem, spacing, previewBarButtonItem]
}

Upvotes: 1

Related Questions