我有以下问题:我创建了一个自定义的
来自我的自定义
在我的
我尝试这样做:
但是没有成功。假设我的容器视图是300 x 100,当我将我的自定义
我尝试过不使用容器视图——直接将我的自定义
有人能否解释一下如何通过
非常感谢。 编辑#1:当我尝试在我的CustomView上运行Andrea的代码时出现的错误
在他的文章中:
UIView
类,并在XIB
文件中对其进行布局。假设我的自定义视图在XIB中的大小为150 x 50。我已经启用了sizeClasses
(wAny hAny)和AutoLayout
。在模拟度量中,我设置了Size = Freeform
,Status Bar = None
,所有其他项都是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
在我的
UIViewController
中,我想添加这个自定义的UIView
,我已经创建了一个虚拟的UIView(我已经在Interface Builder中设置了它的大小),我想把我的CustomView
添加到其中,并且我希望我的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
添加到其中时,我的自定义视图仍然是150 x 50。我尝试过不使用容器视图——直接将我的自定义
UIView
对象添加到我的UIViewController
的self.view中,并使用以下方式设置其宽度和高度:
autoLayout
约束- 在初始化我的自定义
UIView
时使用initWithFrame
有人能否解释一下如何通过
autoLayout
约束以编程方式添加我的自定义XIB
中制作的UIView
并调整其大小?非常感谢。 编辑#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;
在他的文章中:
- (void) stretchToSuperView:(UIView*) view;
通过这种方法,一切都按照我的预期运作。
感谢 @Andrea 的帮助。
layoutIfNeeded
?您能否发布更改宽度和高度的代码?此外,如果视图在故事板中具有高度和宽度约束,则将约束添加到“contentView”将导致冲突,因为它们将发生冲突。 - Catalina T.