简体   繁体   English

如何以编程方式设置设备(UI)方向?

[英]How to set device (UI) orientation programmatically?

Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa. 希望屏幕上的所有内容(UI)能够从左到右旋转或反之亦然。

How do I go about doing this? 我该怎么做呢? Is this private? 这是私人的吗?

I know that 我知道

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}

lets you say which orientations the UI can be, but is there a way to force only one at a time? 让你说出UI可以有哪些方向,但有没有办法一次只强制一个?

Cheers 干杯

In iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is absent. 在iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]方法不存在。 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];
    }

}

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

That method is called to determine whether your interface should automatically rotate to a given rotation (ie letting UIKit do the hard work, rather than you doing it manually). 调用该方法来确定您的界面是否应该自动旋转到给定的旋转(即让UIKit执行艰苦的工作,而不是手动执行)。

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; 如果您希望UI自动旋转到所有方向,您只需return YES;

Is that what you were asking? 那是你问的吗?

If you present a modal view controller which implements -shouldAutorotateToInterfaceOrientation: to only support one orientation, the whole interface will automatically be forced into it. 如果你提出一个模态视图控制器来实现-shouldAutorotateToInterfaceOrientation:只支持一个方向,整个界面将自动强制进入它。 I don't think there's a way to programmatically change orientation otherwise. 我不认为有一种方法可以以编程方式改变方向。

Also this simple way works: 这种简单的方法也适用:

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

and back: 然后回来:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];

In Swift to change the orientation to portrait: 在Swift中将方向更改为纵向:

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

See: https://stackoverflow.com/a/24259601 请参阅: https//stackoverflow.com/a/24259601

This was addressed by: Guntis Treulands https://stackoverflow.com/a/10146270/894671 这是通过以下方式解决的:Guntis Treulands https://stackoverflow.com/a/10146270/894671

//-----------------------------------
// FORCE PORTRAIT CODE
//-----------------------------------
[[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationPortrait animated:NO];
//present/dismiss viewcontroller in order to activate rotating.
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }

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);
}

As suggested on a related thread shouldAutorotateToInterfaceOrientation is deprecated. 正如在相关线程上建议的那样,不应该使用shouldAutorotateToInterfaceOrientation Newer apps should leverage the following methods as described in Apple's UIViewController Class Reference until they themselves become deprecated: 较新的应用程序应该使用Apple的UIViewController类参考中描述的以下方法,直到它们自己被弃用:

  • shouldAutorotate , shouldAutorotate
  • preferredInterfaceOrientationForPresentation ; preferredInterfaceOrientationForPresentation ; and
  • attemptRotationToDeviceOrientation . attemptRotationToDeviceOrientation

From what I understand it's best to use different View Controllers for views which need to lock into (ie have a preferred) orientation mode which differs from the last. 根据我的理解,最好使用不同的视图控制器来查看需要锁定(即具有首选)定向模式的视图,该模式与上一个不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 7 中以编程方式设置设备方向? - How do I programmatically set device orientation in iOS 7? 如何在 Swift 中以编程方式向项目添加设备方向? - How to add device orientation to project programmatically in Swift? 如何在 Swift 中以编程方式更改设备方向? - How to change Device Orientation programmatically in Swift? 如何在iOS 6中以编程方式更改设备方向 - How to change the device orientation programmatically in iOS 6 如何以编程方式获取设备的当前方向? - How to get current orientation of device programmatically? 如何在设备定向期间设置UITextfield位置 - How to set UITextfield position during device orientation 如何设置iOS设备方向仅景观? - How to set iOS device orientation only landscape? 如何在dismissModalViewController之后设置方向以更正设备方向? - how to set orientation to correct device orientation after dismissModalViewController? 如何在iOS中基于设备方向以编程方式创建视图? - How do I programmatically create views based on device orientation in iOS? 如何在以编程方式创建的设备方向上更改 UIView 子类的框架? - How to change the frame of a UIView subclass on device orientation which is programmatically created?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM