7

我使用 stroyboard 从 xib 创建预览,但出现以下错误:

file:///Users/miaios/Desktop/Demo/XibDemo/XibDemo/Base.lproj/Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent raised a "NSInternalInconsistencyException" exception: Could not load NIB in bundle: 'NSBundle </Users/miaios/Library/Developer/Xcode/DerivedData/XibDemo-hkqfrktawlqpokczkfobsrmkfxqy/Build/Intermediates/IBDesignables/Products/Debug-iphonesimulator/XibDemo.app> (loaded)' with name 'HXXibView'

file:///Users/miaios/Desktop/Demo/XibDemo/XibDemo/Base.lproj/Main.storyboard: error: IB Designables: Failed to render instance of HXXibView: The agent threw an exception.

代码:

#import <UIKit/UIKit.h>

IB_DESIGNABLE

@interface HXXibView : UIView

@property (nonnull, nonatomic, strong) IBInspectable NSString *text;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

@implementation HXXibView

#pragma mark - Init Methods
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self xibSetup];
    }
    return self;
}

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

- (void)setText:(NSString *)text {
    _label.text = text;
}
- (NSString *)text {
    return _label.text;
}


#pragma mark - Private Methods
- (void)xibSetup {
    UIView *view = [self loadViewFromNib];
    view.frame = self.bounds;
    [self addSubview:view];
}

- (UIView *)loadViewFromNib {
    NSBundle *bundle = [NSBundle bundleForClass:[HXXibView class]];
    UINib *nib = [UINib nibWithNibName:NSStringFromClass([HXXibView class]) bundle:bundle];
    UIView *view = [[nib instantiateWithOwner:self options:nil] firstObject];
    return view;
}

@end

Xib按File's OwnerClass加载,不是UIViewClass,OniOS8是正常的,iOS7是错误的

错误路径:

1.将项目目标更改为iOS7.

2.退出Xcode。

3.清理 DerivedData 文件夹。

4.打开项目。

5.选中Main.storyboard,查看预览。

这是演示

4

0 回答 0