ios 在更深的视图层次结构中获取没有导航栏高度和标签栏高度的框架高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30503254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get frame height without navigation bar height and tab bar height in deeper view hierarchy
提问by Darko D.
I have a ViewController (B) which is handled by a PageViewController which is inside another ViewController (A) and open it modal. I have centered the ViewController (B) exactly in the middle of the screen. This works fine.
我有一个 ViewController (B),它由另一个 ViewController (A) 内的 PageViewController 处理并打开它。我已经将 ViewController (B) 正好位于屏幕中间。这工作正常。
But when I push the ViewController (A) from a NavigationController the frame of ViewController (B) is to big and extends below the NavigationBar and the TabBar. But I want it centered between the NavigationBar and the TabBar.
但是当我从 NavigationController 推送 ViewController (A) 时,ViewController (B) 的框架很大并且延伸到 NavigationBar 和 TabBar 下方。但我希望它在 NavigationBar 和 TabBar 之间居中。
I know how I can get the height of the navbar and the tabbar so I could resize ViewController (B):
我知道如何获得导航栏和标签栏的高度,以便我可以调整 ViewController (B) 的大小:
var topBar = self.navigationController?.navigationBar.frame.height
var bottomBar = self.tabBarController?.tabBar.frame.height
But this does not work deep down in the vier hierarchy in ViewController (B). self.navigationController and self.tabBarController are nil. So how can I get the height of the navbar and tabbar deeper down in the view hierarchy? Or do I just have to pass it down from one ViewController to another till I reach ViewController (B)? Or is there another more obvious way to center the view? Thanks.
但这在 ViewController (B) 的 vier 层次结构的深处不起作用。self.navigationController 和 self.tabBarController 为零。那么如何在视图层次结构中更深入地获得导航栏和标签栏的高度?或者我是否只需要将它从一个 ViewController 传递到另一个直到我到达 ViewController (B)?还是有另一种更明显的方式来使视图居中?谢谢。
(I have tried to post screenshots for better understanding but I miss the needed reputation points to post images, sorry)
(我试图发布截图以便更好地理解,但我错过了发布图片所需的声誉点,抱歉)
回答by Ehsan Jelodar
Use this :
用这个 :
let height = UIApplication.sharedApplication().statusBarFrame.height +
self.navigationController!.navigationBar.frame.height
this support portrait and landscape state.
此支持纵向和横向状态。
Swift 5
斯威夫特 5
let height = UIApplication.shared.statusBarFrame.height +
self.navigationController!.navigationBar.frame.height
回答by BSK-Team
For UITabBar, this code work :
对于 UITabBar,此代码有效:
self.tabBarController?.tabBar.frame.height
回答by Matthew Harries
For Swift 3
对于 Swift 3
navBarHeight = (self.navigationController?.navigationBar.intrinsicContentSize.height)!
+ UIApplication.shared.statusBarFrame.height
回答by mmika1000
As for iOS 13, since the statusBarFrame
property of a UIApplication object was deprecated, one can use:
对于iOS 13,由于statusBarFrame
UIApplication 对象的属性已弃用,因此可以使用:
let statusBarHeight = navigationController?.view.window?.windowScene?.statusBarManager?.statusBarFrame.height ?? 0
let navigationBarHeight = navigationController?.navigationBar.frame.height ?? 0
Now, it may seem that doesn't help you @OP since your navigationController is nil, but you could try to use view.window?.windowScene?.statusBarManager?.statusBarFrame.height
for the statusBar height and figure out a way to find the navBar height as well? Hope that helps :)
现在,@OP 似乎对您没有帮助,因为您的 navigationController 为零,但是您可以尝试使用view.window?.windowScene?.statusBarManager?.statusBarFrame.height
statusBar 高度并找出一种方法来查找导航栏高度?希望有帮助:)
回答by Rajdeep Singh
Navigation bar height status bar height and tab bar height is always constant :
导航栏高度状态栏高度和标签栏高度始终不变:
- Navigation bar - 44pts
- Status bar - 20pts
- Tab bar - 49pts.
- 导航栏 - 44pts
- 状态栏 - 20pts
- 标签栏 - 49pts。
You can directly subtract these constants from view height: self.view.frame.size.height- (44+20+49)
可以直接从视图高度减去这些常量: self.view.frame.size.height- (44+20+49)