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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1054539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:38:11  来源:igfitidea点击:

Not sure why UIView is being nudged up by around 10px

iphonexcodeuiviewwindowxib

提问by mac_55

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:

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

[window addSubview:myView];

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

我对 UIButton 的 IBAction 执行相同的操作以将视图更改为 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.

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

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.

知道为什么会发生这种情况,我如何修复它,或者我如何进一步调试正在发生的事情?我检查了我的 .xib 文件,它们占用了设备的整个高度(即没有白条),所以这看起来是 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 applicationDidFinishLaunchingeverything is fine. However, if I replace the two messages to windowwith the [self originalView]method, then everything goes a bit haywire.

编辑:我已经稍微缩小了问题的范围。我正在使用两种方法来加载子视图。当我在里面加载第一个视图时,applicationDidFinishLaunching一切都很好。但是,如果我更换了两条消息window[self originalView]方法,那么一切都有点失控。

- (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 applicationDidFinishLaunchingor in [self originalView]but it seems like windowis turning out to be a different object.

从我所见,我总是调用相同的代码行,无论它是在里面applicationDidFinishLaunching还是在里面,[self originalView]但它似乎window变成了一个不同的对象。

回答by Nikolai Ruhe

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

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

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.

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

回答by firestoke

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~ :)

现在对我来说很好用,祝你好运~:)

回答by Reed Olsen

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.

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

回答by iiFreeman

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

看起来你UIViewController在错误的地方调用了 pushVC 。我有同样的问题。它是通过[self.navigationController pushViewController: animated:]从层次结构中的前一个移到viewDidLoad来解决的。viewDidAppear:UIViewController

回答by MarkHim

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
  1. 根控制器中的 shouldAutorotate 定义 - 不确定它是否是错误,但尝试与所有子视图控制器的该函数的返回保持一致(通常导致视图控制器隐藏在状态栏后面)
  2. 子视图的 WantFullscreen 定义 - 如果可能,设置为 NO

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

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