不知道为什么 UIView 被推高了大约 10px

发布于 2024-07-25 11:04:12 字数 1410 浏览 9 评论 0原文

我创建了一个简单的 iPhone 应用程序,其中有两个 .xib 文件。 在应用程序的应用程序委托中,确实完成了启动,我通过调用显示第一个 .xib 文件:

[window addSubview:myView];

并对 UIButton 的 IBAction 执行相同的操作,以将视图更改为 myView2。

我发现,当我运行应用程序时,两个视图都会出现一个大约 10 像素的白条。 我还注意到按钮偏移了 10 像素(大约)。 所以我的印象是视图是从屏幕外 10 个像素处显示的,并且结尾很短。

知道为什么会发生这种情况,如何修复它,或者如何进一步调试正在发生的事情? 我检查了我的 .xib 文件,它们消耗了设备的整个高度(即没有白条),所以这看起来是 XCode 内部的问题。

编辑:我已经缩小了问题的范围。 我使用两种方法来加载子视图。 当我在 applicationDidFinishLaunching 中加载第一个视图时,一切都很好。 但是,如果我用 [self originalView] 方法替换 window 的两条消息,那么一切都会变得有点混乱。

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    //[self originalView];  <- I want to use this instead of the following two lines

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}

-(void)endView{
    endV = [[EndViewController alloc] initWithNibName:@"EndView" bundle:nil];
    [window removeFromSuperview];
    [window addSubview:endV.view];  
}
-(void)originalView{
    viewController = [[MyAppViewController alloc] init];
    [window removeFromSuperview];
    [window addSubview:viewController.view];

}

从我所看到的来看,我总是调用相同的代码行,无论它是在 applicationDidFinishLaunching 内部还是在 [self originalView] 中,但它看起来像 window 原来是一个不同的对象。

I've created a simple iPhone app which has two .xib files. In the app delegate at application did finish launching I display the first .xib file by calling:

[window addSubview:myView];

and I do the same on an IBAction for a UIButton to change the view to myView2.

What I'm finding is that there's a white bar of around 10 pixels when I run the app, for both views. I also notice that the buttons are offset by 10 pixels (approx.). So I get the impression that the view is being displayed from 10 pixels off the screen and ending short.

Any idea why this might be happening, how I can fix it, or how I can further debug what's going on? I've checked my .xib files and they are consuming the full height of the device (i.e. no white bars), so this looks to be a problem inside XCode.

EDIT: I've narrowed down the problem a little. I'm using two methods to load the subview. When I load the first view inside applicationDidFinishLaunching everything is fine. However, if I replace the two messages to window with the [self originalView] method, then everything goes a bit haywire.

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    //[self originalView];  <- I want to use this instead of the following two lines

    // Override point for customization after app launch    
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

}

-(void)endView{
    endV = [[EndViewController alloc] initWithNibName:@"EndView" bundle:nil];
    [window removeFromSuperview];
    [window addSubview:endV.view];  
}
-(void)originalView{
    viewController = [[MyAppViewController alloc] init];
    [window removeFromSuperview];
    [window addSubview:viewController.view];

}

From what I can see, I'm always calling the same lines of code, no matter if it's inside the applicationDidFinishLaunching or in [self originalView] but it seems like window is turning out to be a different object.

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

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

发布评论

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

评论(5

落在眉间の轻吻 2024-08-01 11:04:12

以正常方式使用 UIViewController 时(即 将其推到 UINavigationController 上),它会调整其视图的框架。

由于您是手动添加子视图,因此您必须自己调整框架。 原点位于右上角(状态栏顶部)。 您想要将视图向下移动 20 像素。

When using a UIViewController the normal way (i.e. pushing it on a UINavigationController), it adjusts its view's frame.

Since you're adding the subview manually, you have to adjust the frame yourself. The origin is in the upper right corner (at the top of the status bar). You want to shift your view 20 pixels down.

温柔嚣张 2024-08-01 11:04:12

这是我的解决方案:

// adjust the frame of subview which is going to be add
self.navController.view.frame = CGRectMake(0, 0, 320, 460);
[self.view addSubView:self.navController.view];

现在对我来说效果很好,祝你好运〜:)

Here is my solution:

// adjust the frame of subview which is going to be add
self.navController.view.frame = CGRectMake(0, 0, 320, 460);
[self.view addSubView:self.navController.view];

It works fine for me now, good luck~ :)

骄傲 2024-08-01 11:04:12

在界面生成器中,您可以添加将以编程方式显示的模拟界面元素(例如状态栏或导航栏)。 在检查器中,选择您的视图,然后转到“属性”选项卡。 您可以在此处模拟状态栏、顶部栏或底部栏。

In interface builder, you can add Simulated Interface Elements that will appear programatically (such as the status bar or the navigation bar). In your inspector, select your view, and go to the Attributes tab. Here you can simulate The Status bar, a top bar, or a bottom bar.

豆芽 2024-08-01 11:04:12

看起来您在错误的位置对该 UIViewController 调用了 PushVC。 我有同样的问题。 通过将之前的 UIViewController 中的 [self.navigationController PushViewController:animated:]viewDidLoad 移动到 viewDidAppear: 来解决这个问题> 在层次结构中。

Looks like you call pushVC to this UIViewController in the wrong place. I had the same problem. It was solved by moving [self.navigationController pushViewController: animated:] from viewDidLoad to viewDidAppear: in a previous UIViewController in hierarchy.

滿滿的愛 2024-08-01 11:04:12

每当出现奇怪的间隙,或者视图部分隐藏在状态栏后面时,它都与根控制器中的 shouldAutorotate 定义有关

  1. - 不确定它是否是一个错误,但尝试与返回保持一致该功能适用​​于所有子视图控制器(通常会导致视图控制器隐藏在状态栏后面)
  2. 子视图的 WantsFullscreen 定义 - 如果可能的话设置为 NO

我的应用程序中存在两个错误并解决了它们! 希望我可以节省你一些时间(我浪费了很多时间)

Whenever a weird gap appears, or the view is partly hidden behind the status bar, it has something to do with either

  1. shouldAutorotate definition in your root controller - not sure if it is a bug or not, but try to stay consistent with the return of that function for all sub-viewcontrollers (usually results in a viewcontroller being hidden behind the status bar)
  2. a wantsFullscreen definition of a subview - set to NO if possible

i had both bugs in my app and solved them! Hope I could save you some time (I wasted a lot on it)

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