简体   繁体   English

在UIViewController中使用IBDesignable和IBInspectable

[英]Using IBDesignable and IBInspectable with UIViewController

I am new to iOS development. 我是iOS开发的新手。 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. 我正在使用swift,我想以编程方式设计我的视图,但我想使用IBDesigner和IBInspectable来加速我的设计过程。

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? 我现在的问题是如何使用IBDesignable和IBInspectable的界面构建器? Do I need to add a .xib? 我需要添加.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? 我看到其他一些问题提到我需要使用游乐场,但我试图避免使用它,我本来想知道是否可以查看我的整个viewcontroller?

Thank you, 谢谢,

You can not use IBDesignable with another class that not inherit from UIView or NSView. 您不能将IBDesignable与另一个不从UIView或NSView继承的类一起使用。 Check the Guideline by the Apple about the IBDesignable: 查看Apple关于IBDesignable的指南:

You can use two different attributes—@IBDesignable and @IBInspectable—to enable live, interactive custom view design in Interface Builder. 您可以使用两个不同的属性 - @ IBDesignable和@ IBInspectable - 在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 创建从UIView类或NSView类继承的自定义视图时,可以在类声明之前添加@IBDesignable属性

Source: Live Rendering 来源: 实时渲染

a 一种

There is a workaround to test out your view controller layout using IBDesignable. 有一种解决方法可以使用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. 创建IBDesignable UIView子类并将视图控制器的视图添加为子视图。
  3. Create a xib and set the class its view to the subclass you created in step 2 创建一个xib并将类视图设置为您在步骤2中创建的子类

To elaborate on step 2, your IBDesignable's initWithFrame: method may look like 要详细说明第2步,您的IBDesignable的initWithFrame:方法可能看起来像

- (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. 因此,除了这个initWithFrame方法之外,您的视图控制器还可以处理所有布局代码。 You may want to pass data to your view controller in your subview's prepareForInterfaceBuilder method. 您可能希望在子视图的prepareForInterfaceBuilder方法中将数据传递给视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用@IBDesignable和@IBInspectable在UIView中自定义此图像选项卡更好 - How to custom this image tab in UIView,using @IBDesignable and @IBInspectable is better IBDesignable视图中的IBInspectable按钮 - IBInspectable button inside IBDesignable view Swift @ IBDesignable / @ IBInspectable UIView样式 - Swift @IBDesignable/@IBInspectable UIView styling Swift @IBDesignable - @IBInspectable多个变量 - Swift @IBDesignable - @IBInspectable multiple variables iOS 中的 IBDesignable 和 IBInspectable 到底有什么区别? - What exactly is the difference between IBDesignable and IBInspectable in iOS? IBInspectable / IBDesignable的Swift多继承解决方案? - Swift multiple inheritance solution for IBInspectable/IBDesignable? 我如何使用@IBDesignable和@IBInspectable创建uitextfield的自定义子类,并且可以在左侧设置placholder padding - How can I make a custom subclass of uitextfield and over there using @IBDesignable & @IBInspectable I can set placholder padding from left 快速将自定义@IBDesignable视图的故事板指南标题设置为@IBInspectable值? - Set the Storyboard guide title to a @IBInspectable value for a custom @IBDesignable view in swift? 约束更新图层后,调用@IBDesignable中设置的@IBInspectable - Call @IBInspectable set in @IBDesignable after layer is updated by constraints 同时使用@objc和@IBDesignable - Using both @objc and @IBDesignable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM