ios - 编程布局约束导致旋转不调整基本 UIView
<p><p>所以我正在创建一个包含两个元素的 UIViewController 子类:标题 UIView 和它下面的 UITableView。</p>
<pre><code>----------------
||------------||
||Header View ||
||------------||
||------------||
|| Table View ||
|| ||
|| ||
|| ||
||------------||
----------------
</code></pre>
<p>我在初始化代码中给Header view和Table View添加约束如下:</p>
<pre><code>- (void)setupHeaderView
{
self.headerView.backgroundColor = ;
self.headerViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.headerView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
self.headerViewLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.headerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
self.headerViewTrailingConstraint = [NSLayoutConstraint constraintWithItem:self.headerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
self.headerViewHeightConstraint = [NSLayoutConstraint constraintWithItem:self.headerView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:HeaderViewHeight];
}
- (void)setupTableView
{
self.tableViewTopConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.headerView
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
self.tableViewLeadingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
self.tableViewTrailingConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1.0
constant:0.0];
self.tableViewBottomConstraint = [NSLayoutConstraint constraintWithItem:self.tableView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1.0
constant:0.0];
}
</code></pre>
<p>这两个 View 被添加到 self.view 并在调用之前:</p>
<pre><code>self.view.translatesAutoresizingMaskIntoConstraints = NO;
</code></pre>
<p>但主要问题是,在加载此 View 并旋转设备/模拟器时, View 会保持其纵向框架并保持其原点,因此设备窗口是水平的,但 View 框架仍然是其纵向框架,导致它离开屏幕并在其旁边有一个空白的黑色空间:</p>
<p>提供的屏幕截图:
<a href="/image/IawQx.png" rel="noreferrer noopener nofollow"><img src="/image/IawQx.png" alt="Simulator Screenshot"/></a> </p>
<p>我如何保持传播到父 View 以及随后传播到其 subview 的方向变化?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>实际上,您应该将标题和表格 View 的 <code>translatesAutoresizingMaskIntoConstraints</code> 设置为 <code>NO</code>。 </p>
<p>主视图的 <code>translatesAutoresizingMaskIntoConstraints</code> 值应始终保持为 <code>YES</code>:</p>
<pre><code>view.translatesAutoresizingMaskIntoConstraints = YES;
headerView.translatesAutoresizingMaskIntoConstraints = NO;
tableView.translatesAutoresizingMaskIntoConstraints = NO;
</code></pre></p>
<p style="font-size: 20px;">关于ios - 编程布局约束导致旋转不调整基本 UIView,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36851351/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36851351/
</a>
</p>
页:
[1]