Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIView outside of a View Controller in a Storyboard

I'm stuck with a question I can't find an answer to in the official documentation, on Stackoverflow or on Google.

I'm an experienced iOS developer but new to Storyboards. I'd like to instantiate a view on the current viewcontroller, a view that, in a NIB, I would draw outside the viewController and attach an outlet to to refer to it in code and call something like addSubview: or so. It's useful to draw an overlay or, in my current case, a callout view for use in a MapView delegate. But I can't figure out how I can do this in a Storyboard.

I can't seem to have a view outside of a viewcontroller, and views inside the viewcontroller either have to be subviews of the controller's view or don't be visible in Interface Builder at all (which is okay when you drag a view composed outside, in a traditional NIB, but quite a problem if you need to tweak even the least detail of it).

Any hint on what to do or where to look? Thanks.

like image 991
KPM Avatar asked Jul 08 '12 19:07

KPM


People also ask

What is the difference between UIView and UIViewController?

They are separate classes: UIView is a class that represents the screen of the device of everything that is visible to the viewer, while UIViewController is a class that controls an instance of UIView, and handles all of the logic and code behind that view.

How do I add a ViewController to a storyboard?

To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.


2 Answers

It appears there is no way to do it. See Jason's comment and this question addressing the same problem, though in a different context.

However, I realised earlier when thinking about the implications of my problem — as I began to explain in my previous comment before deciding it would be worth a full answer — it makes sense to separate the design of a custom view from the design of an application flow. So the fact that it's not possible to design custom views in Storyboards may very well be... by design.

A custom view (which is a reusable component) is very different from a view controller (which represents a scene in your application storyboard). It's the same as a character and a scene, in a movie. You don't want to design your character in your scene, even if you happen to use that character in only one scene. It's fortuitous if you use it in only one scene, but not essential. So it has to be separate.

The fact that you want to use Interface Builder to design your custom view is only an implementation detail that should be encapsulated in your class. The application storyboard has no need of knowing, even less managing, how you deal with the particular implementation of your reusable view.

Hmm, I wish I had more of this brand of interrogation :)

like image 96
KPM Avatar answered Sep 27 '22 23:09

KPM


You can use a Container View, is like a regular view(You can assign a IBOutlet UIView) but you can define it outside of your UIViewController.

The "container view" is outside the view, but i can assign it to a IBOutlet UIViewYou can design it

like image 32
Josejulio Avatar answered Sep 28 '22 01:09

Josejulio