ios - 在 ios 中添加 View Controller 作为 subview

标签 ios objective-c ios7 ios8

嗨,我正在尝试添加 view controller作为 child view .然后删除这个 view controller形成父 View 。我为此目的使用以下代码。

 self.loginView = [self.storyboard instantiateViewControllerWithIdentifier:@"LOGIN"];
 [self.view addSubview:self.loginView.view];

此代码适用于 iOS8但在 iOS7此代码不起作用,它显示了屏幕的一半。显示了一半登录。

这可能是什么解决方案?

最佳答案

添加自定义 UIView您要在其中添加和显示 subview Controller 的主视图(在 XIB 中)中的对象。让 contentView是该 View 的名称。使用以下代码添加 subview 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]; 

如果您不添加最后一行,您的 subview Controller 将不会收到事件。通过使用此代码,您可以同时在父 View Controller 和 subview Controller 中接收事件。

关于ios - 在 ios 中添加 View Controller 作为 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222383/

相关文章:

ios - 处理 NSXmlParser 中的自关闭标签?

ios - 应用程序:didRegisterForRemoteNotificationsWithDeviceToken: not called ios 10. 3.2

ios - UIAlertView 处理文本字段

iphone - 将所有日历日期插入到 uitableview 中。

ios - Swift 中的 UIView.animateWithDuration - iOS7 问题

html - NSAttributedString 使用图像加载 html

ios - Opengles着色器插值问题

ios - UITextContainerView 隐藏了 UITextView 的可点击链接

iphone - iOS 5—— "self"中的 presentModalViewController nils parentViewController?

uitableview - ios7 - UITableViewCell 内置 UIPickerView 和 UIDatePicker