首页 文章

在iOS 8中处理方向更改的“正确”方法是什么?

提问于
浏览
97

有人可以告诉我在iOS 8中使用纵向和横向界面方向的“正确”或“最佳”方法吗?似乎我想要用于此目的的所有功能都在iOS 8中被弃用,而我的研究似乎没有明确,优雅的替代方案 . 我真的应该看看宽度和高度,以确定我们是在纵向还是横向模式?

例如,在我的视图控制器中,我应该如何实现以下伪代码?

if we are rotating from portrait to landscape then
  do portrait things
else if we are rotating from landscape to portrait then
  do landscape things

4 回答

  • 2

    Apple建议使用大小类作为可用屏幕空间大小的粗略度量,以便UI可以显着改变其布局和外观 . 考虑到纵向的iPad具有与横向相同的尺寸等级(常规宽度,常规高度) . 这意味着您的UI在两个方向之间应该或多或少相似 .

    但是,iPad中从纵向到横向的变化非常重要,即使尺寸类没有改变,您也可能需要对UI进行一些较小的调整 . 由于不推荐使用 UIViewController 上与接口方向相关的方法,Apple现在建议在 UIViewController 中实现以下新方法作为替代:

    - (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
    {
        [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];
    
        // Code here will execute before the rotation begins.
        // Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]
    
        [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    
            // Place code here to perform animations during the rotation.
            // You can pass nil or leave this block empty if not necessary.
    
        } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {
    
            // Code here will execute after the rotation has finished.
            // Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:]
    
        }];
    }
    

    大!现在你在旋转开始之前以及完成之后得到回调 . 但实际上知道旋转是纵向还是横向呢?

    Apple建议将旋转视为父视图大小的变化 . 换句话说,在iPad从纵向旋转到横向旋转期间,您可以将其视为根级视图,只需将其 bounds.size{768, 1024} 更改为 {1024, 768} 即可 . 知道了这一点,你应该使用 size 传递到上面的 viewWillTransitionToSize:withTransitionCoordinator: 方法来判断你是否正在旋转到纵向或横向 .

    如果您想要一种更加无缝的方式将遗留代码迁移到新的iOS 8工作方式,请考虑在UIView上使用this simple category,它可用于根据视图大小确定视图是"portrait"还是"landscape" .

    回顾一下:

    • 您应该使用大小类来确定何时显示根本不同的UI(例如"iPhone-like" UI与"iPad-like" UI)

    • 如果在大小类未更改但容器(父视图)大小发生变化时(例如iPad旋转时)需要对UI进行较小的调整,请在UIViewController中使用 viewWillTransitionToSize:withTransitionCoordinator: 回调 .

    • 应用程序中的每个视图都应该仅根据布局给出的空间做出布局决策 . 让视图的自然层次结构将此信息向下级联 .

    • 同样,不要使用 statusBarOrientation (基本上是设备级属性)来确定是否为"portrait" vs "landscape"布局视图 . 状态栏方向只应由处理 UIWindow 之类的代码使用,这些代码实际上位于应用程序的根本级别 .

  • 247

    基于smileyborg非常详细(和接受)的答案,这是使用swift 3的改编:

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        super.viewWillTransition(to: size, with: coordinator)
        coordinator.animate(alongsideTransition: nil, completion: {
            _ in
            self.collectionView.collectionViewLayout.invalidateLayout()
        })        
    }
    

    UICollectionViewDelegateFlowLayout 实施中,

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        // retrieve the updated bounds
        let itemWidth = collectionView.bounds.width
        let itemHeight = collectionView.bounds.height
        // do whatever you need to do to adapt to the new size
    }
    
  • 11

    我只是使用通知中心:

    Add an orientation variable (will explain at end)

    //Above viewdidload
    var orientations:UIInterfaceOrientation = UIApplication.sharedApplication().statusBarOrientation
    

    Add Notification when view appears

    override func viewDidAppear(animated: Bool) {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationChanged:", name: UIDeviceOrientationDidChangeNotification, object: nil)
    }
    

    Remove Notification when view goes away

    override func viewWillDisappear(animated: Bool) {
            NSNotificationCenter.defaultCenter().removeObserver(self, name: UIDeviceOrientationDidChangeNotification, object: nil) 
    }
    

    Gets current orientation when notification is triggered

    func orientationChanged (notification: NSNotification) {
        adjustViewsForOrientation(UIApplication.sharedApplication().statusBarOrientation)
    }
    

    Checks orientation (portrait/landscape) and handles events

    func adjustViewsForOrientation(orientation: UIInterfaceOrientation) {
        if (orientation == UIInterfaceOrientation.Portrait || orientation == UIInterfaceOrientation.PortraitUpsideDown)
        {
            if(orientation != orientations) {
                println("Portrait")
                //Do Rotation stuff here
                orientations = orientation
            }
        }
        else if (orientation == UIInterfaceOrientation.LandscapeLeft || orientation == UIInterfaceOrientation.LandscapeRight)
        {
           if(orientation != orientations) {
                println("Landscape")
                //Do Rotation stuff here
                orientations = orientation
            }
        }
    }
    

    我添加方向变量的原因是因为在物理设备上进行测试时,方向通知会在设备中的每次轻微移动时调用,而不是在旋转时调用 . 添加var和if语句只会在切换到相反方向时调用代码 .

  • 14

    从UI的角度来看,我认为使用Size Classes是Apple推荐的处理不同方向,大小和规模的接口的方法 .

    请参阅以下部分:特征描述界面的大小等级和比例:https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS8.html

    “iOS 8增加了新功能,使处理屏幕尺寸和方向更加通用 . ”

    这篇也是一篇好文章:https://carpeaqua.com/thinking-in-terms-of-ios-8-size-classes/

    EDIT 更新链接:https://carpeaqua.com/2014/06/14/thinking-in-terms-of-ios-8-size-classes/(图片来源:Koen)

相关问题