How to change UIToolbar height? [duplicate]
Asked Answered
M

3

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

Mize answered 22/8, 2016 at 8:20 Comment(0)
E
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

Evetteevey answered 22/8, 2016 at 8:23 Comment(4)
May I ask where to put it? in UINavigationController? The method doesn't seems override any method from its superclass.Mize
@victorleungtw added .Evetteevey
It's working perfectSheared
@victorleungtw mate my codes working perfectly please approve my answer , thank you ;)Evetteevey
M
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)
Macfadyn answered 22/8, 2016 at 8:40 Comment(5)
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.Mize
Also tried option 2, and set the frame in UINavigationController with strong reference @IBOutlet. It doesn't change either.Mize
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 beforeMacfadyn
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.Mize
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 workMacfadyn
E
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

Evetteevey answered 22/8, 2016 at 8:23 Comment(4)
May I ask where to put it? in UINavigationController? The method doesn't seems override any method from its superclass.Mize
@victorleungtw added .Evetteevey
It's working perfectSheared
@victorleungtw mate my codes working perfectly please approve my answer , thank you ;)Evetteevey
M
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)
Monkeypot answered 22/8, 2016 at 8:22 Comment(1)
Thank you, but I forgot to mention auto layout is disabled.Mize

© 2022 - 2024 — McMap. All rights reserved.