简体   繁体   中英

If I'm using Storyboards and want to present one view atop all my others occasionally, how do I build it?

I have my normal view controller, but once in awhile I'll present something of an options screen atop the view controller. I don't know how to set this up in a Storyboard though, as if I had it on top and then set hidden to true, it would obstruct all the other views and be rather annoying to fiddle around with.

What should I be doing in this case?

You can create the view in a separate XIB file, and then load the view from your view controller programmatically. If this view is showed often you can keep the view in a property, otherwise you can just load it from the XIB every time. To load a view contained in a XIB:

UIView *view = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] firstObject];
[self.view addSubview:rootView];

If this view is something like a settings, I'd recommend to use a separate view controller. You could use a Storyboard and then programmatically add it as a child view controller.

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MyStoryboard" bundle:nil];
UIViewController *vc = [sb instantiateInitialViewController];
[self addChildViewController:vc];
content.view.frame = self.view; // here you set the frame of your view
content.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
[self.view addSubview:vc.view];
[vc didMoveToParentViewController:self];

Are you trying to create a view that is smaller than the view controller? If that's the case then you can create one programmatically by sub classing UIView. But if it will cover your entire view controller then you can create a view controller with a .xib file. You can then draw everything on it using IB and then call that view controller morally.

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