KPM KPM - 1 month ago 18
iOS Question

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.


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 :)