Avoid hardcoding iPhone screen size with programmatic view creation

前端 未结 2 1333
死守一世寂寞
死守一世寂寞 2021-02-03 10:48

I was looking up how to create a view programmatically and found the following example code:

self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

2条回答
  •  误落风尘
    2021-02-03 11:17

    Is there a way to look up the size of the screen?

    Yup:

    CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
    

    applicationFrame

    This property contains the screen bounds minus the area occupied by the status bar, if it is visible. Using this property is the recommended way to retrieve your application’s initial window size. The rectangle is specified in points.

提交回复
热议问题