【问题标题】:UIView stuck in portrait modeUIView 卡在纵向模式
【发布时间】:2012-10-03 14:22:44
【问题描述】:

我无法以横向模式启动应用程序。视图始终以纵向模式显示。

我在 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 ;

【问题讨论】:

  • 你在为 iOS 6 开发吗??
  • 使用最新的 Xcode 4.5 和 iOS 5.0
  • 看来我忘了初始化 UIViewController。在那之后它起作用了。

标签: objective-c ios uiview uiviewcontroller autoresizingmask


【解决方案1】:

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

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

【讨论】:

  • 感谢您确认它适用于 iOS6。如果没有 autoresizingMask,我无法让它在 iOS 5/5.1 上运行。
  • 使用 iPhone 3GS 和 Ios 5.1(我妻子的电话)我不得不改变 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight )); } 而不是像下一个答案一样返回 YES
  • 这很奇怪,即使改变它并且没有 autoresizingMask,我仍然停留在纵向模式。状态栏在右侧。
【解决方案2】:

使用它在启动时找到方向..它会正常工作:

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

然后使用这个方向来旋转你的视图

【讨论】:

    【解决方案3】:

    在您的ViewController 中替换此代码:

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||(interfaceOrientation == UIInterfaceOrientationLandscapeRight));
    } 
    

    【讨论】:

    • 返回YES没有区别
    • return yes 返回值:1- UIInterfaceOrientationLandscapeLeft, 2-UIInterfaceOrientationLandscapeRight, 3-UIInterfaceOrientationPortrait,4- UIInterfaceOrientationPortraitUpsideDown
    • 在 ipad 6.0 模拟器中尝试过,确实有效。但不在 iPad 5.0 模拟器或我的 5.1 设备中。
    • 因为 myView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ;它适用于 ios 6
    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    相关资源
    最近更新 更多