Andrew Tuzson
Andrew Tuzson

Reputation: 639

Center bar buttons in toolbar in Xcode

I have two buttons inside of a toolbar that is positioned at the bottom of the screen. I would like to center those buttons within the toolbar. Everything I have found is focused on centering buttons within a nav bar and I am unsure of how to approach this issue. What am I missing?

enter image description here

This is the UI I am attempting to emulate:

enter image description here

Upvotes: 2

Views: 2686

Answers (2)

Bishal Ghimire
Bishal Ghimire

Reputation: 2600

Thks @Andrew, it works but for someone who wants it from code here is the example in Swift 5.0

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationController?.setToolbarHidden(false, animated: true)
        let spaceItemLeft = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        let nextItem = UIBarButtonItem(title: "NEXT", style: .plain, target: self, action: #selector(nextTapped))
        let spaceItemRight = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil)
        toolbarItems = [spaceItemLeft, nextItem, spaceItemRight]
    }

    @objc func nextTapped() {
        print("NEXT Tapped")
    }

Upvotes: 0

Andrew Tuzson
Andrew Tuzson

Reputation: 639

Adding a flexible space to the left and right of the buttons centered the buttons inside of the toolbar. Thanks to Midhun MP for the tip.

Upvotes: 10

Related Questions