ios - 以编程方式自动布局不起作用
<p><p>我想深入了解自动布局,所以我想以编程方式进行以便正确理解它。我知道如何通过 Storyboard很好地使用它。现在这就是我正在做的事情(在 <code>viewDidAppear:</code>)</p>
<pre><code>-(void)scenario2{
PGView *viewA = [initWithFrame:CGRectMake(100, 100, 100, 100) andBackgroundColor:];
;
PGView *viewB = [initWithFrame:CGRectMake(200, 300, 100, 100) andBackgroundColor:];
;
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewB.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint;
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
//problem statement
constraint = ;
;
NSLog(@"%@",viewA);
NSLog(@"%@",viewB);
}
</code></pre>
<ol>
<li><p>所以这里我初始化了两个不同颜色的 View 。</p></li>
<li><p>我知道我需要设置属性。在应用任何新约束之前,<code>translatesAutoresizingMaskIntoConstraints</code> 为 <code>NO</code>。所以我做到了。</p></li>
<li><p>现在我正在指定两个 View 的高度和宽度。所以,两个 View 的 x_position 和 y_position 都是 0。我运行代码并得到预期的结果。两个 View 在点 (0,0) 处都有指定的高度和宽度。</p></li>
</ol>
<p>问题</p>
<ol start="4">
<li><p>现在我在写“问题陈述”根据viewA的宽度调整viewB的宽度时,它不起作用。</p></li>
<li><p>另外,当我打印 ViewviewA 和 viewB 时,它会将帧打印为 (0,0,0,0)。</p></li>
</ol>
<p>谁能解释一下这种行为?<br/><br/><br/></p>
<p>编辑:这是我所做的一些修改。我已经使用“hasAmbiguousLayout”检查了两个 View 的歧义,现在它工作正常。现在如果我想将 viewB 的宽度调整为三次,下一步应该是什么viewA 的宽度?</p>
<pre><code>-(void)scenario2{
PGView *viewA = ;
viewA.backgroundColor = ;
;
PGView *viewB = ;
viewB.backgroundColor = ;
;
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewB.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *constraint;
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewB)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
" options:0 metrics:nil views:NSDictionaryOfVariableBindings(viewA)]];
NSLog(@"%d",viewA.hasAmbiguousLayout);
NSLog(@"%d",viewB.hasAmbiguousLayout);
NSLog(@"%@",viewA);
NSLog(@"%@",viewB);
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>问题在于您已将 A 和 B 的宽度限制分别明确设置为 200 和 100。因此,当与其他约束结合时,指定一个宽度应为另一个宽度三倍的附加约束是不可满足的。</p>
<p>此外,约束是不明确的,因为您已经定义了宽度和高度约束,但还没有定义 <em>where</em> 两个框架应该在哪里。是的,您已经为这两个 View 定义了 <code>frame</code>,但是在应用约束时会忽略它。您可能根本不应该定义 <code>frame</code> 值,因为它具有误导性和混淆性。但是您应该设置,例如,前导和顶部约束,或者在 VFL 中指定这些,或者添加 <code>centerX</code> 和 <code>centerY</code> 约束。您只需要一些约束来指定 View 的放置位置,并且有很多方法可以指定它。</p>
<p>最后你没有看到合理的 <code>frame</code> 值的原因是你已经定义了约束,但是它们还没有被应用。如果需要,您可以在 <code>viewDidLayoutSubviews</code> 中检查它们。</p>
<hr/>
<p>你可能会这样做:</p>
<pre><code>- (void)scenario2 {
PGView *viewA = ;
viewA.backgroundColor = ;
;
PGView *viewB = ;
viewB.backgroundColor = ;
;
viewA.translatesAutoresizingMaskIntoConstraints = NO;
viewB.translatesAutoresizingMaskIntoConstraints = NO;
NSDictionary *views = NSDictionaryOfVariableBindings(viewA, viewB);
// note, I added `|-100-` to say it's 100 points from the top (and 100 tall)
" options:0 metrics:nil views:views]];
// likewise, to say 200 points from the left (and 100 wide)
" options:0 metrics:nil views:views]];
// again, 100 from the top (as well as 200 tall)
" options:0 metrics:nil views:views]];
// and 100 from the left
// but I've removed width definition, as we'll use your multiplier constraint below
" options:0 metrics:nil views:views]];
// now that we've removed the width constraint from the above,
// this should now work fine
NSLayoutConstraint *constraint = ;
;
// no point in doing this, because constraints haven't yet been applied
//
// NSLog(@"%@",viewA);
// NSLog(@"%@",viewB);
}
// if you want to see where they are, you could do that here:
- (void)viewDidLayoutSubviews {
;
for (UIView *view in self.view.subviews) {
NSLog(@"%@", view);
}
NSLog(@"-----");
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 以编程方式自动布局不起作用,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/38801693/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/38801693/
</a>
</p>
页:
[1]