OGeek|极客世界-中国程序员成长平台

标题: ios - pop segue 后方向未更新 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:07
标题: ios - pop segue 后方向未更新

我的设置

我有一个 UICollectionViewController,它显示 纵向 2 列横向 3 列,就像这样:

Portrait Orientation . . . . Landscape Orientation

我正在使用此代码在旋转时触发布局刷新:

- (void)updateViewConstraints
{
    [super updateViewConstraints];
    [[self.collectionView collectionViewLayout] invalidateLayout];
}

一切正常,但是...

问题

1.我正在纵向查看collectionView:

Portrait Orientation

一切都好。

2.我单击其中一个单元格以转到详细 View ,该 View 也支持两种方向。然后在详细 View 中,我将方向切换为横向。还是不错的。

3. 还是在 Landscape 中,我现在点击 回到 collectionView,然后我看到的是这样的:

The error哎呀!

我完全不知道这是什么原因。在 SO 和 Google 上搜索没有出现任何结果。

我的尝试

我尝试在 collectionVC.m 文件中实现此方法:

- (void)viewWillTransitionToSizeCGSize)size withTransitionCoordinatorid<UIViewControllerTransitionCoordinator>)coordinator
{
   [super blahblah];
   [[self.collectionView collectionViewLayout] invalidateLayout];
}

虽然即使我在 detailView 上也会调用它,但它并不能解决问题。

注意事项:

  1. 除了我在上面发布的两个之外,我没有实现任何与方向相关的应该/将/已完成的方法。
  2. 我注意到的一件事是,collectionView 正在切换到 2 列布局。看起来它的 View 边界变得困惑了。

Potato对不起,很长的帖子。这是一个土 bean 。

那么你有什么想法吗?

更新:我抓到了 screenshot of the UI hierarchy ,可能会有所帮助。



Best Answer-推荐答案


您面临的问题是,当集合 viewController 不可见时,旋转确实发生了。所以 updateViewConstraintsviewWillTransitionToSize:withTransitionCoordinator: 都不会被调用。

这个问题有两种可能的解决方案。

你可以观察到方向的变化

 // in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(orientationDidChange name:UIDeviceOrientationDidChangeNotification object:nil];

 - (void)orientationDidChangeNSNotification *)notification
  {
       //TODO: resetup the layout itemSize, etc if your it depend on the collection bounds.
       // and make sure to do that after the rotation did happend(i.e  in viewWillAppear or orientationDidChange
      [[self.collectionView collectionViewLayout] invalidateLayout];
  }

您可以在 viewWillAppear 中使布局无效

     -(void)viewWillAppearBOOL)animated  
     {  
       [super viewWillAppear:animated];  
        //TODO: resetup the layout itemSize, etc if your it depend on the collection bounds.
       // and make sure to do that after the rotation did happend(i.e  in viewWillAppear or orientationDidChange
       [[self.collectionView collectionViewLayout] invalidateLayout];
     }

关于ios - pop segue 后方向未更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34739534/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4