gpt4 book ai didi

ios - 问答 - 如何强制设备方向在横向和纵向之间旋转

转载 作者:行者123 更新时间:2023-11-29 11:36:06 26 4
gpt4 key购买 nike

I'm posting this in Q&A style as there are currently a few posts on S/O with similar questions and answers that aren't perfectly correct for all use cases. They typically suffice OP's one specific use case but are bad for people coming to the site searching for a general-use answer and lead to hours of frustrated debugging, as I just experienced. (And in doing so scourged through all of StackOverflow's resources to find that this question (and its answer) had not been officially asked anywhere except in comments on other posts).


问:如何强制我的应用在纵向和横向模式之间随意切换?

虽然有许多微小的片段允许这样做,但它们会以各种无法预料的方式失败。

例如:

1) 您可以旋转屏幕 UI(不是设备方向),但是如果您截屏或显示 iOS 原生内容(例如弹出警报),它们将以错误的方式定向。

2) 如果您将 orientation 键设置为所需的方向,UI 并不总是会在之后(或根本不会)自动更新。

3) 如果您只是手动执行此操作并手动更新 UI,其他 VC 可能仍会篡改方向,它不会“锁定”。

4) 如果您手动更新设备方向并刷新 UI,添加新的 ViewController.view 将翻转宽度和高度,因为设备设置尚未更新,即使 UI 已更新,您也必须等待未知的时间在更新这些属性(UIDevice 的维度)之前完成旋转动画的时间(0 到 1 秒之间)。

等等

我在下面的回答解决了在搜索各种 SO 线程时发现的所有潜在问题。

最佳答案

答:

添加到 AppDelegate.h

@property () BOOL landscape;

添加到 AppDelegate.m

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (self.landscape) {
return UIInterfaceOrientationMaskLandscapeRight;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}


-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
if (self.landscape) {
return UIInterfaceOrientationLandscapeRight;
} else {
return UIInterfaceOrientationPortrait;
}
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if (self.landscape) {
return (UIInterfaceOrientationLandscapeRight);
} else {
return (UIInterfaceOrientationPortrait);
}
}

-(NSUInteger)supportedInterfaceOrientations {
if (self.landscape) {
return UIInterfaceOrientationLandscapeRight;
} else {
return UIInterfaceOrientationPortrait;
}
}

-(BOOL)shouldAutorotate {
return NO;
}

添加到你的VC.m

-(void)setLandscape:(BOOL)landscape {

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.landscape = landscape;

if (landscape) {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
} else {
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}

[UINavigationController attemptRotationToDeviceOrientation];//THIS IS THE MOST IMPORTANT LINE IN HERE AND EVERYONE AND ALL SAMPLE CODE LEAVES IT OUT FOR SOME REASON, DO NOT REMOVE THIS LINE. (Forces UI to update), otherwise this orientation change will randomly fail around 1% of the time as UI doesn't refresh for various unknown reasons.

}

最重要的一行是 [UINavigationController attemptRotationToDeviceOrientation]; 由于某种原因,网络上的每个人、stackoverflow、示例代码等都遗漏了它。只需设置 orientation 键将导致它在 98% 的时间内工作,但随机 UI 不会更新或者它会在设置键之前更新并且你会遇到方向错误,这会强制它更新当你需要的时候。

关于ios - 问答 - 如何强制设备方向在横向和纵向之间旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49269499/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com