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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4330979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:12:32  来源:igfitidea点击:

How to set device (UI) orientation programmatically?

iosiphoneobjective-corientation

提问by Sam Jarman

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

干杯

采纳答案by tobyc

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).

调用该方法以确定您的界面是否应该自动旋转到给定的旋转(即让 UIKit 完成繁重的工作,而不是您手动完成)。

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

因此,如果您希望您的应用程序仅在横向运行,您可以使用以下方法实现该方法的主体:

return UIInterfaceOrientationIsLandscape(interfaceOrientation);

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?

这就是你问的吗?

回答by UIBuilder

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

在 iOS[[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation]方法中不存在。但是我们可以使用状态栏旋转视图,为此:

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

}

回答by Chris

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

回答by Justin Spahr-Summers

If you present a modal view controller which implements -shouldAutorotateToInterfaceOrientation:to only support one orientation, the whole interface will automatically be forced into it. I don't think there's a way to programmatically change orientation otherwise.

如果您提供一个实现-shouldAutorotateToInterfaceOrientation:仅支持一个方向的模态视图控制器,则整个界面将自动强制进入它。我认为没有办法以编程方式改变方向。

回答by Cristian Radu

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

回答by Pavel

Also this simple way works:

这种简单的方法也有效:

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

and back:

然后回来:

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

回答by to0nice4eva

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

回答by hariszaman

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }

回答by byJeevan

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

回答by Josh Habdas

As suggested on a related threadshouldAutorotateToInterfaceOrientationis deprecated. Newer apps should leverage the following methods as described in Apple's UIViewController Class Referenceuntil they themselves become deprecated:

作为一个相关的线程建议shouldAutorotateToInterfaceOrientation已被弃用。较新的应用程序应该利用 Apple 的UIViewController 类参考中描述的以下方法,直到它们本身被弃用:

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

From what I understand it's best to use different View Controllers for views which need to lock into (i.e. have a preferred) orientation mode which differs from the last.

据我所知,最好对需要锁定(即具有首选)方向模式的视图使用不同的视图控制器,该模式与上一个不同。