Re-using complex custom UI elements - Xcode Storyboard - swift

In Xcode on the StoryBoard I built a complex UI element consisting of many sub-views.
I encapsulated it all into a UIView subclass but each element has to be built out on the storyboard wherever it is used.
Is there a way to build it in such a way that all a developer would need to do to use it is to add a UIView of the appropriate sub-class and not need to layout all of the subviews? Similar to how we do not need to interact with the underlying structure of buttons or labels.

This is possible, but it requires a bit of a workaround. It basically requires you to create a UIView subclass, which loads all it's subviews from a separate nib file
This question has the answers for you.

Related

How to create custom View without subclassing from UIView

I'm hoping someone could help me.
I'm wondering if it was possible to create a custom View without subclassing from UIView. The reason being I wish to have just the bare minimum of functions that I need without all the other clutter that you find in UIView.
Is this possible or am I strictly bound to making all possible Subclasses I wish to create come from UIView? If this is the case is it possible for me to hide some of the functions and attributes in UIView from outside observers and only reveal what I need revealed?
I may be completely misinterpreting your question, but creating a subclass of UIView allows you to edit your view specifically to what you want with no required methods.
For example:
class CustomView: UIView {
//Your custom view code here
}
Can be implemented with complete customizability.
Perhaps you are referring to an extension of UIView? You can even use an extension of CustomView at this point. I guess it is very unclear what you are asking.
You can design the view in storyboard and have the code in viewController. Depends on how you want to design your program and if you want to reuse your custom view.

Convert iPhone app to universal app: Grouped UITableView

I am using xib-s for my UITableViewCells on iPhone. Now I need to convert my app to universal app. Do I have to create new xib-s (looking exactly the same way) for the iPad version? My current problem is that I am using grouped UITableViews. As the left and right margins on both devices differ, some of the controls on iPad are partly "out of the cells".
In case I need new xib-s (looking exactly the same way), do I need separate properties for the controls in the ViewController? For example, for a label named myLabel, do I need a second property myLabelIpad or is there a better way to handle that?
Cheers
My suggestion is to create new xib. But if there are small changes , then you can maintain one xib. However it depends on how different your iPhone / iPad versions are. For example, if the iPad version is just a bigger version of the iPhone one with a few extra buttons, etc, it's easier to use one UIView and just set the frames of the subviews appropriately.
First understand what you are up to. How do you want your iPad app to look like? How does it navigate. Ceratinly you want to leverage from the lager screen of the ipad. When ever you do that and the auto-resizing mechanism is not sufficient (which it rarely is) then go for a separated xib file.
Second - for each XIB which hosts the same number and types of UIView and UIConrol subclasses, you should be able to use the same view controller for two separate XIBs. Just link all conrols within both XIB to the related IBOutlet properties and IBAction methods alike.
However, when you take leveraging from iPad capabilites seriously then you are like to end up with a different set of view controllers. That is when you can combine the controls of several iPad views wihin one single iPad view and similar cases.
Nevertheless, think of using popups. The content of a popup could nicely correlate to what is a full screen on the iPhone. In that case you can use the same view controller again within that popup container.
Does this sort of answer your question? If not, then please be more specific.
Actually you can retain the same Table cell xib files. You just have to get the Auto resizing masks of the cell and its subviews right.

How do I make a custom UIView accessible?

I have a custom UIView subclass that contains a grid of cells, each of which are also custom UIView subclasses.
I'm interested in using the Keep It Functional test framework, which requires that every view have an acccessibilityLabel.
How do I configure the cell classes to have accessibility labels, so I can refer to them individually in my tests?
I think my autocomplete was lying to me. All I had to do was:
[gridCell setIsAccessibilityElement:YES];
[gridCell setAccessibilityLabel:[NSString stringWithFormat:#"cell-%d", cellIndex]];

Does storyboard eliminate the need for .nib

With the introduction of storyboard, I wouldn't select to create a .xib/.nib when I create a subclass of UIViewController, because I can just drag out a viewcontroller in interface builder and assign it to the new class.
So, with storyboard, when would i need to use the .xib/.nib file?
Thank you.
Storyboards don't completely remove the need for NIB files. One common example is when creating subviews to go inside a UIScrollView. You can't create the subviews separately in a storyboard; instead you have to create separate NIB(s) for the subviews and attach them to the scroll view programatically.
In fact, on almost any occasion where you have a need for subviews which change at runtime you'll want to use separate NIBs.
Storyboards are great for certain kinds of apps, but for anything even remotely complicated the old way works very well still. An additional benefit is that they co-exist nicely. You can put placeholder views in your storyboard and fill them programatically with NIB-defined views later on.
I guess, storyboard contains the .xib/.nib files for all your views. It present relationships between them and helps you to avoid confusion if you have a LOT of views. Also it saves your time and nerves when writing code.
I have tried out storyboarding lately and I do have mixed feelings about its use:
Pro's
Convenience: You might have to write much less code. So you can for example show a static view without having to write any code.
Overview: You can quickly see how the ViewControllers relate to each other
Efficiency: For the simple use cases I find the Storyboarding much more efficient than "the old way".
Example 1: Editor > Embed In > Navigation Controller and voilĂ , no more instantiating and configuring is required.
Example 2: You can make "Prototype Cells" for TableView which speeds up the creation of static table views dramatically. AFAIK this is not possible with nib files
It's cool :-)
Con's
I find that the re-usability somehow suffers with storyboards. Although I can segue to a given ViewController from multiple points, it is still limited since in one case I might use the VC as a Popover and in another as a Master-ViewController.
Assuming that you are working in a big team and different people are changing different parts of your application, having one monolithic storyboard which you then have to merge might pose a big problem team-work-wise.
I really would like to have some mechanism to include or reference existing nib files into the storyboard which is currently not possible.
So considering these points I still find storyboarding very appealing but it makes sense to combine both approaches which is not really a big deal. Additionally, this technology it is still quite new so it might improve in the (near) future.
If you use storyboards, you generally won't need to create separate .xib files. You can still use a .xib file if you want to -- it might be useful e.g. if you have a particularly complex view that you'd like to configure by itself. Most of the time, though, there won't be a need to do that.

How to reuse common subelements of a custom UITableViewCell with iOS Interface Builder?

I have several custom UITableViewCell, which share a lot of common subelements.
I tried creating a UIView and using it as a subview for all the table cells, but obviously this wouldn't work, since entities in IB are object instances, not classes or templates.
How can I capture these common subelements in a custom control and drag it into all table cells? Ideally it would be a solution where one doesn't have to muck around with programmatic Nib loading and whatnot.
I think the "easiest" answer would be to create your CustomSubview class with the common, shared elements programmatically, and then in IB add a UIView subview and give it the class of CustomSubview. You'll have to write the subview in code, but you will still be able to reuse it in your various xibs.