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

时间:2015-06-22 12:07:22

标签: ios objective-c xcode6 interface-builder custom-view

我正在尝试创建将从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。定时器视图不在中心。

3 个答案:

答案 0 :(得分:1)

在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;

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

希望有所帮助

答案 1 :(得分:1)

使用约束为创建的视图设置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];

它对我有用

答案 2 :(得分:0)

使用此

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