How to add a subview that has its own UIViewController in Objective-C?

前端 未结 2 575
失恋的感觉
失恋的感觉 2020-11-21 23:03

I am struggling with subviews that have their own UIViewControllers. I have a UIViewController with a view (light pink) and two buttons on a

2条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 23:23

    I see two problems. First, since you're making the controllers in the storyboard, you should be instantiating them with instantiateViewControllerWithIdentifier:, not initWithNibName:bundle:. Second, when you add the view as a subview, you should give it a frame. So,

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        self.aboutVC = [self.storyboard instantiateViewControllerWithIdentifier:@"aboutVC"]; // make sure you give the controller this same identifier in the storyboard
        [self addChildViewController:self.aboutVC];
        [self.aboutVC didMoveToParentViewController:self];
        self.aboutVC.view.frame = self.utilityView.bounds;
        [self.utilityView addSubview:self.aboutVC.aboutView];
    }
    

提交回复
热议问题