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

发布于 2024-10-05 16:51:32 字数 249 浏览 4 评论 0原文

希望屏幕(UI)上的所有内容都能够从横向左向右旋转,反之亦然。

我该怎么做呢?这是私人的吗?

我知道这

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

可以让你说出 UI 可以是哪个方向,但是有没有办法一次只强制一个方向?

干杯

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 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?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

爺獨霸怡葒院 2024-10-12 16:51:32

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

}

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

}
寂寞陪衬 2024-10-12 16:51:32

我在这方面取得了成功:

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

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];
}
深海少女心 2024-10-12 16:51:32

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

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


返回 UIInterfaceOrientationIsLandscape(interfaceOrientation);

如果您希望 UI 自动旋转到所有方向,您可以
<代码>
返回是;

这就是你问的吗?

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?

断桥再见 2024-10-12 16:51:32

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

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.

完美的未来在梦里 2024-10-12 16:51:32

这种简单的方法也有效:

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

然后返回:

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

Also this simple way works:

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

and back:

[[UIApplication sharedApplication] setStatusBarHidden:NO];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
半边脸i 2024-10-12 16:51:32

在 Swift 中将方向更改为纵向:

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

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

In Swift to change the orientation to portrait:

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

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

此刻的回忆 2024-10-12 16:51:32

解决者: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];

This was addressed by: 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];
当爱已成负担 2024-10-12 16:51:32
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        objc_msgSend([UIDevice currentDevice], @selector(setOrientation:),    UIInterfaceOrientationLandscapeLeft );
    }
恋竹姑娘 2024-10-12 16:51:32

尽管这不是一个闲置的解决方案,但对我来说效果很好。

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

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);
}
陌路黄昏 2024-10-12 16:51:32

正如相关线程上的建议 shouldAutorotateToInterfaceOrientation已弃用。较新的应用程序应利用 Apple 的 UIViewController 中描述的以下方法类引用,直到它们本身被弃用:

  • shouldAutorotate
  • preferredInterfaceOrientationForPresentation;和
  • attemptRotationToDeviceOrientation

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

As suggested on a related thread shouldAutorotateToInterfaceOrientation is deprecated. Newer apps should leverage the following methods as described in Apple's UIViewController Class Reference until they themselves become deprecated:

  • shouldAutorotate,
  • preferredInterfaceOrientationForPresentation; and
  • 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文