简体   繁体   中英

Add Storyboard as subview in UIViewController

I am creating a reusable view for which I have created a separate Storyboard . This reusable view will be added in different UIViewController is Main.StoryBoard . For this I followed storyboard reference. But I am not sure how to add Storyboard as a subview inside a UIViewController having some definite frame.
Please let me know if you want to have a screenshot as to how I am designing the screen.

first you need to get the reference of the storyboard from the different storyboard.

UIViewController *viewControllerToAdd =
    [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]instantiateViewControllerWithIdentifier:@"viewForPopover"];

//Instead of MainStoryboard you write the name of the Storyboard which you have created separately

the you write these lines

[self addChildViewController:viewControllerToAdd];
[self.view addSubview:viewControllerToAdd.view];
[viewControllerToAdd didMoveToParentViewController:self]; 

This much should probably do, i havent tried it yet

make reusable UIViewController and then addChildViewController wherever you want. Below i made RemarksViewController which i used in many views of UIViewController

    RemarksViewController *vC=[[UIStoryboard storyboardWithName:@"yourStoryboardName" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"showRemarks"];
    [self addChildViewController:vC];
    vC.view.bounds=self.view.bounds;
    [self.view addSubview:vC.view];
    [vC didMoveToParentViewController:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM