使用导航后退按钮进行自动旋转

时间:2011-09-02 09:10:06

标签: xcode ipad uinavigationcontroller orientation

我正在创建一个支持所有方向的应用 我在这里使用navigationController 我正在使用此代码

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

if (interfaceOrientation == UIInterfaceOrientationPortrait 
    || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
    category1.frame = CGRectMake(54, 68, 190, 174);
    category2.frame = CGRectMake(291, 68, 190, 174);
    category3.frame = CGRectMake(525, 68, 190, 174);
    category4.frame = CGRectMake(54, 296, 190, 174);
    category5.frame = CGRectMake(291, 296, 190, 174);
    category6.frame = CGRectMake(525, 296, 190, 174);
    category7.frame = CGRectMake(54, 527, 190, 174);
    category8.frame = CGRectMake(291, 527, 190, 174);
    category9.frame = CGRectMake(525, 527, 190, 174);
    category10.frame = CGRectMake(291, 757, 190, 174);
    extra1.frame = CGRectMake(80, 781, 138, 125);
    extra2.frame = CGRectMake(551, 781, 138, 125);

}
else 
{
    category1.frame = CGRectMake(61, 50, 190, 174);
    category2.frame = CGRectMake(298, 50, 190, 174);
    category3.frame = CGRectMake(537, 50, 190, 174);
    category4.frame = CGRectMake(774, 50, 190, 174);
    category5.frame = CGRectMake(61, 278, 190, 174);
    category6.frame = CGRectMake(298, 278, 190, 174);
    category7.frame = CGRectMake(537, 278, 190, 174);
    category8.frame = CGRectMake(774, 278, 190, 174);
    category9.frame = CGRectMake(298, 509, 190, 174);
    category10.frame = CGRectMake(537, 509, 190, 174);
    extra1.frame = CGRectMake(80, 533, 120, 125);
    extra2.frame = CGRectMake(800, 533, 120, 125);

}

}

在这个视图中它完美地运作。当我选项卡按钮它转到secondviewcontroller我在哪里显示tableview 但是,如果我通过导航后退按钮返回第一个视图,则方向显示不完美 上面的代码不适用于导航后退按钮。

我该怎么做才能解决这个问题? 有什么建议??

提前谢谢。

编辑:

我已从firstview中删除了上面的函数,并将代码复制到此函数中。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
    if (interfaceOrientation == UIInterfaceOrientationPortrait 
    || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
    category1.frame = CGRectMake(54, 68, 190, 174);
    category2.frame = CGRectMake(291, 68, 190, 174);
    category3.frame = CGRectMake(525, 68, 190, 174);
    category4.frame = CGRectMake(54, 296, 190, 174);
    category5.frame = CGRectMake(291, 296, 190, 174);
    category6.frame = CGRectMake(525, 296, 190, 174);
    category7.frame = CGRectMake(54, 527, 190, 174);
    category8.frame = CGRectMake(291, 527, 190, 174);
    category9.frame = CGRectMake(525, 527, 190, 174);
    category10.frame = CGRectMake(291, 757, 190, 174);
    extra1.frame = CGRectMake(80, 781, 138, 125);
    extra2.frame = CGRectMake(551, 781, 138, 125);

}
else 
{
    category1.frame = CGRectMake(61, 50, 190, 174);
    category2.frame = CGRectMake(298, 50, 190, 174);
    category3.frame = CGRectMake(537, 50, 190, 174);
    category4.frame = CGRectMake(774, 50, 190, 174);
    category5.frame = CGRectMake(61, 278, 190, 174);
    category6.frame = CGRectMake(298, 278, 190, 174);
    category7.frame = CGRectMake(537, 278, 190, 174);
    category8.frame = CGRectMake(774, 278, 190, 174);
    category9.frame = CGRectMake(298, 509, 190, 174);
    category10.frame = CGRectMake(537, 509, 190, 174);
    extra1.frame = CGRectMake(80, 533, 120, 125);
    extra2.frame = CGRectMake(800, 533, 120, 125);

}

    return YES;
 }

在我的第二个视图控制器中我已经完成了

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {

    return YES;
 }

当我通过点击导航返回按钮返回第一个视图时其他条件(横向模式)工作正常但我不知道为什么它不能用于如果条件(Portraitmode)。

1 个答案:

答案 0 :(得分:1)

在以下函数中写下所有这些代码。

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
        //set all the frames according to the current interface orientation
        return YES;
 }

新编辑:

将此方法写入detailviewcontroller.m文件

  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
        UIViewController *controller = [self.navigationController.viewControllers objectAtIndex:0];
        [controller shouldAutorotateToInterfaceOrientation:interfaceOrientation];

        return YES;
  }