【问题标题】:iOS App UIView rotating, content is notiOS App UIView 旋转,内容不旋转
【发布时间】:2013-03-04 19:28:45
【问题描述】:

我目前正在构建一个 iOS 应用程序,唯一支持的方向是横向左右。我已经通过几种方式指定了这一点:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation); //iOS 5 compatibility.
}
-(NSInteger)supportedInterfaceOrientations:(UIWindow *)window{
    return UIInterfaceOrientationMaskLandscape; //iOS 6
}
-(BOOL)shouldAutorotate{
    return YES;
}

我已在应用程序的 Info.plist 中将支持的方向指定为仅横向的方向。当我在我的设备上(甚至在模拟器中)运行应用程序时,我知道根视图是横向的(切换器位于屏幕的长边,通知中心也是如此),但是内容仍然来自屏幕的左上角,就像在纵向模式下一样,而不是左下角或右上角,就像我想要的那样。我不知道是什么导致内容不随父视图旋转,因为此代码适用于 iOS 5,但不适用于 iOS 6。

编辑:Here's a screenshot.

编辑:这是我的 main.m:

#import <UIKit/UIKit.h>
int main(int argc, char **argv) {
    NSAutoreleasePool *p = [[NSAutoreleasePool alloc] init];
    int ret = UIApplicationMain(argc, argv, @"ClockApplication", @"ClockApplication");
    [p drain];
    return ret;
}

这是我的 ClockApplication.mm:

#import "RootViewController.h"

@interface ClockApplication: UIApplication <UIApplicationDelegate> {
    UIWindow *_window;
    RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end

@implementation ClockApplication
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _viewController = [[RootViewController alloc] init];
    [_window addSubview:_viewController.view];
    [_window makeKeyAndVisible];
}

- (void)dealloc {
    [_viewController release];
    [_window release];
    [super dealloc];
}
@end

【问题讨论】:

  • 我不太清楚您希望它如何呈现...如果可能,发布一些图片,甚至是线框图...
  • @CainaSouza 我添加了问题的截图。
  • 你在使用导航控制器吗?
  • @CainaSouza 不,唯一的 UI 元素是我的根视图控制器(UIViewController)及其 ivars。
  • 您是否厌倦了旋转设备?也许出于某种原因它以纵向模式开始。

标签: ios objective-c


【解决方案1】:

尝试从这里改变你的方法

-(NSInteger)supportedInterfaceOrientations:(UIWindow *)window{
}

到这里

-(NSUInteger)supportedInterfaceOrientations{
}

编辑

尝试像这样在导航控制器中调用您的视图:

UIViewController *yourViewController = [[UIViewController alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:yourViewController];
nc.navigationBarHidden = YES;
[self.navigationController presentModalViewController:nc animated:NO];
[self.navigationController dismissModalViewControllerAnimated:NO];

编辑

#import "RootViewController.h"

@interface ClockApplication: UIApplication <UIApplicationDelegate> {
    UIWindow *_window;
    RootViewController *_viewController;
}
@property (nonatomic, retain) UIWindow *window;
@end

@implementation ClockApplication
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];
    nc.navigationBarHidden = YES;

    _window.rootViewController = nc;
    [_window makeKeyAndVisible];
}

- (void)dealloc {
    [_viewController release];
    [_window release];
    [super dealloc];
}
@end

【讨论】:

  • 抱歉,返回的是NSUInteger而不是NSInteger
  • 另外,在这个方法(具有这个新签名的新方法)设置一个断点并检查它是否被调用。
  • 它到达断点,所以它被调用了。但问题依然存在。
  • 试试我刚才回答的内容
  • ClockApplication 没有 navigationController 属性。
【解决方案2】:

尝试添加:

_window.rootViewController = _viewController;

编辑:无论如何,您的代码看起来像是使用“已弃用”样式。我建议为 iOS6 建立一个新项目并使用它的模板。它在 iOS5 上运行,但 iOS 发生了变化。

【讨论】:

    【解决方案3】:

    只有UIWindow 中的rootViewController 会接收轮换事件。

    [_window setRootViewController:_rootViewController];
    

    查看the apple technical notes

    【讨论】:

    • 谢谢,这似乎是解决我的问题的最简单方法。知道为什么在 iOS 5 上不是这种情况(例如,它在 iOS 5 上运行良好,但在 6 上运行良好)?
    • iOS 6 需要 rootViewController
    猜你喜欢
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多