我有一个应用程序,我想在某些视图中支持设备旋转,但其他在横向模式下没有特别有意义,所以当我交换视图时,我想强制旋转设置为纵向.
在UIDevice上有一个未记录的属性设置器可以完成这个技巧,但显然会产生编译器警告,并且可能会随着SDK的未来版本而消失.
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
Run Code Online (Sandbox Code Playgroud)
是否有任何记录的方法来强制定位?
更新:我想我会提供一个例子,因为我没有找到shouldAutorotateToInterfaceOrientation,因为我已经实现了它.
我希望我的应用程序支持View 1中的横向和纵向,但只支持View 2中的纵向.我已经为所有视图实现了shouldAutorotateToInterfaceOrientation但是如果用户在View 1中处于横向模式然后切换到View 2,我想强制执行手机旋转回肖像.
我试图在我的应用程序中强制只有一个视图在横向模式,我打电话
override func shouldAutorotate() -> Bool {
print("shouldAutorotate")
return false
}
override func supportedInterfaceOrientations() -> Int {
print("supportedInterfaceOrientations")
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.LandscapeLeft
}
Run Code Online (Sandbox Code Playgroud)
视图以纵向模式启动,并在更改设备方向时保持旋转.
永远不会调用shouldAutorotate.
任何帮助,将不胜感激.
UIDevice.orientationinclude UIDeviceOrientationFaceUp和 的可能值UIDeviceOrientationFaceDown.虽然知道设备是扁平的可能是有用的,但这并不能告诉我们它是否是平面显示面向纵向或横向模式的界面.有没有办法在设备返回模糊信息的情况下找到GUI的当前方向?我想我可以跟踪方向变化事件以记住最后的纵向/横向方向或检查主要UIView的边界高度和宽度,但这似乎是kludgey.设备或UIView上有一个我失踪的财产吗?
希望屏幕上的所有内容(UI)能够从左到右旋转或反之亦然.
我该怎么做呢?这是私人的吗?
我知道
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {}
Run Code Online (Sandbox Code Playgroud)
让你说出UI可以有哪些方向,但有没有办法一次只强制一个?
干杯
我开发了iOS应用程序并在iOS6设备上进行了测试.在测试时,我意识到我的应用程序没有按预期响应方向更改.
这是我的代码:
// Autorotation (iOS >= 6.0)
- (BOOL) shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)
确切地说,我想知道iOS中的方向更改调用了哪些方法.
我的应用程序中的所有视图控制器仅在纵向工作,除了可以是纵向或横向的视图控制器.
我有一些使用场景如下:
执行这些操作后,应用程序将保持横向方向,并且不会自动将其更改为纵向.
我使用supportedInterfaceOrientations控制视图控制器方向(我使用的是iOS 6.0).我做错了什么?当用户按下后退按钮时应用程序自动将方向更改为允许时,如何才能获得正确的行为?谢谢你的答案!
我正在使用Ios6中的iPad应用程序,当我们点击右边的按钮时,我正在执行如下操作:
-(IBAction)camerabuttonAction:(id)sender
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];
[self.popoverController presentPopoverFromRect:CGRectMake(50, -250, 500, 300) inView:appDelegate.splitview.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当我在Land scape模式时,如果我点击按钮.每次使用后,相机将以纵向模式显示(图像以反向模式显示).但是,如果我摇动iPad,它会在LandScape中显示,即正确的方向.
见下图
当我处于Land scape模式时,如果我点击按钮,相机会显示如下图像:

如果我摇动iPad,那么相机会显示如下图像:

我已经尝试了很多并用Google搜索,但我找不到任何解决方案.这是在节省我的时间,所以如果有人工作,请指导我并发布示例代码.
我尝试了下一个:
UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)
上
viewWillAppear
Run Code Online (Sandbox Code Playgroud)
但它不起作用.如何在viewWillAppear上旋转屏幕?
UPDATE
我使用下一个代码来锁定方向:
var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 4.0
private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
if shouldRotate == true {
return Int(UIInterfaceOrientationMask.portrait.rawValue)
} else {
return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
}
}
Run Code Online (Sandbox Code Playgroud)
在我的第一个控制器viewWillAppear我设置:
if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)
在我的SECOND控制器中:
if let appDelegate = …Run Code Online (Sandbox Code Playgroud) ios ×5
iphone ×5
objective-c ×3
cocoa-touch ×2
orientation ×2
swift ×2
ios6 ×1
ipad ×1
ipad-3 ×1
landscape ×1