我使用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 Owner
类,而不是UIView
类,开iOS8
是正常的,iOS7
是错误
错误路径:
1.将项目目标更改为
iOS7
。2.Quit Xcode。
3.Clean DerivedData文件夹。
4.打开项目。
5.选择
Main.storyboard
,然后查看预览。
这是Demo