如何在更改方向时完全隐藏UIView?

Pan*_*ros 10 iphone uiview autoresize iphone-sdk-3.0

我想设计一个视图/视图控制器,它在横向时自动显示/隐藏子视图.我希望子视图完全消失,其他子视图占用它的空间.

使用UIViewController,我编写了设置子视图的frame属性并调用它的代码:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration;
Run Code Online (Sandbox Code Playgroud)

这在大多数情况下解决了这个问题,但是当视图没有出现时发生方向改变时会出现问题.绕过这个,我也在调用调整大小的方法:

- (void)viewWillAppear:(BOOL)animated;
Run Code Online (Sandbox Code Playgroud)

但这在一些罕见的情况下(涉及UISearchDisplayController)有问题所以我也在调用resizing方法

- (void)viewDidAppear:(BOOL)animated;
Run Code Online (Sandbox Code Playgroud)

你可以理解,我对这段代码不满意,我正在寻找一种更好/更高效的方法来做到这一点.

有任何想法吗?

Pen*_*One 10

假设您拥有的是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];
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

然后也做

- (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)];
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

尺寸假设广告横幅高度为44.0,没有导航栏(44.0)或状态栏(20.0),因此您可能需要调整布局的数字.


Tuo*_*nen 0

里面

- (void)didRotateFromInterfaceOrientation:
    (UIInterfaceOrientation)fromInterfaceOrientation
Run Code Online (Sandbox Code Playgroud)

隐藏它

sub_view.hidden = YES;
Run Code Online (Sandbox Code Playgroud)

再次展示

sub_view.hidden = NO;
Run Code Online (Sandbox Code Playgroud)