简体   繁体   中英

Swift - get size of viewController with navigation bar and tab bar

my viewController has both navigation and tab bars. I need to figure out available space after subtracting size of navigation and tab bar. I tried:

let height = UIScreen.main.bounds.height - self.navigationController!.navigationBar.frame.size.height - self.tabBarController!.tabBar.frame.size.height

But it is not working... Am I doing something wrong?

EDITED: Sorry lacked information. I want to embed UIScroll view in between Navigation and Tab bar. The equation returned height that is bigger than available space.

titleView = UIScrollView(frame: CGRect(x: 0, y:0, width: titleWidth, height: height))

The titleView surpassed "tabbar.y"

The easiest way to get this information is using layout guides.

let viewHeight = view.frame.height - (topLayoutGuide.length + bottomLayoutGuide.length)

However, I think you're much better off using Autolayout to manage the height of your scrollView instead of setting in directly.

The solution was to also subtract height of statusBar. Damn..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM