我正在使用ActivityInstrumentationTestCase2类为应用程序编写一些验收测试.我想在测试中引起方向更改,以确保发生许多事情.其中包括确保保留Activity状态,同时我还要确保使用适当的方向布局.
我知道我可以简单地测试onSaveInstanceState/onRestoreInstanceState/onPause/onResume/etc. 确保保留实例状态的方法.但是,我想知道是否存在导致方向改变事件的机制?
这是否会涉及注入某种运动事件来诱骗设备/仿真器认为它已被旋转,或者是否存在由仪器提供的实际方法?
谢谢和干杯!
testing instrumentation android screen-orientation uiinterfaceorientation
我的iOS 7应用程序正常工作.今天我用Xcode 6尝试了它,当我运行它时,我有一个令人讨厌的惊喜:(:
您可以看到Xcode现在绘制我的viewController,就像它处于纵向模式一样,但正如您所看到的那样是横向模式.
你知道iOS 8中有什么变化吗?:/我使用了以下定位方法:
编辑:
我发现这个方法:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskLandscape;
}
Run Code Online (Sandbox Code Playgroud)
现在我的第一个viewcontrollers正常工作:).问题是当我显示一个模态(改变UIInterfaceOrientationMaskAll中的方法集)时重新制作截图的丑陋效果.我正是在这种模式下改变我的方法:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if(self.restrictRotation)
{
return UIInterfaceOrientationMaskLandscape;
}
else
{
return UIInterfaceOrientationMaskAll;
}
}
// THIS IS A DELEGATE OF MODAL VIEW CONTROLLER
- (void)updateInterfaceAfterDocumentPreviewViewController
{
NSLog(@"updateInterfaceAfterDocumentPreviewViewController");
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft];
AppDelegate* appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
appDelegate.restrictRotation = YES;
}
Run Code Online (Sandbox Code Playgroud) 如何使用Size类设计具有不同布局的iPad横向和纵向屏幕.我只能找到两个方向的w-regular和h-regular.示例:我需要使用Size Class在纵向和横向上垂直对齐2个视图
补充: 我看到我的问题经常在没有赞成的情况下被查看,所以我决定你们没有得到你搜索的内容.重定向到有关如何处理iOS6中的方向更改的答案非常好的问题
方向变化的具体要求: 限制轮换
欢迎Upvotes :)
我已经从Master Detail模板创建了一个新项目,并尝试以横向方向启动它.如你所知
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
方法已弃用,我们必须使用
- (NSUInteger)supportedInterfaceOrientations
和/或
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
这是我的代码:
- (NSUInteger)supportedInterfaceOrientations {
NSLog(@"supported called");
return UIInterfaceOrientationMaskAll;//Which is actually a default value
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
NSLog(@" preferred called");//This method is never called. WHY?
return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样,我试图在首选方法中返回横向,但它永远不会被调用.ps文档说明:
讨论系统在全屏显示视图控制器时调用此方法.当视图控制器支持两个或更多方向时,您可以实现此方法,但内容在其中一个方向中最佳.
如果视图控制器实现此方法,则在显示时,其视图将以首选方向显示(尽管稍后可以将其旋转到另一个受支持的旋转).如果未实现此方法,系统将使用状态栏的当前方向显示视图控制器.
所以,问题是:为什么永远不会调用prefferredOrientation方法?我们应该如何在不同的控制器中处理不同的方向?谢谢!PS不会将问题标记为重复.我调查了所有类似的问题,他们没有我的答案.
根据 iOS 和 iPadOS 16 Beta 3 发行说明:- 尝试在 \xc2\xa0 UIDevice \xc2\xa0via\xc2\xa0上设置方向setValue:forKey:\xc2\x80\x99 不受支持且不再有效。相反,他们说使用:preferredInterfaceOrientationForPresentation。
preferredInterfaceOrientationForPresentation就我而言,通过使用或 ,强制视图控制器方向在 iOS 16 beta 中不起作用requestGeometryUpdate。
以前,UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,\xc2\xa0forKey: "orientation")工作正常。
我的应用程序由后台UIView中的工具栏和AVCaptureVideoPreviewLayer组成.我想看看关于设备方向的工具栏旋转,所以在我的主ViewController中,我实现了这个功能:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait
|| interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
Run Code Online (Sandbox Code Playgroud)
这很好用,但是当界面方向改变时,我看到背景UIView(带有视频层)也在旋转,所以我的视频视图现在向左或向右旋转90度......这真的很酷!
所以我的问题是:有没有办法在特定的UIView上禁用自动旋转动画?
我已经看到了一个类似的问题:为单个UIView禁用自动旋转,但答案不适合我的问题,我真的需要背景视图不要移动,不要用一种"计数器来解决问题动画".所以我决定提出这个话题.
有任何想法吗 ?
谢谢 !
该[[UIDevice currentDevice] orientation]方法返回超出纵向和横向方向的多个方向.我很清楚检查返回的方向是否"有效",不幸的是,当我的应用程序需要特定的方向来确定运行哪些方法时返回的方向不是"有效",我无法知道哪种方法是适当.
我已经尝试了很多解决方案,但是现在我唯一能够解决的问题是检查方向是否正确LandscapeLeft或者LandscapeRight如果不是我认为屏幕是纵向的.不幸的是,当设备面朝上并且屏幕处于横向时,情况并非如此.
我试图使用父视图控制器的方向:
parentController.interfaceOrientation;
Run Code Online (Sandbox Code Playgroud)
不幸的是它返回UIDeviceOrientationUnknown.我搜索SO和谷歌搜索遇到此问题的其他人.看来愚蠢,我认为苹果将有什么,但LandscapeLeft/Right和PortraitUp/Down他们的方位.
我真正需要的是APPS方向而不是设备,因为这些有时是不一致的.有什么建议?
加载游戏中心时,其默认方向为纵向.为了将其锁定在横向模式中,添加了一个类别.
@implementation GKMatchmakerViewController (LandscapeOnly)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return ( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (BOOL)shouldAutorotate {
return NO;
}
@end
Run Code Online (Sandbox Code Playgroud)
它在iOS 6下面工作正常.但在iOS6中显示错误.
由于未捕获的异常'UIApplicationInvalidInterfaceOrientation'而终止应用程序,原因:'支持的方向与应用程序没有共同的方向,并且shouldAutorotate返回YES'
请解释一下解决方案.
我在用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
委托根据方向类型更改视图的框架
即
if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
self.view.frame=CGRectMake(0,0,500,300);
}
else
{
self.view.frame=CGRectMake(0,0,300,400);
}
Run Code Online (Sandbox Code Playgroud)
如何在iOS 6中处理相同的情况
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Run Code Online (Sandbox Code Playgroud)
已在iOS6中弃用.
我正在使用以下委托来设置所有方向.
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationAllMask;
}
Run Code Online (Sandbox Code Playgroud)
但,
-(BOOL)shouldAutoRotate
{
return YES;
}
Run Code Online (Sandbox Code Playgroud)
没有被调用.如何处理这种情况?

我有一个项目使用UINavigationController和segues工作正常,所有这些都正确旋转,事情是......我只是想禁用autorotation特定的UIViewController.我试过这个:
- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation {
return NO;
}
// New Autorotation support for iOS 6.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我UIViewController会自动旋转,欢迎任何帮助:)
iphone ×5
ios ×4
ios6 ×4
ipad ×3
objective-c ×2
android ×1
autorotate ×1
cocoa-touch ×1
game-center ×1
ios16 ×1
iphone-5 ×1
size-classes ×1
swift ×1
testing ×1
uiview ×1
xcode ×1