我无法将我的应用从Autoresize迁移到Autolayout机制。我需要的是一个动态大小和子视图的视图,它将连接到左上角与其超级视图相同的大小。在IB中,我将PieView(我的超级视图)的大小设置为186x186。让IB生成所有需要的约束(我将在代码中创建新的约束):
PieView.m -updateConstraints()
- (void)updateConstraints {
[super updateConstraints];
[self removeConstraintsAffectingViewAndSubviews];
[self setTranslatesAutoresizingMaskIntoConstraints:NO];
CGFloat sizeCoef = 0.7f;
CGFloat percent = [self.category getFillinPercent] / 100.0f;
percent = percent > 1.0f ? 1.0f : percent;
sizeCoef += (1.0f - sizeCoef) * percent;
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f
constant:100.f];//kDiameter * sizeCoef];
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0f
constant:100.0f];//kDiameter * sizeCoef];
//_imgEmptyCircle, _imgFullCircle - UIImageViews
NSDictionary *views = NSDictionaryOfVariableBindings(_imgEmptyCircle, _imgFullCircle, self);
NSDictionary *metrics = @{@"height":@100.0};
[self addConstraints:@[width, height]];
NSString *visualForm = @"H:|[_imgEmptyCircle(height)][_imgFullCircle(height)]|";
NSArray *horizontalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:visualForm
options:0
metrics:metrics
views:views];
visualForm = @"V:|[_imgEmptyCircle(height)][_imgFullCircle(height)]|";
NSArray *verticalConstraints = [NSLayoutConstraint constraintsWithVisualFormat:visualForm
options:0
metrics:metrics
views:views];
[self addConstraints:verticalConstraints];
[self addConstraints:horizontalConstraints];
}
结果让我感到惊讶。蓝色的一个正确显示我想要,但绿色的一个有奇怪的行为。
当然我有错误:无法同时满足约束条件
第一张imageView :
NSIBPrototypingLayoutConstraint:0xa2795d0'IB在构建时自动生成,用于具有固定帧的视图'V:[UIImageView:0x8a37150(186)]>
NSLayoutConstraint:0xa0305c0 V:[UIImageView:0x8a37150(100)]
第二个imageView :同一个
可能是PieView :
(
NSLayoutConstraint:0x8a40750 V:[CEPieView:0x8a36e50(100)],
NSLayoutConstraint:0xa030570 V:| - (0) - [UIImageView:0x8a37150](名称:'|':CEPieView:0x8a36e50),
NSLayoutConstraint:0xa0305c0 V:[UIImageView:0x8a37150(100)],
NSLayoutConstraint:0xa0305f0 V:[UIImageView:0x8a37150] - (0) - [UIImageView:0xa277cf0]>
NSLayoutConstraint:0xa030620 V:[UIImageView:0xa277cf0(100)],
NSLayoutConstraint:0xa030650 V:[UIImageView:0xa277cf0] - (0) - | (名称:'|':CEPieView:0x8a36e50)
)
请给我一个建议如何解决它。如何在AutoLayout机制的帮助下创建和旧的自动调整掩码(UIViewAutoresizingFlexibleHeight,UIViewAutoresizingFlexibleWidth)。任何帮助将不胜感激。
答案 0 :(得分:0)
您设置约束的方式是错误的。基本上你要求superview是100pts 100pts,并且包含两个子视图,每个子视图是100pts×100pts并且彼此并排在AND之上。所以首先你的超级视图必须是200pts宽和/或高,然后你不能有两个视图,它们彼此相邻并位于顶部。 你真正想做的事(我认为)是:
NSString *visualForm = @"H:|[_imgFullCircle]|"; // No need to explicitly set the height
然后添加相应的约束,然后:
visualForm = @"H:|[_imgEmptyCircle(height)]|";
为此添加另一个约束,并对垂直约束执行相同的操作。
基本上你最终得到:
//_imgEmptyCircle, _imgFullCircle - UIImageViews
NSDictionary *views = NSDictionaryOfVariableBindings(_imgEmptyCircle, _imgFullCircle, self);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_imgEmptyCircle]|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_imgFullCircle]|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imgEmptyCircle]|"
options:0
metrics:nil
views:views]];
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_imgFullCircle]|"
options:0
metrics:nil
views:views]];