UIViewController 未填满屏幕

发布于 2024-10-16 21:54:26 字数 1586 浏览 2 评论 0原文

我有一个 UINavigationController 推动另一个 UIViewController。在此 UIViewController 中,我将在纵向模式下显示一个 UITableView,在横向模式下显示另一个视图。

因此,在我的 viewDidLoad 中,我创建了 UIView,然后向其中添加 2 个 ViewController。我的问题是,当它加载时,我在顶部得到以下白边。 example

我认为这是因为(在下面的步骤 3 中)

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

没有返回全屏,减去导航栏。这是对的吗?如果是这样,我怎样才能让它返回完整尺寸,这样就没有白边距?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];

// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc] 
    initWithNibName:@"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];

// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

// Step 4 - Add the vertical view to the containerView
//        and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease]; 
[[self view] addSubview:containerView];                     
[containerView addSubview:[tableDataViewController view]];

// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];    
 }

非常感谢

迈克

I have a UINavigationController that pushes on another UIViewController. In this UIViewController I am going to show a UITableView when in portrait mode and another view in landscape mode.

Therefore in my viewDidLoad I am creating UIView and then adding 2 ViewControllers to this. My problem is that when it loads up I get the following white margin at the top.
example

I think this is because of (in my Step 3 below ) the

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

is not returning the full screen, minus the navigation bar. Is this right? If so how can I make it return the full size so there is no white margin?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];

// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc] 
    initWithNibName:@"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];

// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

// Step 4 - Add the vertical view to the containerView
//        and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease]; 
[[self view] addSubview:containerView];                     
[containerView addSubview:[tableDataViewController view]];

// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];    
 }

Many thanks

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坏尐絯 2024-10-23 21:54:26

我认为你的帧偏移有问题。启用导航栏后,您在 appFrame 中获得的矩形的偏移量为 44.f(导航栏的高度) - 检查 NSLog 并查看是否属实。

由于您正在设置将放置在原点的视图的框架,因此应将 x 和 y 原点设置为零。您可以通过检查以更安全的方式执行此操作

CGFloat navHeight = navController.navigationBarHidden ? 0 :
    navController.navigationBar.frame.size.height;

。事实上,我认为使用 [UIScreen mainScreen] 的bounds 属性可能是更好的整体解决方案。它将正确设置原点和大小,您不需要检查导航栏是否存在。

检查发生了什么:

CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(@"%@", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(@"%@", NSStringFromCGRect(screenFrame));

I think you have an issue with your frame offsets. With the navigation bar enabled the rect you get in appFrame has a y offset of 44.f (the navigation bar's height) - check with NSLog and see if that's true.

Because you are setting the frame of a view that will be placed at the origin it should have x and y origins set to zero. You can do this in a safer manner by checking

CGFloat navHeight = navController.navigationBarHidden ? 0 :
    navController.navigationBar.frame.size.height;

In fact I think using the bounds property of [UIScreen mainScreen] may be a better overall solution. It will come with the origin and size set correctly and you shouldn't need to check the presence of the navigation bar.

Check what's going on:

CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(@"%@", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(@"%@", NSStringFromCGRect(screenFrame));
╭⌒浅淡时光〆 2024-10-23 21:54:26

有几件事正在发生。最重要的是,frame 描述了视图在其父视图坐标系中的位置。

containerView 似乎是作为自定义视图的子视图添加的,其坐标系原点位于导航栏下方,而不是应用程序框架。

您想要填充自定义视图,因此 containerView 的框架应该类似于

CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);

There are a couple things going on. Above all, frame describes a view's location in its superview's coordinate system.

containerView seems to be added as a subview of a custom view, and its coordinate system origins below the navigation bar, not the application frame.

You want to fill the custom view, so the frame of containerView should be something like

CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文