在取keyWindow的值时,由于keyWindow 是实时变化的,其值可能为0,用[UIScreen mainScreen].bounds 即可。
[UIScreen mainScreen].bounds vs [UIApplcation sharedApplication].keyWindow.bounds?
I have view that i want to cover entire screen. And i want to set its frame to cover entire screen. Browsing the stack overflow i found these two different ways of setting view frame to cover the screen:
[UIScreen mainScreen].bounds
[UIApplcation sharedApplication].keyWindow.bounds
It seems to me they are returning same values always, or at least in few test cases I have tried.
Currently i am using UIScreen, but i curious to know difference between these calls? Will there be some cases where they will return different values?
The methods are slightly different. [UIScreen mainScreen] returns the devices UIScreen object, the bounds of this will always be the size of the devices screen. [UIApplication sharedApplication].keyWindow returns the current key UIWindow for the application, which could conceivably not be the full size of the devices screen.
Another obvious case where these could differ is if the device is attached to multiple screens. In this case, [UIScreen mainScreen].bounds will always return the devices screen size, but the key window could be on one of the other screens, and it will have a completely different size, depending on what kind of screen it is attached to.
本文探讨了两种设置视图框架以覆盖整个屏幕的方法:通过[UIScreen mainScreen].bounds 和 [UIApplication sharedApplication].keyWindow.bounds。这两种方法虽然通常返回相同值,但它们在多屏环境下或当应用窗口不全屏时可能会有所不同。
7177

被折叠的 条评论
为什么被折叠?



