2012-08-03 35 views
1

我有一个UIPageViewController,其中视图中添加了两个子视图:带有UITapGestureRecognizer的透明子视图和带有一些按钮的工具栏,当我点击另一个子视图时,该按钮从底部向上滑动。UIPageViewController在旋转之后将子视图带到前面

编辑。 这是子视图设置在viewDidAppear:(BOOL)动画

CGRect frame; 
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
    frame = CGRectMake(50, 0, 220, 480); 
} 
else { 
    frame = CGRectMake(50, 0, 380, 320); 
} 
if (!tapView) { 
    tapView = [[LSTapView alloc]initWithFrame:frame]; 
    //[tapView setBackgroundColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:0.4]]; 
    [self.view addSubview:tapView]; 
    [tapView release]; 
} 
else { 
    if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
     [tapView setFrame:CGRectMake(50, 0, 220, 480)]; 
     [fontViewController.view setFrame:CGRectMake(0, 480, 320, 92)]; 
    } 
    else { 
     [tapView setFrame:CGRectMake(50, 0, 380, 320)]; 
     [fontViewController.view setFrame:CGRectMake(0, 320, 480, 92)]; 

    } 
} 

if (!fontViewController){ 
    fontViewController = [[LSFontViewController alloc]initWithNibName:@"LSFontView" bundle:nil]; 
} 
if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice]orientation])){ 
    [fontViewController.view setFrame:CGRectMake(0, 480, 320, 92)]; 
} 
else { 
    [fontViewController.view setFrame:CGRectMake(0, 320, 480, 92)]; 
} 

[self.view addSubview:fontViewController.view]; 

如果我改变页面,而无需旋转设备,一切都运行在两个方向的罚款。然而,当我旋转设备时,这两个子视图消失了,我发现它们不在前面。无论如何,如果我加入这个我的代码:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self.view bringSubviewToFront:tapView]; 
    [self.view bringSubviewToFront:fontViewController.view]; 
} 

一个奇怪的现象发生了:我不能改变的网页了,或者更好的,一个和下一个viewControllers正确加载,但他们不显示和网页别改变。

有人可以解释我发生了什么?

谢谢。 L.

+0

你能告诉你如何最初设置你的子视图? – sergio 2012-08-03 17:16:09

+0

当然,我会编辑问题.. – Lolloz89 2012-08-04 09:18:27

回答

1

我通过注册UIPageViewController到UIDeviceOrientationDidChangeNotification这样解决了这个问题,简单地添加了didRotateFromInterfaceOrientation选择器,将数据源搞乱并导致奇怪的崩溃。

0

您的问题应该与在iOS 5下旋转设备后不自动调用viewWillAppear这一事实有关。因此,当您旋转设备时,您将代码重新排列子视图的框架不是执行。我不知道这是iOS 5中的错误,还是iOS 5的某些特定小版本中的错误,但是几周前我发现了这个错误,并且它在自动化存在的情况下也破坏了我的逻辑。

一个简单的尝试修复,这将是:

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{ 
    [self viewWillAppear:NO]; 
} 

让我知道这是否正常工作。

在任何情况下,我觉得很困惑,你正在viewWillAppear实例化你的意见。国际海事组织,正确的地方是viewDidLoad

我的建议是处理子视图创建的逻辑在viewDidLoad然后处理视图定位的逻辑在一个单独的方法,我们称之为layoutSubviews,你会从viewDidLoaddidRotateFromInterfaceOrientation同时调用。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation) name:UIDeviceOrientationDidChangeNotification object:nil]; 

,并通过添加这个简单的选择:

-(void)didChangeOrientation{ 
    [self.view bringSubviewToFront:tapView]; 
    [self.view bringSubviewToFront:fontViewController.view]; 
} 

对于一些原因,我真的不知道和了解

+0

感谢您的回答,但不幸的是,这并没有帮助... – Lolloz89 2012-08-04 10:10:20

+0

你可以检查viewWillAppear执行时,你旋转设备,并通过它来确保一切都发生在你身边期望?还请检查我的建议以上... – sergio 2012-08-04 11:02:46

+0

一件重要的事情:在viewWillAppear,确保设备的方向是你期望的... – sergio 2012-08-04 11:06:35