在ios中添加视图控制器作为子视图 [英] adding view controller as child view in ios
问题描述
您好我正在尝试添加视图控制器
作为子视图
。然后删除此视图控制器
表单父视图。我正在使用以下代码。
Hi i am trying to add a view controller
as a child view
. and later remove this view controller
form parent view.I am using following code for this purpose.
self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self.view addSubview:self.loginView.view];
此代码适用于 iOS8
但在 iOS7
此代码无效,显示屏幕的一半。显示半部分登录。
This code works fine for iOS8
but in iOS7
this code is not working it shows the half of the screen.On half part login is shown.
什么可以解决这个问题??
What could be the solution for this??
推荐答案
添加自定义 UIView
主视图中的对象(在XIB中),您要在其中添加并显示子视图控制器。让 contentView
是该视图的名称。使用以下代码添加子视图控制器:
Add a custom UIView
object in your main view (in XIB) in which you want to add and show your child view controller. Let contentView
is the name of that view. Use following code to add child view controller:
self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
[self addChildViewController:self.loginView];
[self.loginView.view setFrame:CGRectMake(0.0f, 0.0f, self.contentView.frame.size.width, self.contentView.frame.size.height)];
[self.contentView addSubview:self.loginView.view];
[self.loginView didMoveToParentViewController:self];
如果你不添加最后一行,你的子视图控制器将不会收到事件。通过使用此代码,您可以同时在父视图控制器和子视图控制器中接收事件。
if you don't add last line, your child view controller will not receive events. By using this code you can simultaneously receive events in both parent and child view controllers.
这篇关于在ios中添加视图控制器作为子视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!