Programmatically create UIToolbar with default height
Asked Answered
S

2

5

There are a number of questions regarding the height of UIToolbar, but I don't see one where the height is obtained dynamically. Is there a way to create a UIToolbar with the correct default height?

Sjoberg answered 17/3, 2016 at 8:29 Comment(0)
S
4

Create the toolbar with 0 height, then call sizeToFit. The toolbar will then have the default height.

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, width, 0)];
[toolbar sizeToFit];

Swift version:

let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: width, height: 0))
toolbar.sizeToFit()
Sjoberg answered 17/3, 2016 at 8:29 Comment(2)
Would the behaviour be any different if initialized with a non-zero height?Barolet
@Barolet is been a long time since I wrote this up, but my guess is that it doesn't matter.Sjoberg
J
0

if you are using UINavigationController

navigationController?.isToolbarHidden = false

This will give default navigation controller toolbar with default height

Jadajadd answered 22/4, 2017 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.