2016-04-25 410 views
2

所以,我創建了一個UIViewController子類與兩個元素:標題UIView和它下面的UITableView。編程佈局約束導致旋轉不調整基地UIView

---------------- 
||------------|| 
||Header View || 
||------------|| 
||------------|| 
|| Table View || 
||   || 
||   || 
||   || 
||------------|| 
---------------- 

我加入約束在init代碼頁眉視圖和表視圖如下:

- (void)setupHeaderView 
{ 
    self.headerView.backgroundColor = [UIColor whiteColor]; 

    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]; 
} 

這兩個視圖被添加到self.view和由呼叫被preceeded:

self.view.translatesAutoresizingMaskIntoConstraints = NO; 

但是,主要問題是,加載此視圖並旋轉設備/模擬器時,視圖將保持其縱向框架並保持其原點,以使設備窗口處於水平狀態, W幀仍是其肖像幀,導致它被屏幕之外,並具有一個空白的黑色空間旁邊:提供

截圖: Simulator Screenshot

如何維護傳播到上海華方位的變化,並在其後的子視圖?

+0

....我覺得自己像一個白癡....這工作。謝謝。如果您發表評論,我會將其標記爲已回答。 –

回答

2

實際上,您應該將標題和表格視圖的translatesAutoresizingMaskIntoConstraints設置爲NO

主視圖的translatesAutoresizingMaskIntoConstraints值應始終保持爲YES

view.translatesAutoresizingMaskIntoConstraints = YES; 
headerView.translatesAutoresizingMaskIntoConstraints = NO; 
tableView.translatesAutoresizingMaskIntoConstraints = NO;