Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to 'design' a custom UIView in storyboard, and use in multiple ViewControllers without adding to any specific controller?

I'm in need of a custom UIView which is to be used in multiple different ViewControllers. Usually when I create a custom UIView, I drag an instance of UIView into my ViewController in storyboard, create a subclass of UIView, and point to this subclass in the Identity Inspector for the view. From there, I would connect the UI-objects as outlets to the header-file of the subclass.

The view I want to make now, is not supposed to be a part of any specific controller, but should be added to any controller that asks for it. A rogue UIView. I know I can create the entire UIView programmatically, and just create an instance of it, but I'd like to have it (and design it) in my storyboard.

In Storyboard, the only objects I'm allowed to drag 'outside a ViewController', are other ViewControllers.

I have never used anything other than Storyboard for iOS-developing, but I have come over tutorials which have been using the other mode, from the olden days, which looks like what I need. Is it possible to get something similar into my storyboard? Or would this require its own setup/design? If so, how?

I was also thinking of solving this with adding a 'phantom' UIViewController containing my custom View; designing it there, but instantiate it from the other controllers, but this sounds so hacky..

I'd like to do this with a UITableViewCell as well, but I guess that would have the same solution.

like image 212
Sti Avatar asked Dec 04 '13 08:12

Sti


People also ask

Can we use multiple storyboards in one application?

A storyboard is meant to explain a story, not a saga. An app's storyboard can be easily divided into multiple storyboards, with each one representing an individual story.

How do I add a custom view to storyboard?

Using a custom view in storyboardsOpen up your story board and drag a View (colored orange below for visibility) from the Object Library into your view controller. Set the view's custom class to your custom view's class. Create an outlet for the custom view in your view controller.

How create custom UIView in programmatically in Swift?

Creating Custom Views programatically Open Xcode ▸ File ▸ New ▸ File ▸ Cocoa Touch class ▸ Add your class name ▸ Select UIView or subclass of UIView under Subclass of ▸ Select language ▸ Next ▸ Select target ▸ Create the source file under your project directory.

How do I add a custom view in ViewController?

Open the storyboard. Drag and drop a Custom View onto the view controller scene. Under the identity inspect, set the class name to TestView.


2 Answers

For your UIView, you should be creating a custom UIViewController in your storyboard and instantiate the view controller to access the view:

MyViewController *myViewController = [self.storyboard  instantiateViewControllerWithIdentifier:@"MyViewController"];
// Access the view using "myViewController.view"

This is a very common practice in iOS since storyboards were presented. If you are using multiple storyboards, you should create a new instance of UIStoryBoard with:

UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@"StoryboardName" bundle:nil];

And then instantiate the view controller with this instance of the storyboard.

If you want to solely use a UIView, you should be creating a .xib file, i.e. the olden days format. To create a custom UITableViewCell, I would absolutely use a .xib file. Your last option would be to create a UIView programmatically, which could be called from any place in your application. Depending on the complexity of this view, this may be a valid option.

like image 84
Chris Avatar answered Sep 21 '22 07:09

Chris


I don't think you can create a custom UIView in storyboard. So in this case, you can create a xib file for that custom view. Then when you want use that view, just use

UIView *customView = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:self options:nil] objectAtIndex:0]; 

to get that view.

like image 40
Eric Qian Avatar answered Sep 20 '22 07:09

Eric Qian