ios - 在应用程序启动时检测正确的方向

标签 ios ios5

我有一个通用应用程序,带有一个 splitcontroller 和一个从 detailcontroller 的 viewdidload 模态显示的模态(它是一个登录屏幕)

打开 ipad 版本时,我希望能够根据设备的初始方向以纵向或横向启动它。问题是它总是以纵向启动(根据 documentation 预期)。

如果我将设备设置为纵向,然后将设备设置为横向,则可以正常工作。但是,如果我直接横向打开应用程序,它不会。

顺便说一句:我已经在该登录 View Controller 的 (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 处设置了return true

更新: 如果我从 viewDidAppear 而不是 viewDidLoad 执行 segue,模态的方向工作正常,但 SplitController 在模态之前出现一段时间,我该如何避免这种情况?

- (void)viewDidAppear:(BOOL)animated
{
    [self.splitViewController performSegueWithIdentifier:@"toModal" sender:self.splitViewController];
}

最佳答案

您可以有条件地设置(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 的方向。像这样的东西并不漂亮,但它有效:

UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;

if(orientation == 0) //Default orientation 
    //UI is in Default (Portrait) -- this is really a just a failsafe. 
else if(orientation == UIInterfaceOrientationPortrait)
    //Do something if the orientation is in Portrait
else if(orientation == UIInterfaceOrientationLandscapeLeft)
    // Do something if Left
else if(orientation == UIInterfaceOrientationLandscapeRight)
    //Do something if right

否则这篇文章似乎与您正在尝试做的事情相关:

Launching application in landscape orientation for IPad


提案2


您可以创建一个类,该类具有使用以下方法查找设备方向(即“deviceInfo”)的方法:

UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication]statusBarOrientation])

UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication]statusBarOrientation])

您还可以存储任何您想要的拉伸(stretch)系数/尺寸/测量值。然后在 -(id)initWithFrame:(CGRect)frame 中的 view 中,您可以调用您的方法来检查方向(即 deviceInfo.isLandscape? ).然后将 viewautoresizingMask 设置为等于 UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth。然后,在 -(void)layoutSubviews 中将 view.frame 设置为您想要的任何尺寸:

CGRectMake(x-origin, y-origin, width, height);

这是获取当前方向的引用:

http://www.ddeville.me/2011/01/getting-the-interface-orientation-in-ios/

更改 View 框架的尺寸相对简单,可以在 Apple 的文档或一些简单的谷歌搜索中找到。

关于ios - 在应用程序启动时检测正确的方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621689/

相关文章:

ios - 使用 NSUrlSession 的服务器没有响应

iphone - glGenBuffers 和 glGenFramebuffers 和 glGenRenderbuffers 之间有什么区别

ios - Thread 1 :EXC_BAD_ACCESS(code=1, address=0x20004018) 如何解决?

iphone - 页面 View Controller : How to release ViewControllers added to it?

storyboard - iOS 5 segue实现

iphone - ios5 上的自动释放替代方案

ios - 以编程方式获取 View Controller 的实例

ios - 在 Web View 中生成 PDF 时,如何先将 PDF 数据写入文件

ios - TensorFlow-Lite Swift 设置(bridge-header/includes/root 文件夹)

ios block 示例。如何创建 block objective-c