【发布时间】: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];
}
}
}
【问题讨论】:
标签: uiview view ios4 landscape