UIview全屏

Ale*_*dro 5 ios

我正在使用以下代码创建UIview:

UIview* baseView = [[UIview alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//self.view = baseView;
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = YES;
baseView.alpha = 0.7;
Run Code Online (Sandbox Code Playgroud)

唯一的问题是即使我使用mainScreen选项将其设置为全屏,它也会全屏显示,除了UIWindow顶部的5cm线.这有什么理由吗?

Pet*_*tar 16

好的,所以之所以发生这种情况,是因为[[UIScreen mainScreen] applicationFrame]如果可见,将返回窗口框架减去状态栏的大小.

要修复它,一个简单的方法是:

 UIView* baseView = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                             0,
                                                             [[UIScreen mainScreen] applicationFrame].size.width,
                                                             [[UIScreen mainScreen] applicationFrame].size.height)];
//self.view = baseView;
[self.view addSubview:baseView];
[baseView setBackgroundColor:[UIColor blackColor]];
baseView.userInteractionEnabled = YES;
baseView.alpha = 0.7;
Run Code Online (Sandbox Code Playgroud)

  • 我想你想要[UIScreen mainScreen] .bounds不是[UIScreen mainScreen] .applicationFrame (3认同)
  • 它正在工作,但显示在选项卡栏和导航栏下方。 (2认同)
  • 此答案适用于 iOS v < 7.0 (2认同)