UIView 背景大小问题 [英] UIView background sizing issue

查看:17
本文介绍了UIView 背景大小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像往常一样将 UIImageView 添加到 UIViewController 的 UIView 中,图像视图的框架与 self.view 相同,以确保图像将覆盖整个视图.但是,状态栏和 imageView 之间有大约 20 像素的填充,我似乎无法摆脱.

I'm adding a UIImageView to the UIViewController's UIView as I would normally, with the frame of the image view being the same as self.view to make sure that the image will cover the whole view. However, there is a roughly 20px padding between the status bar and the imageView which I can't seem to get rid of.

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

I've tried NSLogging the different frames, and receiving the following results:

 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,并且在自身和状态栏之间完全没有填充.

The results show that the image view should in fact be covering self.view with no padding at all between itself and the status bar.

UIViewController 是 UINavigationController 的根视图控制器,我已将导航栏设置为隐藏.我认为这可能是导致问题的原因,但是在删除 UINavigationController 并将其替换为 UIViewController 后,填充仍然存在.

The UIViewController is the root view controller of a UINavigationController, and I have set the navigation bar to be hidden. I thought that maybe this could be causing the issue however, after removing the UINavigationController and replacing it with just the UIViewController, the padding was still there.

有没有人对删除填充有任何建议/以前遇到过这个问题并有解决办法?

Has anybody got any advice for removing the padding / has experience this issue before and has a fix?

这是显示正在发生的事情的图像 - 绿色区域是 UIView (self.view),橙色区域是 UIImageView.

Here is an image showing what's going on - the green area is the UIView (self.view), and the orange area is the 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:

In your app delegate set the FullScreenLayout to YES just before you make it the view controller the root view controller:

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

或者你可以在 viewDidLoad 中设置

or you can set it in viewDidLoad

[self setWantsFullScreenLayout:YES];

关于 setWantsFullScreenLayout 的总结:当视图控制器呈现其视图时,它通常会缩小该视图,使其框架不会与设备的状态栏重叠.将此属性设置为 YES 会导致视图控制器调整其视图的大小,以便它填满整个屏幕,包括状态栏下方的区域.(当然,要做到这一点,托管视图控制器的窗口本身必须调整大小以填满整个屏幕,包括状态栏下方的区域.)如果您具有半透明状态,通常会将此属性设置为 YES栏并希望您的视图内容在该视图后面可见.从 这里

A summary on setWantsFullScreenLayout: When a view controller presents its view, it normally shrinks that view so that its frame does not overlap the device’s status bar. Setting this property to YES causes the view controller to size its view so that it fills the entire screen, including the area under the status bar. (Of course, for this to happen, the window hosting the view controller must itself be sized to fill the entire screen, including the area underneath the status bar.) You would typically set this property to YES in cases where you have a translucent status bar and want your view’s content to be visible behind that view. From here

希望这会有所帮助.

这篇关于UIView 背景大小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆