以下是我来自didFinishLaunchingWithOptions中的应用程序委托(delegate)的代码

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

CGRect windowFrame = self.window.frame;
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, windowFrame.size.height-100, windowFrame.size.height, windowFrame.size.height)];
bottomView.backgroundColor = [UIColor redColor];
[self.window addSubview:bottomView];

//    self.viewController.view.frame = CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height-100);

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;

我的ViewController是FreeForm,它的高度刚好在245左右。如您所见,我正在尝试放置一个高度为100点的底 View 。

我只是想学习这一点。

但是 View 总是充满整个屏幕。如果我注释掉将 View Controller 设置为Windows rootviewcontroller,则可以在屏幕上看到我的底 View 。

我做错了什么?请指教。

谢谢。

最佳答案

代替

self.window.rootViewController = self.viewController;

当我编码
[self.window addSubview:self.viewController.view];

给了我预期的结果,它决定了自由格式的大小。

设置为rootviewcontroller时,它会出现,它将填满整个可用屏幕,而不管XIB上设置的自由格式大小如何。

关于ios - 除了rootviewcontroller之外,无法向uiwindow添加其他 subview ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14784896/

10-13 04:07