由于Orientation/Shake,UINavigationController加载视图不正确

Sum*_*Sum 6 iphone xcode uinavigationbar popviewcontroller ios

背景:App有一个回家的功能.主视图仅支持纵向.如果你比平时更加​​晃动,你所看到的视图开始旋转(这很好),但随后它会检测到一个震动,并将popViewControlller发送到主视图.当它这样做时它加载导航控制器就好了,但是(主页内容)下的视图被加载到栏后面并被拉伸(它基本上是在导航栏下方加载,因此它被拉伸)

后退按钮处理这个从风景到肖像很好(因为它不是中间过渡)

我应该如何处理这个方向更改(从摇动),以便我可以弹回到根视图控制器,而无需在导航栏下加载视图?

编辑:发生的事情是内容认为它有整个视图加载,所以它自己伸展整个屏幕,没有意识到它上面的导航栏.我可以看出,因为图像加载已拉长

增加了50美元的赏金.

编辑这里我是如何检测摇晃和弹出的

- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{

    if ( event.subtype == UIEventSubtypeMotionShake )
    {

            UINavigationController *navController = self.navigationController;

            [[self retain] autorelease];
            HomeViewController *home = [[HomeViewController alloc]init];
            [navController popViewControllerAnimated:YES];

            home.title =@"Home View Controller";
            [home release];     
        }

    if ( [super respondsToSelector:@selector(motionEnded:withEvent:)] )
        [super motionEnded:motion withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

这是我的App代表:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    navController = [[UINavigationController alloc]init];
    [self.window addSubview:navController.view];

    HomeViewController *home = [[HomeViewController alloc]init];
    [[self home] setFrame:[[UIScreen mainScreen] applicationFrame]];
Run Code Online (Sandbox Code Playgroud)

我会在这里加一个模型.

普通视图:

普通视图

摇晃/流行后的拉伸视图:

Streched

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 
{}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

Amr*_*mro 2

我对你的代码有点困惑,所以我真的建议从头开始。正如 Lukya 提到的,没有理由重新创建 HomeViewController。我也对“[[自我保留]自动释放];”感到困惑 少量。除非您在其他地方做错了什么,否则这应该是没有必要的。

所以我会从这个开始......在 application:didFinishLaunchingWithOptions: 中做这样的事情:

    - (BOOL)应用程序:(UIApplication *)应用程序 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        HomeViewController *home = [[[HomeViewController alloc] init] autorelease];
        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:home] autorelease];
        [self.window addSubview:navController.view];
    }

该窗口将保留您的导航控制器,而导航控制器将保留您的 HomeViewController。

然后在motionEnded:withEvent:中做类似的事情:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)事件
    {
        if (event.subtype == UIEventSubtypeMotionShake)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }

应该是这样。

如果这不起作用那么你能提供任何其他信息吗?例如,HomeViewController 是否在 shouldAutorotateToInterfaceOrientation: 中实现(并返回 YES)?如果是这样,您能否返回“否”,以便它不会旋转,因为您的第一行显示“主视图仅支持纵向”?

willRotateToInterfaceOrientation:duration:编辑:和的一个例子didRotateFromInterfaceOrientation:

在您检测到抖动的任何控制器的标题中添加一个布尔值:

    BOOL 是旋转;

在您的实现文件中添加我们想要覆盖的两个 UIViewController 方法 - 类似于:

    - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 持续时间:(NSTimeInterval)duration {
        [超级 willRotateToInterfaceOrientation:toInterfaceOrientation 持续时间:duration];
        正在旋转=是;
    }

    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
        [超级 didRotateFromInterfaceOrientation:fromInterfaceOrientation];
        正在旋转=否;
    }

现在,为您的事件处理程序执行以下操作:

    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)事件
    {
        if (event.subtype == UIEventSubtypeMotionShake && !isRotating)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }