CollectionViewController并使用左/右旋转进行无效

时间:2017-12-30 14:03:55

标签: swift xcode uicollectionview

我创建了最简单的Apps,Xcode 9.2,New Project / Single View App / Next / Next etc 。并用 CollectionViewController 替换 ViewController

这是 CollectionViewController 的完整代码,并注意 self.collectionView!.backgroundColor = UIColor.yellow 将背景颜色设置为黄色。

这很好用。当我在模拟器中旋转iPhone时,它似乎无法使 ViewController 无效,正如黄色看到的那样,当手机处于水平状态时黄色不会延伸手机的整个宽度。

rotated iPhone in simulator

这是我的代码。

import UIKit

class CollectionViewController : UICollectionViewController, UICollectionViewDelegateFlowLayout {

override func viewDidLoad() {
    super.viewDidLoad()
    self.collectionView!.backgroundColor = UIColor.yellow
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    print("test-1")
    let x1 = self.collectionViewLayout;
    //x1.invalidateLayout()   //uncomment this line
    print("test-2")
   }
}

我以为我理解了这一点,我也搜索了Apple论坛和StackOverflow(这里是really relevant prior帖子),看来我需要在代码中手动使集合视图无效。

如果我从行 // x1.invalidateLayout()删除注释 xCode在输出窗口“test-1”和“test-2”中以无限循环打印出来。

这是一个将我推向正确方向的指针,非常受欢迎。很明显,当iPhone在模拟器中旋转时,水平地它也应该以黄色覆盖整个屏幕。

谢谢。

0 个答案:

没有答案