【问题标题】:Objective-c device orientationObjective-c 设备方向
【发布时间】:2014-02-19 21:35:22
【问题描述】:

我正在构建一个模板应用程序。我在设备旋转/方向方面遇到了一些问题。我的 appDelegate didFinishLaunching 中有以下内容:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

在我的 VC viewDidLoad 中:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];

...

//add the title bar
    titleBar = [[OAI_TitleBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 1024.0, 50.0)];
    titleBar.titleBarTitle = @"OAI Template";
    titleBar.hasAccount = YES;
    titleBar.hasReset = YES;
    titleBar.hasHome = YES;
    [titleBar buildTitleBar];
    [self.view addSubview:titleBar];

这是我的 deviceOrientationDidChange 方法:

//rotate the vc view
    if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
        [self.view setFrame:CGRectMake(0.0, 0.0, 1024.0, 768.0)];

    } else if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)) {

        [self.view setFrame:CGRectMake(0.0, 0.0, 768.0, 1024.0)];
    }

    //call the rotation management methods of various classes
    [titleBar adjustForRotation:orientation];

}

当我旋转设备时,这工作正常,视图按预期调整。但是当应用程序启动时,x,y 坐标是错误的 - 当我在纵向启动时记录框架时,VC 视图的框架是 {{0, 20}, {768, 1004}},当以横向模式启动时是 {{20, 0}, {748, 1024}}

标题栏框架是 {{20, 0}, {748, 1024}} 用于横向和纵向(正如我编码的那样)。

但是,我在模拟器和设备上看到的情况截然不同。您应该看到顶部有一个黑条(高度为 50 像素左右),左侧有一个徽标,然后是一些按钮,然后应用程序标题向右对齐。从下图中可以看出,当应用程序以任一方向启动时,它的打开 x/y 坐标都不会接近 0/20。任何帮助,将不胜感激。

在我看来,当应用程序以横向启动时,它会显示为纵向,而当它以纵向启动时,虽然显示是正确的,但它会偏离大约 20.0f 左右。任何帮助,将不胜感激。

【问题讨论】:

  • 好的,所以纵向解决方案很明显,它应该重置为 0、20、1004、768 但我仍然遇到横向视图问题,尽管我现在可以看到标题栏, vc 视图本身正在从设备的左边缘加载到大约 200.0f 处。

标签: ios orientation


【解决方案1】:

解决了,我

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

在我的 viewController 中,确保正确的方向在我的 plist 中并且加载正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    相关资源
    最近更新 更多