[英]Xcode 6. Create custom view from nib file
I'm trying to create my custom view that will be loaded from nib file. 我正在尝试创建将从nib文件加载的自定义视图。
So I have 3 files: CustomView.h, CustomView.m, CustomView.xib. 所以我有3个文件:CustomView.h,CustomView.m,CustomView.xib。
As I need this view in my storyboard, I'm trying to load it in initWithCoder:
method: 当我在情节提要中需要此视图时,我试图将其加载到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;
}
But in this case view size is much more bigger than I need(I need the same size as my view has). 但是在这种情况下,视图的尺寸比我需要的要大得多(我需要与视图相同的尺寸)。
I tried to add constraints for width and height, but them I'm getting runtime error: 我试图添加宽度和高度的约束,但是它们出现运行时错误:
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.
New code is: 新代码是:
- (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;
}
Maybe somebody already have code that works? 也许有人已经可以使用代码了? Or can explain where is my problem? 还是可以解释我的问题在哪里?
Here is a sample project . 这是一个示例项目 。 Timer view is not in the center. 计时器视图不在中心。
Add this method in CustomView.m file 在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;}
and in CustomView.h file add + (id)createCustomView;
并在CustomView.h文件中添加+ (id)createCustomView;
and call this static method with class name to where you are creating object of it. 并使用类名将此静态方法调用到要创建它的对象的位置。
Hope it helps 希望能帮助到你
to use constraints set translatesAutoresizingMaskIntoConstraints for created view: 使用约束集为创建的视图设置translatesAutoresizingMaskIntoConstraints:
subView.translatesAutoresizingMaskIntoConstraints = NO;
add constraints, for example: 添加约束,例如:
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];
it works for me 这个对我有用
Use this 用这个
[[[NSBundle mainBundle] loadNibNamed:@"TableHeaderView"
owner:self
options:nil] objectAtIndex:0
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.