Xcode 6. 从 nib 文件创建自定义视图

Xcode 6. Create custom view from nib file

我正在尝试创建将从 nib 文件加载的自定义视图。

所以我有 3 个文件:CustomView.h、CustomView.m、CustomView.xib。

因为我的情节提要中需要这个视图,所以我尝试在 initWithCoder: 方法中加载它:

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

    if (self != nil) {
        UIView *view = [[NSBundle bundleForClass:[self class]] loadNibNamed:KVBTimerViewNibName owner:self options:nil].firstObject;
        [self addSubview:view];
    }

    return self;
}

但在这种情况下,视图大小比我需要的大得多(我需要与我的视图大小相同)。

我尝试为宽度和高度添加约束,但出现运行时错误:

    2015-06-22 16:17:27.492 KVBTimer[8289:993411] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7f9e0bf19090 V:[KVBTimerView:0x7f9e0bf18a90(250)]>",
    "<NSLayoutConstraint:0x7f9e0bf1bda0 V:|-(0)-[UIView:0x7f9e0bf1fba0]   (Names: '|':KVBTimerView:0x7f9e0bf18a90 )>",
    "<NSLayoutConstraint:0x7f9e0bf1f700 UIView:0x7f9e0bf1fba0.bottom == KVBTimerView:0x7f9e0bf18a90.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f9e0bf2dfe0 h=--& v=--& UIView:0x7f9e0bf1fba0.midY == + 57>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f9e0bf1f700 UIView:0x7f9e0bf1fba0.bottom == KVBTimerView:0x7f9e0bf18a90.bottom>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2015-06-22 16:17:27.494 KVBTimer[8289:993411] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x7f9e0bf1b560 H:|-(0)-[UIView:0x7f9e0bf1fba0](LTR)   (Names: '|':KVBTimerView:0x7f9e0bf18a90 )>",
    "<NSLayoutConstraint:0x7f9e0bf1bc80 UIView:0x7f9e0bf1fba0.right == KVBTimerView:0x7f9e0bf18a90.right>",
    "<NSLayoutConstraint:0x7f9e0bf1a090 KVBTimerView:0x7f9e0bf18a90.width == UIView:0x7f9e0bf1eee0.width>",
    "<NSAutoresizingMaskLayoutConstraint:0x7f9e0bf2db80 h=--& v=--& UIView:0x7f9e0bf1fba0.midX == + 223.5>",
    "<NSLayoutConstraint:0x7f9e0bd32960 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7f9e0bf1eee0(375)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7f9e0bf1bc80 UIView:0x7f9e0bf1fba0.right == KVBTimerView:0x7f9e0bf18a90.right>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

新代码是:

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

    if (self != nil) {
        UIView *view = [[NSBundle bundleForClass:[self class]] loadNibNamed:KVBTimerViewNibName owner:self options:nil].firstObject;
        [self addSubview:view];
        NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
    NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
    NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:view attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];

    [self addConstraint:topConstraint];
    [self addConstraint:bottomConstraint];
    [self addConstraint:leftConstraint];
    [self addConstraint:rightConstraint];
    }

    return self;
}

也许有人已经有了有效的代码?或者可以解释一下我的问题在哪里?

这里是sample project。计时器视图不在中心。

使用这个

[[[NSBundle mainBundle] loadNibNamed:@"TableHeaderView"
                                                                owner:self
                                                              options:nil] objectAtIndex:0
                                   ];

在 CustomView.m 文件中添加此方法

+ (id)createCustomView{
CustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:nil options:nil] lastObject];
// make sure customView is not nil or the wrong class!
if ([customView isKindOfClass:[CustomView class]])
{
    return customView;
}
else
    return nil;}

并在 CustomView.h 文件中添加 + (id)createCustomView;

并使用 class 名称调用此静态方法到您创建它的对象的位置。

希望对您有所帮助

为创建的视图使用约束集 translatesAutoresizingMaskIntoConstraints:

subView.translatesAutoresizingMaskIntoConstraints = NO;

添加约束,例如:

NSLayoutConstraint *topConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0];
NSLayoutConstraint *bottomConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:subView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0];

[self addConstraint:topConstraint];
[self addConstraint:bottomConstraint];
[self addConstraint:leftConstraint];
[self addConstraint:rightConstraint];

对我有用