iphone - 如何在改变方向时完全隐藏 UIView?

标签 iphone iphone-sdk-3.0 uiview autoresize

我想设计一个 View / View Controller ,在横向时自动显示/隐藏 subview 。我希望 subview 完全消失,其他 subview 占据其空间。

使用 UIViewController,我编写了设置 subview 的框架属性并调用它的代码:

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

这在大多数情况下可以解决问题,但是当 View 未出现时发生方向变化时就会出现问题。 为了绕过这个问题,我还调用了调整大小方法:

- (void)viewWillAppear:(BOOL)animated;

但这在某些罕见的情况下会出现问题(涉及 UISearchDisplayController),因此我还在

上调用调整大小方法
- (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),因此您可能需要调整布局的数字。

关于iphone - 如何在改变方向时完全隐藏 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2433273/

相关文章:

iphone - ffmpeg 音频和 iPhone

Safari 上的 Javascript 屏幕方向

ios - 为什么这不足以添加一个 View 以使用自动布局占据所有屏幕?

ios - 最佳实践 : UIViewController or UIView

Iphone UIView 切换 : Previous view's subviews polluting newly switched-to view

iphone - ios 中整数值的正则表达式 - 接受多个符号?

iphone - 在 UIAlertView 响应中设置 NSString 的值,不能在其他地方使用吗?

objective-c - 将 UITable 中的 UITableViewCell 中的 subview 中的对象的 CGRect 转换为相对于 self.view 的 CGRect

iphone - iPhone SDK 如何在通讯录中创建联系人?

ios - 在安装新版本的应用程序时升级现有应用程序中的CoreData