UIView陷入了纵向模式

时间:2012-10-12 18:01:57

标签: objective-c ios uiview uiviewcontroller autoresizingmask

我在横向模式下启动应用时遇到问题。视图始终以纵向模式显示。

我在plist中配置了以下内容:

<key>UISupportedInterfaceOrientations</key>
<array>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
</array>

UIViewController在AppDelegate.m中创建:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myViewController = [MyViewController alloc];

    UIView *myView = [[UIView alloc] init];
    myView = [UIColor whiteColor];
    self.myViewController.view = myView;

    self.window.rootViewController = self.myViewController;
    self.window makeKeyAndVisible];
    return YES;
}

将shouldAutorotateToInterfaceOrientation添加到MyViewController.m

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

当设备处于纵向模式且应用程序启动时,状态栏处于横向模式。但是当显示UIView时,它将以纵向模式结束。当设备处于横向模式时,会发生同样的事情。

但是,当我在Interface Builder中创建UIViewController时,它可以工作。

这是否与autoresizemasking相关,因为当我在AppDelegate.m中添加它时它不起作用:

 myView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;

3 个答案:

答案 0 :(得分:0)

ViewController

中替换此代码
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
} 

答案 1 :(得分:0)

使用它来查找启动时的方向..它将正常工作:

UIInterfaceOrientation orientation = [UIDevice currentDevice].orientation;
if (! UIDeviceOrientationIsValidInterfaceOrientation(orientation))
    orientation = [UIApplication sharedApplication].statusBarOrientation;

然后使用此方向旋转视图

答案 2 :(得分:0)

在你的代码中我发现了语法错误,我认为你的意思是myView.backgroundColor = [UIColor whiteColor]; 但这是唯一的错误,你的代码工作正常,应用程序启动并保持在横向模式,有或没有myView.autoresizingMask

我的环境: xCode 4.5 设备iPhone 4S与IOS 6.0