简体   繁体   English

添加故事板视图作为以编程方式创建的视图的子视图

[英]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.

相关问题 如何以编程方式在情节提要子视图后面添加子视图 - how to add subview programmatically behind storyboard sub view iOS:如何以编程方式将视图添加到情节提要视图 - iOS: How to programmatically add a view to a storyboard view Swift-以编程方式将自定义Xib视图添加为子视图 - Swift - Add Custom Xib View As Subview Programmatically 以编程方式加载UITableViewController并将视图添加为子视图 - Load UITableViewController programmatically and add view as subview 无法以编程方式将视图添加到情节提要中的现有子视图 - Can't add a view to an existing subview in storyboard programatically 如何在没有任何延迟的情况下将UIView作为子视图添加到Storyboard的视图? - How to add an UIView as a subview to Storyboard's view without any delay? 以编程方式显示Storyboard视图 - Programmatically displaying Storyboard view 将在故事板内部创建的视图控制器作为另一个视图控制器的子视图(进入屏幕的一部分)加载 - Load a view controller created inside a storyboard as a subview (into a part of the screen) of another view controller 情节提要集合视图拒绝进入以编程方式创建的按钮 - Storyboard collection view refusing to go in front of programmatically created button 在Storyboard中创建的TabbarController无法以编程方式更改视图 - Created TabbarController in Storyboard can't change view programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM