我有以下问题:我创建了一个自定义UIView
类及其布局XIB
文件。假设我在 XIB 中的自定义视图的大小是 150 x 50。我已启用sizeClasses
(瓦尼哈尼)和AutoLayout
。在模拟指标中我设置了Size = Freeform
, Status Bar = None
, all other = Inferred
.
我的自定义代码UIView
类看起来像这样:
#import "CustomView.h"
@implementation CustomView
#pragma mark - Object lifecycle
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self setup];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
[self setup];
}
return self;
}
#pragma mark - Private & helper methods
- (void)setup {
if (self.subviews.count == 0) {
[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
self.bounds = self.view.bounds;
[self addSubview:self.view];
}
}
@end
In my UIViewController
我想在哪里添加这个自定义UIView
,我制作了虚拟 UIView(我在 Interface Builder 中设置了大小),我想在其中添加我的CustomView
and 我希望我的CustomView
适合我的容器视图的边界.
我正在尝试这样做:
_vCustomView = [[CustomView alloc] init];
_vCustomView.translatesAutoresizingMaskIntoConstraints = NO;
[_vContainer addSubview:_vCustomView];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
但没有运气。假设我的容器视图是 300 x 100,当我添加自定义视图时UIView
to it, 我的自定义视图仍然是 150 x 50.
我尝试在没有容器视图的情况下工作 - 添加我的自定义UIView
直接反对我的自我观点UIViewController
并设置它的宽度和高度:
-
autoLayout
限制条件
- 通过使用
initWithFrame
初始化我的自定义时UIView
但再次 - 没有运气。自定义视图始终为 150 x 50。
有人可以向我解释一下我该怎么做吗以编程方式添加我的自定义UIView
在制作XIB
并使用调整它的大小autoLayout
限制条件?
提前谢谢了。
编辑#1:尝试在我的 CustomView 上运行 Andrea 的代码时出现错误
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:0x17489b760 V:[UIView:0x17418d820(91)]>",
"<NSLayoutConstraint:0x170c9bf80 V:|-(0)-[CustomView:0x1741da7c0] (Names: '|':CustomView:0x1741da8b0 )>",
"<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-| (Names: '|':CustomView:0x1741da8b0 )>",
"<NSLayoutConstraint:0x170c9be90 V:|-(0)-[CustomView:0x1741da8b0] (Names: '|':UIView:0x17418d820 )>",
"<NSLayoutConstraint:0x170c9bee0 CustomView:0x1741da8b0.bottom == UIView:0x17418d820.bottom>",
"<NSAutoresizingMaskLayoutConstraint:0x170c9dba0 h=--& v=--& CustomView:0x1741da7c0.midY == + 25.0>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-| (Names: '|':CustomView:0x1741da8b0 )>
编辑#2:它有效!
@Andrea 添加后:
view.translatesAutoresizingMaskIntoConstraints = NO;
in his:
- (void) stretchToSuperView:(UIView*) view;
方法,一切都按照我的预期进行。
感谢@Andrea。