簡體   English   中英

調整大小並在景觀中放置旋轉視圖:更新約束還是平移/更改框架?

[英]Resizing and placing a rotated view in landscape: updating constraints or translating/changing frames?

我有一個子視圖,它以縱向放置在屏幕底部:

在此處輸入圖片說明

我在storyboard中為此方向設置了約束,以將其前導,尾隨和底部空間固定到超級視圖,以及子視圖的固定高度。

當設備處於橫向時,我希望此子視圖位於屏幕的右側,如下所示:

在此處輸入圖片說明

在后面的UIViewController子類中,將M_PI / 2弧度旋轉應用於viewWillTransitionToSize:withTransitionCoordinator:的子視圖,然后得到以下結果:

在此處輸入圖片說明

我嘗試根據需要將子視圖放置在屏幕的右側,因此在旋轉之后,我調用[self.view setNeedsUpdateConstraints]; 並嘗試為此定位設置新的約束,但是我無法獲得想要的結果。 因此,我不確定是否要嘗試做錯事情...我認為,設置新約束將子視圖固定在屏幕的右側,將會調整其高度並刷新其X和Y值。 ..也許正確的方法是平移旋轉的子視圖並更改其框架?

我該如何解決?

謝謝

編輯:這是我以編程方式為景觀設置的約束示例:

NSLayoutConstraint *customSubviewConstraintTop = [NSLayoutConstraint constraintWithItem:self.customSubview
         attribute:NSLayoutAttributeLeading
         relatedBy:NSLayoutRelationEqual
         toItem:self.view
         attribute:NSLayoutAttributeLeading
         multiplier:1.0
         constant:0];

這應該是將子視圖的頂部(旋轉前的左側)固定到屏幕的頂部(縱向時為屏幕的右側)...對嗎?

編輯2:這是我在updateViewConstraints用於橫向約束的updateViewConstraints

if (!isPortrait) {

    self.customSubView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, M_PI/2);

    [self.view removeConstraint:self.customSubViewConstraintL];
    [self.view removeConstraint:self.customSubViewConstraintR];
    [self.view removeConstraint:self.customSubViewConstraintB];
    [self.view removeConstraint:self.customSubViewConstraintH];

    [self.customSubView setTranslatesAutoresizingMaskIntoConstraints:NO];

    self.customSubViewConstraintH = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:NSLayoutAttributeNotAnAttribute
                                                         multiplier:1.0
                                                           constant:35.0];

    self.customSubViewConstraintL = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeLeading
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeading
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintR = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeTrailing
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTrailing
                                                         multiplier:1.0
                                                           constant:0.0];

    self.customSubViewConstraintB = [NSLayoutConstraint constraintWithItem:self.customSubView
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0];

    [self.view addConstraint:self.customSubViewConstraintH];
    [self.view addConstraint:self.customSubViewConstraintL];
    [self.view addConstraint:self.customSubViewConstraintR];
    [self.view addConstraint:self.customSubViewConstraintB];
}

您可能使用了受屏幕限制的約束,例如BottomLayoutGuide。 請改用垂直間距。

UPD:問題是您在viewWillTransitionToSize:withTransitionCoordinator:中將旋轉設置為自定義視圖。 只需設置所需的高度和寬度即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM