What is the use of UIScreen property in UIWindow?

3.3k views Asked by At

I was looking at the UIWindow class reference and there I found the UIScreen property which defaults to [UIScreen mainScreen]

@property(nonatomic,retain) UIScreen *screen NS_AVAILABLE_IOS(3_2);  
// default is [UIScreen mainScreen]. changing the screen may be an expensive operation and should not be done in performance-sensitive code

We initiailise the UIWindow object with the UIScreen in AppDelegate

self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]

I am wondering why do we need the UIScreen property in UIWindow

5

There are 5 answers

0
Vidhyanand On BEST ANSWER

UIScreen refers to the device frame.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScreen_Class/index.html

You can get the device size for finding the Devices iPhone 4,iPhone 5, iPhone 6, iPhone 6+ & iPad

[[UIScreen mainScreen] bounds] //gives u the size of the device.

You can differentiate the devices as per above sizes.

[[UIScreen mainScreen] bounds].size.width //gives the width of device [[UIScreen mainScreen] bounds].size.height) //gives the height of device.

Or you can use in this way

CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];

Hope it helps you...

0
Ratikanta Patra On

By default, all windows are created on the primary device screen. If additional screens are attached to the device, assigning a different screen object to this property causes the window to be displayed on the new screen.

As per Apple docs:

Moving windows from screen to screen is a relatively expensive operation and should not be done in performance-sensitive code. Instead, it’s recommended that you change the screen before displaying the window the first time. Changing the screen of a window that has not yet been ordered onto the screen has no significant additional cost.

0
Dharma On

From IOS Developer Library

self.window is assumed to be a declared property of your application delegate that is configured to retain the window object. If you were creating a window for an external display instead, you would assign it to a different variable and you would need to specify the bounds of the non main UIScreen object representing that display.

When creating windows, you should always set the size of the window to the full bounds of the screen. You should not reduce the size of the window to accommodate the status bar or any other items. The status bar always floats on top of the window anyway, so the only thing you should shrink to accommodate the status bar is the view you put into your window. And if you are using view controllers, the view controller should handle the sizing of your views automatically.

0
Hussain Shabbir On

A UIScreen object defines the properties associated with a hardware-based display. iOS devices have a main screen and zero or more attached screens. Use this class to obtain screen objects for each display attached to the device. Each screen object defines the bounds rectangle for the associated display and other interesting properties such as its brightness. For more refer this docs

0
Rumin On

Your application has only one UIWindow where all other view's draw's. When application begins UIWindow covers the entire screen space. This is the reason when you instantiate UIWindow you have to set it's frame size to Main Screen bounds size.

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds];

What is the role of UIScreen here ?

UIScreen represents rectangles of device screen. Your UIScreen knows more about your device screen attributes like resolution, pixel aspect ratio, brightness, dimming.

On other side UIWindow manages your view hierarchy and help views to display contents on the screen. UIWindow during initialisation gets it's frame from UIScreen bound property and initialises it self so that it can have maximum landscape from device screen to give it to subviews. Once initialisation done UIWindow frame includes status bar plus drawing area plus bottom bar.