ios 更改 UIViewController self.view 框架

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10429127/
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-08-30 18:14:41  来源:igfitidea点击:

Change UIViewController self.view frame

objective-ciosuiviewuiviewcontroller

提问by Fabrizio Prosperi

I am trying to display a viewController xib view with its view displayed under a fixed header banner (0,0,320,44) which is an imageView added on Application window.

我正在尝试显示一个 viewController xib 视图,其视图显示在固定标题横幅 (0,0,320,44) 下,这是一个添加到应用程序窗口的 imageView。

This is because I need it to stick on the screen while I navigate multiple view controllers. Its a requirement of the project I am doing.

这是因为当我导航多个视图控制器时,我需要它粘在屏幕上。这是我正在做的项目的要求。

So far I have tried:

到目前为止,我已经尝试过:

  • Resizing the XIB view to the appropriate frame size (0,0,320,416), both with dot notation and setter.

  • Changing the frame in viewDidLoad with self.view.frame = CGRectMake(0, 44, 320, 416);

  • 调整大小的XIB视图到合适的帧大小(0,0,320,416),都与点符号和setter。

  • 使用 self.view.frame = CGRectMake(0, 44, 320, 416) 更改 viewDidLoad 中的框架;

but it is not working. Any suggestion?

但它不起作用。有什么建议吗?

回答by sundar

Set the frame in viewWillAppear

将框架设置在 viewWillAppear

- (void)viewWillAppear:(BOOL)animated
{
     self.view.frame = CGRectMake(0, 44, 320, 416);
    [super viewWillAppear:animated];
}

回答by langtutheky

Just want to add a bit more. The reason for changing view of a UIViewController does not work in viewDidLoadis because of the call [window makeKeyAndVisible], which is usually called after your viewDidLoad. This call will change the frame of the window.rootViewControllerto equal the window's frame. You can test this by changing the UIViewController's view.frame AFTER [window makeKeyAndVisible], and the changes will stick.

只是想补充一点。更改 UIViewController 的视图不起作用的原因viewDidLoad是因为调用[window makeKeyAndVisible],它通常在您的viewDidLoad. 此调用会将 的框架更改window.rootViewController为等于窗口的框架。您可以通过更改 UIViewController 的 view.frame AFTER 来测试这一点,更改将保持不变[window makeKeyAndVisible]

viewWillAppearand viewDidAppearare called within [window makeKeyAndVisible]but after the view's frame got resized.

viewWillAppear并且在视图的框架调整大小之后viewDidAppear被调用[window makeKeyAndVisible]

The caveat is that only the top most controller which is the rootViewControllerhas its view's frame changed automatically. This DOES NOT affect any childViewControllersattached to the rootViewController.

需要注意的是,只有最顶层的控制器才会rootViewController自动更改其视图的框架。这不会影响任何childViewControllers附加到rootViewController.