[英]Add a storyboard view as a subview of a programmatically created view
I have created a special class of UIView that has certain properties, and I did so programmatically because it is usually blank but will at times contain other views. 我创建了一个特殊的UIView类,它具有某些属性,我是以编程方式编写的,因为它通常是空白的,但有时会包含其他视图。 I know that if I create a UIView programmatically I can do something like
[specialView addSubview: aView];
我知道如果我以编程方式创建一个UIView,我可以做一些像
[specialView addSubview: aView];
My problem is that there is a UIView that I created in storyboard and I need to add that UIView as a subview on my special view. 我的问题是我在故事板中创建了一个UIView,我需要在我的特殊视图中添加UIView作为子视图。 I have connected it as a property in my ViewController but when I do the same code as above, nothing happens.
我已经将它作为我的ViewController中的属性连接但是当我执行与上面相同的代码时,没有任何反应。 Thoughts?
思考?
MyViewController *myViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"MyScene"];
[specialView addSubview:myViewController.theViewToAdd];
And don't forget to give such an identifier to your scene (view controller) in Interface Builder. 并且不要忘记在Interface Builder中为场景(视图控制器)提供这样的标识符。
Another solution is you can add the viewcontroller as a childviewcontroller. 另一种解决方案是您可以将viewcontroller添加为childviewcontroller。
func displayContentController(content: UIViewController) {
addChildViewController(content)
self.view.addSubview(content.view)
content.didMoveToParentViewController(self)
}
To remove : 去除 :
func hideContentController(content: UIViewController) {
content.willMoveToParentViewController(nil)
content.view.removeFromSuperview()
content.removeFromParentViewController()
}
That UIViewController which houses the view you want may not be instantiated. 容纳您想要的视图的UIViewController可能无法实例化。 So you would need to instantiate that controller and grab the view from there.
因此,您需要实例化该控制器并从那里获取视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.