Jerland2 Jerland2 - 16 days ago 5
iOS Question

Navigation Controller Toolbar Size & Location - iOS Swift

Hello I am currently working on an application in Swift 3. I am having problems in a table view controller which is embedded in a navigation controller. I am currently displaying the navigationController toolbar. However on the initial table view controller I set the navigation bar to hidden. This results in the toolbar being resized and relocated.

Does anyone know how I can reload the toolbar to follow the following line of code: (This is called in view will appear)

self.navigationController?.toolbar.frame = CGRect(x: 0, y: UIScreen.main.bounds.height-80, width: self.view.frame.size.width, height: 80)


I cannot find out how to resize the toolbar after hiding the navigation bar using the following: (This is called in view did appear)

self.navigationController?.setNavigationBarHidden(true, animated: true)

Answer

You could not resize your toolbar directly:

But you can inherit UIToolbar in your project:

import UIKit

class CustomToolbar: UIToolbar {

override func sizeThatFits(_ size: CGSize) -> CGSize {

    var newSize: CGSize = super.sizeThatFits(size)
    newSize.height = 80  // there to set your toolbar height 

    return newSize
    }

}

In the storyboard:

toolbar's class set to CustomToolbar

The result, height of toolbar is 80:

the height is 80 now