UIView背景大小问题

我正在添加一个UIImageView到UIViewController的UIView,正如我通常的图像视图的框架是相同的self.view ,以确保图像将覆盖整个视图。 然而,状态栏和imageView之间大约有20px的填充,我似乎无法摆脱。

我试过NSLogging不同的帧,并收到以下结果:

  NSLog(@"ImageView => %@", NSStringFromCGRect([imageView frame])); NSLog(@"self.view => %@", NSStringFromCGRect(self.view.frame)); NSLog(@"Screen Bounds => %@", NSStringFromCGRect([[UIScreen mainScreen] bounds])); NSLog(@"App window => %@", NSStringFromCGRect([[[[UIApplication sharedApplication] delegate] window] frame])); [LaunchViewController.m: line 26] ImageView => {{0, 0}, {320, 548}} [LaunchViewController.m: line 27] self.view => {{0, 0}, {320, 548}} [LaunchViewController.m: line 28] Screen Bounds => {{0, 0}, {320, 568}} [LaunchViewController.m: line 29] App window => {{0, 0}, {320, 568}} 

结果显示图像视图实际上应该覆盖self.view ,在它本身和状态栏之间根本没有填充。

UIViewController是UINavigationController的根视图控制器,并且我已经将导航栏设置为隐藏。 我认为也许这可能会导致问题,但是,删除UINavigationController和replace它只是UIViewController后,填充仍然存在。

有没有人有任何build议去除填充/有经验之前这个问题,并有一个修复?

这里是一个图像显示正在发生什么 – 绿色区域是UIView( self.view ),橙色区域是UIImageView。

在这里输入图像说明

 [self.navigationController setNavigationBarHidden:YES]; [self.view setBackgroundColor:[UIColor greenColor]]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; [imageView setImage:[UIImage imageNamed:@"Orange-568h@2x.png"]]; [self.view addSubview:imageView]; 

在您的应用程序委托中,将FullScreenLayout设置为YES,然后再将其设置为视图控制器的根视图控制器:

 ViewController *vc = [[ViewController alloc] init]; [vc setWantsFullScreenLayout:YES]; [[self window] setRootViewController:vc]; 

或者你可以在viewDidLoad中设置它

 [self setWantsFullScreenLayout:YES]; 

关于setWantsFullScreenLayout的总结:当视图控制器呈现其视图时,通常会缩小该视图,使其框架不与设备的状态栏重叠。 将此属性设置为YES会导致视图控制器调整其视图的大小,使其填充整个屏幕,包括状态栏下的区域。 (当然,发生这种情况时,托pipe视图控制器的窗口本身的大小必须能够填充整个屏幕,包括状态栏下方的区域。)通常,在具有半透明状态的情况下,将此属性设置为YES酒吧,并希望您的视图的内容在该视图后面可见。 从这里

希望这可以帮助。

我有同样的问题,并解决了这个问题:

 UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

当状态栏没有hidden时, self.view.frame给你错误的xy

看到你的self.view ,它是20点以下,状态栏显示。 因此, self.view.frame(0,20,w,h)

你的图像视图采用相同的矩形(0,20,w,h) ,并将其添加到self.view 。 对于self.view ,图像视图将其框架设置为self.view的“y”位置下方20个点。

尝试将self.view.bounds设置为图像视图框架。

希望这可以帮助!

试试UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; isntead。 使用bounds而不是frame

viewDidLoad方法你的UIViewController的xib被加载,你的用户界面的更新值不被检测到。

如果您的UIView完全相同(不包括UINavigationBar,UITabbar,状态栏或设备-iPhone 3.5英寸/ 4英寸更改),则不会造成任何问题。 但是如果你在改变过程中的任何东西,你可能会在viewDidLoad上得到错误的值。

最简单的方法来检查这是调整你的屏幕和查看界面上的viewDidAppear,更新UI后。

请检查你的xib / storyboard。 您当前的用户界面和devise界面之间有一个变化。

为了解决这个问题,你可以在viewDidAppear上使用一个函数updateUI ,并再次更新相关视图。

 [self.imageView setFrame:self.view.frame] 

这将解决它。

尝试移动[self.navigationController setNavigationBarHidden:YES]; 至

 - (void) viewWillAppear: (BOOL) animated { [super viewWillAppear: animated]; [self.navigationController setNavigationBarHidden: YES animated: NO]; } 

将imageView添加到self.view中时,尝试将self.view.frame更改为self.view.bounce。

 UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.frame]; 

或者只是使用CGRectMake(0,0,320,568)