根据方向在 XIB 中切换视图
Posted
技术标签:
【中文标题】根据方向在 XIB 中切换视图【英文标题】:Switch View in XIB based on orientation 【发布时间】:2011-05-05 22:43:56 【问题描述】:对于每个方向,我的 XIB 中有两个视图。它们相似但不同,所以我不能依赖自动调整大小。我想根据方向切换这些视图。当我使用方法 didRotateFromInterfaceOrientation 时,当它从 Portrait 旋转时,它将显示横向视图,当它旋转到 PortraitUpsideDown 时,这是一个问题。 如果我修改 willRotateToInterfaceOrientation 的代码,那么在 LandscapeView 中,PortraitView 对用户隐藏,但是当用户单击按钮时,它们不会显示为灰色以显示它们已被单击,并且它的应用程序响应就像用户正在单击按钮一样在纵向视图中。
我是否以正确的方式解决这个问题?我需要做什么来修复这些错误。我更喜欢使用 willRotateToInterfaceOrientation 方法。
谢谢,
代码:
- (void)viewDidLoad
...
for (UIView *v in self.view.subviews)
v.autoresizingMask = UIViewAutoresizingNone;
...
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
return YES;
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
if(fromInterfaceOrientation == UIInterfaceOrientationPortrait
|| fromInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
//Add landscape view
if(![landscapeView superview])
for (UIView *v in landscapeView.subviews)
v.autoresizingMask = UIViewAutoresizingNone;
//No effect after testing
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.33];
[self.view addSubview:landscapeView];
[UIView commitAnimations];
else
if([landscapeView superview])
[landscapeView removeFromSuperview];
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
//Add landscape view
if(![landscapeView superview])
for (UIView *v in landscapeView.subviews)
v.autoresizingMask = UIViewAutoresizingNone;
//No effect after testing
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.33];
[self.view addSubview:landscapeView];
[UIView commitAnimations];
else
if([landscapeView superview])
[landscapeView removeFromSuperview];
【问题讨论】:
【参考方案1】:在你看来确实加载了
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(willRotateToInterfaceOrientation:)
name:@"UIDeviceOrientationDidChangeNotification" object:nil];
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft)
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeRight)
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait)
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown)
viewControllerName *newView = [[viewControllerName alloc] initWithNibName:nil bundle:nil];
newView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:newView animated:YES];
[newView release];
【讨论】:
我应该保留我以前的代码吗?收到此错误:2011-08-17 19:56:13.527 Fraction Calculator Pro[3406:13103] -[Fraction_CalculatorViewController didRotate:]: unrecognized selector sent to instance 0x6341d50
对不起,我在[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willRotateToInterfaceOrientation:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
中打错了,用更新版本的代码替换你之前的代码
并没有真正按照我的需要工作。始终以纵向显示应用程序而不显示横向视图。我有一个非常高级的设置。请求 Apple 的技术人员帮助我。不过感谢您的尝试。以上是关于根据方向在 XIB 中切换视图的主要内容,如果未能解决你的问题,请参考以下文章