简体   繁体   中英

Objective-c device orientation

I am building a template app. I am having some issues with the device rotation/orientation. I have the following in my appDelegate didFinishLaunching:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

and in my 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];

This is my deviceOrientationDidChange method:

//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];

}

This works fine when I rotate the device, the views adjust as expected. But when the app launches, the x,y coordinates are wrong - when I log the frame when launching in portrait orientation, the VC view's frame is {{0, 20}, {768, 1004}}, when launching in landscape mode it is {{20, 0}, {748, 1024}}

The titlebar frame is {{20, 0}, {748, 1024}} for landscape and portrait (which is as I coded it).

However, what I am seeing in the simulator and on my device is drastically different. What you should see is a black bar (50 pixels or so in height) at the top with a logo on the left, followed by some buttons and then the app title aligned right. As you can see from the images below, when the app launches in either orientation it's opening x/y coords are no where near 0/20. Any help would be appreciated.

What it looks like to me is that when the app launches in landscape it is being displayed as portrait and when it launches as portrait, though the display is right, it is off by about 20.0f or so. Any help would be appreciated.

横向屏幕截图

人像屏幕截图

Solved it, I

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

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

in my viewController, made sure the correct orientations were in my plist and it loads fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM