How to set device (UI) orientation programmatically?

后端 未结 10 1486
感情败类
感情败类 2020-11-30 01:14

Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa.

How do I go about doing this? Is this private?

I

相关标签:
10条回答
  • 2020-11-30 01:46

    That method is called to determine whether your interface should automatically rotate to a given rotation (i.e letting UIKit do the hard work, rather than you doing it manually).

    So if you wanted your app to only work in landscape you'd implement the body of that method with:

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);

    If you wanted your UI to auto rotate to all orientations you could just return YES;

    Is that what you were asking?

    0 讨论(0)
  • 2020-11-30 01:46

    I've had success with this:

     if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {
        // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation
        // http://openradar.appspot.com/radar?id=697
        // [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; // Using the following code to get around apple's static analysis...
        [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait];
    }
    
    0 讨论(0)
  • 2020-11-30 01:47

    Even though this is not an idle solution, works well for me.

    - (void)viewDidLoad
    {
       self.view.transform = CGAffineTransformIdentity;
       self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0)); 
       self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
    }
    
    0 讨论(0)
  • 2020-11-30 01:52

    Also this simple way works:

    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    

    and back:

    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
    
    0 讨论(0)
  • 2020-11-30 01:54

    In Swift to change the orientation to portrait:

    UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
    

    See: https://stackoverflow.com/a/24259601

    0 讨论(0)
  • 2020-11-30 01:58

    In iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is absent. But we can rotate a view with status bar, for this:

    - (void)showAlbum {
        // check current orientation
        if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) {
            // no, the orientation is wrong, we must rotate the UI
            self.navigationController.view.userInteractionEnabled = NO;
            [UIView beginAnimations:@"newAlbum" context:NULL];
            [UIView setAnimationDelegate:self];
            // when rotation is done, we can add new views, because UI orientation is OK
            [UIView setAnimationDidStopSelector:@selector(addAlbum)];
            // setup status bar
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];
            // rotate main view, in this sample the view of navigation controller is the root view in main window
            [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];
            // set size of view
            [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];
            [UIView commitAnimations];
        } else {
            [self addAlbum];
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题