I want to get a UITabBar
's width in order to get the width of an item. For this, I'm using:
let itemWidth = tabBar.frame.size.width / tabBar.items.count
However the width I get seems wrong...
The tabBar.frame.size.width
gives me the width of the tabBar in the Storyboard depending of the preview...
In other words, if my Storyboard preview is set to iPad Pro 12.9", tabBar.frame.size.width
will return 1024.0
even if the code is run on an iPhone 5s.
What am I missing here?
UITabBar
's dimensions will only be accurate after its layoutSubviews()
function was executed. Subclass UITabBarController
and access the tab bar's size in viewDidLayoutSubviews()
.
class CustomTabBarController: UITabBarController {
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print(tabBar.frame.width) //this will print the actual width
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments