Uiview u:使用UINib(nibName:nibName,bundle:nil)。实例化所有者(nil,options:nil),这比NSBundle版本快。@Garfbargle这似乎满足了您可以在IB中设计并在情节提要中呈现的要求。基本上,您完全按照为XIB

Uiview u:使用UINib(nibName:nibName,bundle:nil)。实例化所有者(nil,options:nil),这比NSBundle版本快。@Garfbargle这似乎满足了您可以在IB中设计并在情节提要中呈现的要求。基本上,您完全按照为XIB,uiview,autolayout,interface-builder,uistoryboard,xib,Uiview,Autolayout,Interface Builder,Uistoryboard,Xib,u:使用UINib(nibName:nibName,bundle:nil)。实例化所有者(nil,options:nil),这比NSBundle版本快。@Garfbargle这似乎满足了您可以在IB中设计并在情节提要中呈现的要求。基本上,您完全按照为XIB所做的操作,并将@IBDesignable放在类上。您还必须实现init(frame:),但除此之外,它工作得非常好!谢谢您可能希望添加一个使用Swift约束的示例,正如您在Obj-C答案(有和没有VFL)中所做的那样。我回答了自己的问题,看起


u:使用
UINib(nibName:nibName,bundle:nil)。实例化所有者(nil,options:nil)
,这比NSBundle版本快。@Garfbargle这似乎满足了您可以在IB中设计并在情节提要中呈现的要求。基本上,您完全按照为XIB所做的操作,并将
@IBDesignable放在类上。
您还必须实现
init(frame:)
,但除此之外,它工作得非常好!谢谢您可能希望添加一个使用Swift约束的示例,正如您在Obj-C答案(有和没有VFL)中所做的那样。我回答了自己的问题,看起来我在阅读这些SO文章之前设置了出口,目的是设置文件所有者,而不是视图。在我断开所有插座的连接,确保文件所有者正确,然后重新添加所有插座后,它就工作了。设置文件所有者和自定义类之间有什么区别?我在界面生成器中也没有看到预览。Xcode 9代码在运行时可以工作,但在我的Xcode 9中,它在模块行下显示“可设计构建失败”。谢谢!你可能想为那些不想使用VFL的人添加一个不使用VFL的示例。我无法让它在Xcode8/Swift3中工作。运行应用程序时工作正常,但不会显示在情节提要中。对我来说工作正常。如果您试图查看xib上的更改,它将不会显示。在其他控制器内添加一个视图,然后将该视图的类设置为
DesignableXibView
。构建项目以查看更改。对我也很有用。只需将DesignableXibView添加到我当前的故事板中,如Garfbargle postJust me在运行时获取“无法加载捆绑包中的NIB”的步骤5所述?@Jonny我认为
String(description:type(of:self))
应该可以安全地完成此操作
- (id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {

        // 1. load the interface
        [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
        // 2. add as subview
        [self addSubview:self.view];
        // 3. allow for autolayout
        self.view.translatesAutoresizingMaskIntoConstraints = NO;
        // 4. add constraints to span entire view
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|" options:0 metrics:nil views:@{@"view":self.view}]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|" options:0 metrics:nil views:@{@"view":self.view}]];

    }
    return self;
}
required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        UINib(nibName: String(self.dynamicType), bundle: NSBundle.mainBundle()).instantiateWithOwner(self, options: nil)
        self.addSubview(view)
        self.view.translatesAutoresizingMaskIntoConstraints = false
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[view]|", options: NSLayoutFormatOptions.AlignAllCenterY , metrics: nil, views: ["view": self.view]))
        self.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[view]|", options: NSLayoutFormatOptions.AlignAllCenterX , metrics: nil, views: ["view": self.view]))
    }
//  DesignableXibView.swift

import UIKit

@IBDesignable

class DesignableXibView: UIView {

    var contentView : UIView?

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    func xibSetup() {
        contentView = loadViewFromNib()

        // use bounds not frame or it'll be offset
        contentView!.frame = bounds

        // Make the view stretch with containing view
        contentView!.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]

        // Adding custom subview on top of our view (over any custom drawing > see note below)
        addSubview(contentView!)
    }

    func loadViewFromNib() -> UIView! {

        let bundle = NSBundle(forClass: self.dynamicType)
        let nib = UINib(nibName: String(self.dynamicType), bundle: bundle)
        let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView

        return view
    }

}
    import UIKit

@IBDesignable

class DesignableXibView: UIView {

    var contentView : UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        xibSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        xibSetup()
    }

    func xibSetup() {
        contentView = loadViewFromNib()

        // use bounds not frame or it'll be offset
        contentView.frame = bounds

        // Make the view stretch with containing view
        contentView.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

        // Adding custom subview on top of our view 
        addSubview(contentView)
    }

    func loadViewFromNib() -> UIView! {

        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

        return view
    }
} 
public partial class CustomView : UIView
    {
        public ErrorView(IntPtr handle) : base(handle)
        {

        }

        [Export("awakeFromNib")]
        public override void AwakeFromNib()
        {
            var nibObjects = NSBundle.MainBundle.LoadNib("CustomView", this, null);
            var view = (UIView)Runtime.GetNSObject(nibObjects.ValueAt(0));
            view.Frame = Bounds;
            view.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;

            AddSubview(rootView);
        }
    }
#import <UIKit/UIKit.h>

IB_DESIGNABLE
@interface BottomBarView : UIView

@end
#import "BottomBarView.h"

@interface BottomBarView() {
    UIView *contentView;
}

@end


@implementation BottomBarView

-(id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self xibSetup];
    }
    return self;
}

-(id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self xibSetup];
    }
    return self;
}

-(void) xibSetup {
    contentView = [self loadViewFromNib];

    contentView.frame = self.bounds;

    contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [self addSubview:contentView];
}

-(UIView*) loadViewFromNib {
    NSBundle *bundle = [NSBundle bundleForClass:self.classForCoder]; //this is the important line for view to render in IB
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([self class]) bundle:bundle];
    UIView *view = [nib instantiateWithOwner:self options:nil][0];

    return view;
}

@end