0

I'm trying to figure out what the equivalent of 'custom controls' from visual-studio-land are, in Xcode. I want a custom re-usable control that's just a text box beside a segmented control with some custom behavior thrown in, but I don't know how to make it.

I've found articles on making a totally custom control, with its own rendering, but that's massive overkill. I want to combine existing controls, not make totally new ones.

I've tried creating a new 'View' and using that as the custom control, putting a text box and a checkbox on it. But that view doesn't show up in the list of things I can drag onto other views (where the buttons and sliders and etc are listed).

I can make a blank view inherit from my custom view using the 'Custom Class' pane in the right-hand window, but the inheriting view continues to just be... blank. Shouldn't the underlying controls be drawn in the designer, and shouldn't I be able to twiddle exposed properties?

I know this is trivial, I just don't know what trivial thing I have to do to make it work.

Craig Gidney
  • 17,763
  • 5
  • 68
  • 136

1 Answers1

1

Your custom control will need a custom .m and .h file. If you want to use interface builder, it will also require a .xib; see questions like this. (Creating a custom view using a xib)

Alternatively, you could forgo the xib and do the setup of your views programatically, in viewDidLoad:. If you haven't been doing this so far, now could be a good opportunity to get your feet wet; it's an important tool.

As for specifying your custom views from within xCode, the identity inspector in interface builder lets you specify a view's class; you can select a custom class from the drop down, which will be loaded in place of the default view when the storyboard is loaded.

Apple actually recommends building custom controls by bundling together predefined controls, and they go over this in one of the WWDC talks... I think 'Advanced Appearance Customization on iOS', from 2012. That might be a good place to start.

Community
  • 1
  • 1
cmyr
  • 2,235
  • 21
  • 30