6

I want to change the height of the UIToolbar in Navigation Controller, but I am not able to do so in swift with Xcode 7.3 and iOS 9. I have tried to set frame with CGRectMake at viewDidLoad, but it didn't work. Also tried to init a custom UIToolbar(), but the height remain the same.

Edit:

As per one of the answers, I have tried selected toolbar, press control and select the toolbar again, but what I got is shown in below screenshot instead:

Screenshot

Victor Leung
  • 429
  • 1
  • 5
  • 15

3 Answers3

6

There are 2 options depending on whether you are using Storyboards or not.

Option 1: Using Storyboard

1) Go to your Storyboard and from your selected Toolbar press and hold Ctrl and click on your Toolbar again as if you were assigning an IBAction. Then you will get the following:

(Sorry for the quality of the first image; had to take a snap with my phone because I couldn't make a screenshot while holding ctrl)

enter image description here enter image description here

2) Then press on height to get the constraint and change the value:

enter image description here

Option 2: Using Swift for e.g. 55 px height

 yourToolBar.frame = CGRect(x: 0, y: view.frame.size.height - 55, width: view.frame.size.width, height: 55)
tech4242
  • 2,348
  • 2
  • 23
  • 33
  • 1
    Tried option 1, but couldn't open the same window as your screenshot. Mine only has options "Outlets", "Outlet Collection", "Reference Outlets" and "Reference Outlets Collections". By the way the auto layout is disabled in storyboard. – Victor Leung Aug 22 '16 at 08:56
  • Also tried option 2, and set the frame in UINavigationController with strong reference @IBOutlet. It doesn't change either. – Victor Leung Aug 22 '16 at 08:57
  • Yeah if you are using the storyboard you have to use option 1. Are you sure that you have selected your toolbar, then while holding ctrl you clicked on your toolbar again? It should show the blue line with the blue dots on each end. I tried this 1 minute ago and it definitely works if done right - and I have also used this in an app before – tech4242 Aug 22 '16 at 08:58
  • 1
    Thank you for your reply. But I am still not able to get the same screen as yours with xcode version 7.3.1. I edited my question with screenshot attached. – Victor Leung Aug 22 '16 at 09:11
  • I saw your screenshot.. that's what happens when your right click the toolbar etc. Just hold Ctrl while hovering with your mouse above the toolbar and then press/click on the toolbar again. See my new attached image. As long as you don't see the two connecting blue dots, it won't work – tech4242 Aug 22 '16 at 09:13
5

You can use it with ; Change 45 for min or max heights.

override func layoutSubviews() {
    super.layoutSubviews()

    var frame = self.bounds
    frame.size.height = 45
    self.frame = frame
}

override func sizeThatFits(size: CGSize) -> CGSize {
    var size = super.sizeThatFits(size)
    size.height = 45
    return size
}

Use it with like ;

let navigationController = UINavigationController(navigationBarClass: nil, toolbarClass: Toolbar.self)

Thanks

SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
1

If you use auto layout, you could set a Height Constraint in the Storyboard.

Or you can do it programmatically like this:

myToolbar.frame = CGRectMake(0, 50, 320, 35)
Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92