I am new to iOS development. I am using swift and I want to design my views programmatically but I would like to use IBDesigner and IBInspectable to speed up my design process.
Right now I have a view controller with various buttons and labels on it. 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()
}
My question now is how do I use the interface builder with IBDesignable and IBInspectable? Do I need to add a .xib? I saw some other questions mentioning that I need to use a playground, but I was trying to avoid using that, i essentially wanted to know if it is possible to view my entire viewcontroller?
Thank you,
IBDesignable is user defined run time. Mean that if any IBDesignable view is rendered on the storyboard, you will find the view and change the value of the IBInspectable property, this will directly reflect on the storyboard without running the app. IBInspectable is run time property, this works as key coding value.
@IBInspectable allows us to create attributes in code that we can assign in a storyboard or a . xib file. For example, when we want a cornerRadius property available in a Storyboard, we create a cornerRadius property inside our custom view and mark it with @IBInspectable .
You can not use IBDesignable with another class that not inherit from UIView or NSView. Check the Guideline by the Apple about the IBDesignable:
You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. When you create a custom view that inherits from the UIView class or the NSView class, you can add the @IBDesignable attribute just before the class declaration
Source: Live Rendering
a
There is a workaround to test out your view controller layout using IBDesignable.
To elaborate on step 2, your IBDesignable's 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;
}
So beyond this initWithFrame method, all of your layout code can be handled by your view controller. You may want to pass data to your view controller in your subview's prepareForInterfaceBuilder method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With