Using IBDesignable and IBInspectable with UIViewController

I am new to iOS development. I use swift and I want to programmatically develop my views, but I would like to use IBDesigner and IBInspectable to speed up the development process.

Now I have a view controller with various buttons and labels. Here is a snippet of my code:

@IBDesignable class LandingView: UIViewController, LandingViewProtocol { @IBInspectable lazy var backButton: UIButton = { var button = UIButton() button.backgroundColor = Styles.BUTTON_COLOR return button }() @IBInspectable lazy var caption: UILabel = UILabel() } 

Now my question is how to use the interface constructor with IBDesignable and IBInspectable? Do I need to add .xib? I saw some other questions that mentioned that I needed to use the playground, but I tried to avoid this, I, in fact, wanted to know if it is possible to view my entire view manager?

Thanks,

+8
ios uiviewcontroller swift
source share
2 answers

There is a workaround for checking the schema of your view controller using IBDesignable.

  • Place your view controller in code the same way you would normally.
  • Subclass the IBDesignable UIView and add a view controller view as a subtask.
  • Create xib and set the class to its representation for the subclass created in step 2

To elaborate on step 2, your IBDesignable initWithFrame: method may look like

 - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; MyViewController *viewController = [MyViewController alloc] init]; viewController.view.frame = self.bounds; [self addSubview:viewController.view]; return self; } 

Thus, outside this initWithFrame method, all of your layout code can be processed by your controller. You might want to pass data to your view controller in your subview prepareForInterfaceBuilder method.

+3
source share

You cannot use IBDesignable with another class that does not inherit from UIView or NSView. Check out the Apple Guide for IBDesignable:

You can use two different attributes: @IBDesignable and @IBInspectable - inclusion of interactive user design in real time in the Builder Interface. When you create your own view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute before the class declaration

Source: Live Rendering

but

+3
source share

All Articles