IBDesignable UIViewController
Asked Answered
J

2

12

I'd like to be able to layout my view controller in code but see the layout displayed in interface builder.

I know I can create a UIView subclass, make that IBDesignable, and assign it to the view controller's view, but this would require that I make all other subviews properties of this UIView subclass instead of properties of the view controller.

The real desire is to be able to layout my view controllers in code but quickly see any changes without rebuilding the project. If this is possible with playgrounds instead, an answer describing how to do that would also be appreciated.

Thanks for any suggestions.

Jehial answered 6/11, 2014 at 23:6 Comment(0)
J
1

I found a workaround to test view controller layout using IBDesignable.

1.Layout your view controller in code just as you'd do normally

2.Create an IBDesignable UIView subclass and add the view controller's view as a subview.

3.Create a xib and set the class of its view to the subclass you created in step 2

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

{
    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.

Jehial answered 14/12, 2015 at 11:6 Comment(0)
O
-1

In order to layout you own classes in Xcode you need first to import then in your swift playground: here more information.

After you do that, it's came the "tricky" part. In order to make your how class debuggable and visibile in playground, your class must conform to the protocol: CustomPlaygroundQuickLooacable:

Here there is a quick example from the WWDC:

By implementing this protocol, you're basically telling playground how to represent you hown class. I haven't fond any better solution yet.

Overture answered 13/6, 2015 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.