我想设计一个 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/