3

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)
Jerland2
  • 1,096
  • 11
  • 28
  • Hi, Jerland did you try the below answer? – aircraft Nov 22 '16 at 02:09
  • @aircraft YES! Thank you, it worked like a charm, it never occurred to me to manipulate the storyboard toolbar, i was attempting to do it all programmatically. Many thanks – Jerland2 Nov 22 '16 at 02:47

1 Answers1

6

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

aircraft
  • 25,146
  • 28
  • 91
  • 166
  • @Jerland2 The first you must know the `UIToolbar` in `NavigationController ` is readonly, you can validate this in program, and the `customToolbar` is your best choice to `replicate functionality like the Apple Music now playing toolbar` – aircraft Nov 22 '16 at 02:47
  • Sorry deleted comment, i misunderstood your storyboard at first, it has been solved, thank you! – Jerland2 Nov 22 '16 at 02:47
  • This does not seem to work for swift 4. Does anyone have a work-around? Specifically for NavigationController's Toolbar, like in this question. Thanks. – Plutovman Jul 19 '18 at 16:23