簡體   English   中英

方向改變時重新定位控件

[英]Repositioning controls when the orientation changes

我知道當方向改變時,可以使用自動布局使尺寸和位置保持一致。 當方向改變時,是否可以完全改變布局?

例如,請以縱向模式查看簡單登錄屏幕的下面線框。

在此輸入圖像描述

現在,如果我旋轉設備,我想完全重新定位控件。

在此輸入圖像描述

可以通過使用自動布局來完成這種事嗎? 如果沒有,我該怎么辦呢?

謝謝。

在您的情況下,它可以通過兩種方法實現,而不是重新構建每個組件,您可以將控件分組,如下所示。

  1. 父視圖 - >用戶信息視圖 - >所有用戶信息控件..通過這樣做,您只需重新構建用戶信息視圖而不是所有控件。

    然后只剩下徽標和公司名稱進行重新設定..如果您對控件進行分組,則需要重新設置3個控件。

  2. 創建兩個視圖,一個用於縱向,另一個用於橫向模式,只需在旋轉上添加和刪除..這是最快的方法,因為您不必通過繁瑣的代碼重新調整框架。

希望以上有所幫助

你不能設置不同的框架: -

-(void)setFramesForPotrait
{

// set frames here

}

-(void)setFramesForLandscaeMode
{

// set frames here

}

-(bool)shouldAutorotate.....
{
return Yes
}

-()willAutoRotate......
{
if(orientation = uiinterfaceOrientationPotrait)
{
[self setFramesForPotrait];
}
else
{
[self setFramesForLandscape];
}

我遇到了同樣的問題,到目前為止我發現了這個問題。 我正在使用的方法是針對不同的屏幕尺寸或方向具有兩組(可能是多組)約束。 所以我只是為肖像設計並將所有特定於肖像的約束連接到IBOutletCollection: 在此輸入圖像描述 然后將InterfaceBuilder中的VC方向切換為橫向,並為此方向添加所需的約束並連接到相應的插座: 在此輸入圖像描述

然后在代碼中我根據需要刪除或添加約束:

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    [self updateViewConstraints];
}

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    if (self.view.frame.size.width >= kSize * 2 + kPadding * 3)
    {
        [self.view removeConstraints:self.constraintsForPortrait];
        [self.view addConstraints:self.constraintsForLandscape];
    } else
    {
        [self.view removeConstraints:self.constraintsForLandscape];
        [self.view addConstraints:self.constraintsForPortrait];
    }
}

您可以使用此代碼獲得類似的結果,基於方向而不是幀大小添加/刪除約束:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self updateViewConstraints];
}

但請記住,傳聞蘋果公司會發布具有不同屏幕寬高比的設備並廢棄UIInterfaceOrientation和旋轉事件,因此可能需要提前做好准備。 將會看到未來會帶來什么。

這種方法的缺點是,Xcode會抱怨設計是模糊的,實際上是,因為Xcode不知道我們將在運行時消除歧義。 為了解決這個問題,您可以將約束的優先級設置為您在此特定時刻未設計的所有其他方向的可選項(例如,如果您正在設計Portrait,則可以選擇Landscape的約束)。

使用autolayout時,您可以覆蓋updateViewConstraints以修改方向更改時的布局:

- (void)updateViewConstraints{

  [super updateViewConstraints];
  //portrait
  if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){

  }
  //landscape
  else{
    //
  }
}

它可以通過下面的布局約束完成。 這不太合適,因為你的草圖不太合適 - 你的景觀視圖與你在景觀中的真實情況相比太長了。 您必須縮短登錄文本字段以適應所有內容,但這應該讓您了解它是如何完成的。 buttonWidth約束顯示如何在視圖寬度和按鈕寬度之間建立負相關 - 也就是說,按鈕的寬度在橫向上比在縱向中要小。 我有幾個IBOutlet來約束我在代碼中引用的約束。 如果你有興趣,我可以為你描述一下,但我現在就把它扔出去:

@implementation ViewController {
    IBOutlet NSLayoutConstraint *buttonWidth;
    IBOutlet NSLayoutConstraint *logoTop;
    IBOutlet NSLayoutConstraint *logoAlignToLabel;
    IBOutlet NSLayoutConstraint *logoSpaceToLabel;
    IBOutlet NSLayoutConstraint *coNameToButtonAlignment;
    IBOutlet UIButton *b;
    IBOutlet UIImageView *logo;
    IBOutlet UILabel *coName;
    NSLayoutConstraint *con2;
    NSArray *cons1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [b removeConstraint:buttonWidth];
    buttonWidth = [NSLayoutConstraint constraintWithItem:b attribute:NSLayoutAttributeWidth relatedBy:0 toItem:self.view attribute:NSLayoutAttributeWidth multiplier:-.2193 constant:350];
    [self.view addConstraint:buttonWidth];
    [self.view layoutSubviews];
}

- (void)updateViewConstraints{
    [super updateViewConstraints];
    if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
        if (con2 != nil) {
            [self.view removeConstraints:cons1];
            [self.view removeConstraint:con2];
            [self.view addConstraints:@[logoAlignToLabel,logoSpaceToLabel,logoTop,coNameToButtonAlignment]];
        }
    }else{
        NSLog(@"Landscape");
        [self.view removeConstraints:@[logoAlignToLabel,logoSpaceToLabel,logoTop,coNameToButtonAlignment]];
        cons1 = [NSLayoutConstraint constraintsWithVisualFormat:@"|-8-[logo]-4-[coName]" options:NSLayoutFormatAlignAllCenterY metrics:0 views:@{@"logo":logo, @"coName":coName}];
        con2 = [NSLayoutConstraint constraintWithItem:logo attribute:NSLayoutAttributeCenterY relatedBy:0 toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0];
        [self.view addConstraints:cons1];
        [self.view addConstraint:con2];
    }
}

在輪換時,徽標和公司名稱標簽的約束被刪除,新的約束被放置到位。 我在viewDidLoad中放置的按鈕的新約束會自動處理旋轉,所以在旋轉期間我根本不需要調整它。

暫無
暫無

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

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