iPhone SDK:方向(横向和纵向视图)

Dom*_*ess 9 cocoa-touch

好的,我有我的普通应用程序处于纵向模式.我可以强制我的应用程序转到横向模式的视图(使用navigationcontroller和viewcontroller),如下所示:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
Run Code Online (Sandbox Code Playgroud)

但是当我回到主菜单(tableview)时,它会直接回到肖像.我试试这段代码:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用..

有任何想法吗?

kkr*_*zka 9

在UIViewController类中查看函数shouldAutorotateToInterfaceOrientation : . 如果UIView支持方向,则此函数返回YES.如果仅向横向返回YES,则iPhone将自动置于该方向.

以下代码应该这样做.将它放在UIViewController中,该控件控制您想要以横向模式放置的视图.

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


Dom*_*ess 7

为了实现这一点,我将其添加到rootviewcontroller:

- (void)viewWillAppear:(BOOL)animated {
    [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];

}
Run Code Online (Sandbox Code Playgroud)

这似乎现在正在起作用.

  • @Shivan您是否希望手机跳出用户手并旋转不同的方向? (2认同)