簡體   English   中英

如何在更改方向時完全隱藏UIView?

[英]How to hide a UIView completely when changing orientation?

我想設計一個視圖/視圖控制器,在橫向方向上自動顯示/隱藏子視圖。 我希望子視圖完全消失,其他子視圖占用它的空間。

使用UIViewController,我編寫了設置子視圖的frame屬性並調用它的代碼:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;

這在大多數情況下解決了這個問題,但是當視圖沒有出現時發生方向改變時會出現問題。 繞過這個,我也在調用調整大小的方法:

- (void)viewWillAppear:(BOOL)animated;

但這在一些罕見的情況下(涉及UISearchDisplayController)有問題所以我也在調用resizing方法

- (void)viewDidAppear:(BOOL)animated;

你可以理解,我對這段代碼不滿意,我正在尋找一種更好/更高效的方法來做到這一點。

有任何想法嗎?

假設您擁有的是UIWebView和廣告橫幅,那么您可以在橫向時手動調整webView的大小:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration
{
    if (toOrientation == UIInterfaceOrientationPortrait ||
        toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [adView setHidden:NO];
        }
    } else {
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [adView setHidden:YES];
        }       
    }
}

然后也做

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromOrientation 
                                duration:(NSTimeInterval)duration
{
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;
    if (toOrientation == UIInterfaceOrientationPortrait ||
        toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [webView setBounds:CGRectMake(0.0,0.0,320.0,436.0)];
        }
    } else {
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [webView setBounds:CGRectMake(0.0,0.0,480.0,320.0)];
        }       
    }
}

尺寸假設廣告橫幅高度為44.0,沒有導航欄(44.0)或狀態欄(20.0),因此您可能需要調整布局的數字。

- (void)didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation)fromInterfaceOrientation

隱藏它

sub_view.hidden = YES;

再次顯示它

sub_view.hidden = NO;

如果有幾個子視圖需要在橫向和縱向上以非常不同的方式進行布局,那么使用額外的UIView(例如在IB中添加的landscapeView)可能更容易破解它。 使用按鈕,子視圖等加載此視圖,並根據需要進行布局。 您可以使用與通常(縱向)視圖相同的所有連接。 記得申報IBOutlet UIView *landscapeView; 在標題中。 然后你可以使用這個添加視圖:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toOrientation 
                                duration:(NSTimeInterval)duration
{
    if ([landscapeView superview]) {
        if (toOrientation == UIInterfaceOrientationPortrait ||
            toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [landscapeView removeFromSuperview];
        }
    } else {
        if (toOrientation == UIInterfaceOrientationLandscapeLeft ||
            toOrientation == UIInterfaceOrientationLandscapeRight) {
            [[self view] addSubview:landscapeView];
        }       
    }
}

如果時間不能按你的喜好運行,那么你也可以在里面嘗試類似的東西

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

如果你想獲得更豐富的,那么你就可以離開主視圖空,同時創建UIView *portraitViewUIView *landscapeView ,然后刪除當前視圖willRotateToInterfaceOrientation並添加新的視圖didRotateToInterfaceOrientation

為安全起見,您還應確保最初顯示正確的視圖:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIInterfaceOrientation toOrientation = self.interfaceOrientation;
    if (toOrientation == UIInterfaceOrientationPortrait ||
        toOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self.view addSubview:portraitView];
    } else {
        [self.view addSubview:landscapeView];
    }

}

並且

- (void)viewWillDisappear:(BOOL)animated
{
    if ([landscapeView superview]) {
        [landscapeView removeFromSuperview];
    }
    if ([portraitView superview]) {
        [portraitView removeFromSuperview];
    }
}

一個選項是在旋轉時呈現帶有交叉漸變過渡的模態視圖,並在旋轉回來時將其關閉。

Personnaly,對於這樣的情況,我覺得編寫subView的位置比依賴框架的布局管理要簡單得多。

所以,假設你的應用程序顯示標題欄,其高度為20分,這就是我要做的:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    CGFloat titleBarHeight = 20;
    CGFloat navBarHeight = self.navigationController.navigationBar.frame.size.height;
    if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
        webView.frame = CGRectMake(0, 0, 320, 480 - titleBarHeight - navBarHeight - adView.frame.size.height);
        adView.hidden = NO;
    }else{
        webView.frame = CGRectMake(0, 0, 480, 320 - titleBarHeight - navBarHeight);
        adView.hidden = YES;
    }
}

我利用代碼中的父視圖邊界。 嘗試以下代碼,看看動畫是否適合您。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
            webView.frame = self.view.bounds;
        adView.hidden=YES;
            break;
        }
    }
}


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    switch (fromInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
        {
            webView.frame = originalFrame;
            adView.hidden=NO;
            break;
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    adView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    originalFrame = webView.frame;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

暫無
暫無

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

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