简体   繁体   中英

Change Orientation in iOS 9

My app will be launched in Landscape,only one view can change to Portrait, in which there is a button,I click this button to change the orientation to Portrait. It performed as expected on iPad(iOS 8.4,Deployment target: 7.0),and I made an iPhone version,in which I just made some adjust on UI,and it runs on iPhone 6 Plus simulator(Base SDK: 9.1,Deployment target: 7.0),but the button for chagning orientation doesn't work. And I want to know how to implement this in iOS 9; The Below is my code:

- (void)changeOrientation
{
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication]  statusBarOrientation];


     if(orientation==UIInterfaceOrientationLandscapeLeft||orientation==UIInterfaceOrientationLandscapeRight){



    NSNumber *value = [NSNumber  numberWithInt:UIInterfaceOrientationPortrait];

    if (orientation==UIInterfaceOrientationLandscapeLeft) {

        value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    }else {

        value = [NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown];
    }

    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ScreenToPortrait" object:nil];
}else{



    NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
    if (orientation==UIInterfaceOrientationPortrait) {
        value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
    }else {
        value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
    }


    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"ScreenToLandscapeLeft" object:nil];
}

    [UIViewController attemptRotationToDeviceOrientation];

}

This works on iOS9

NSNumber *orientationValue = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue: orientationValue forKey:@"orientation"];
  1. Go to General tab from app target, and enable all orientations.
  2. For all view which support only landscape orientation implement supportedInterfaceOrientations() and return landscape orientation value.

有用的图像添加

seen or set tick on General setting.

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