更新设备上的UICollectionView宽度在Swift中旋转

时间:2016-02-05 20:54:16

标签: ios swift

我有自定义键盘和UICollectionView。单元格没有定义的大小。将设备从纵向旋转到横向时,键盘会适当调整大小,但UICollectionView保持原始宽度。

解决更新宽度以扩展到完全可用的最佳方法是什么?如何将约束更新到可用空间?

UICollectionView是以编程方式创建的,我没有使用故事板以这种方式实现AutoLayout。

override func viewDidAppear(animated: Bool) {
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)

    collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)

    collectionView.dataSource = self
    collectionView.delegate = self

    collectionView.registerNib(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: kbReuseIdentifier)
    collectionView.backgroundColor = UIColor.whiteColor()
    self.collectionView.translatesAutoresizingMaskIntoConstraints = true

    self.view.addSubview(collectionView)

    let collectionViewBottomConstraint = NSLayoutConstraint(item: self.collectionView, attribute: .Bottom, relatedBy: .Equal, toItem: self.view, attribute: .Bottom, multiplier: 1.0, constant: -40.0)
    let collectionViewTopConstraint = NSLayoutConstraint(item: self.collectionView, attribute: .Top, relatedBy: .Equal, toItem: self.view, attribute: .Top, multiplier: 1.0, constant: 10)
    let collectionViewLeftConstraint = NSLayoutConstraint(item: self.collectionView, attribute: .LeadingMargin, relatedBy: .Equal, toItem: self.view, attribute: .LeadingMargin, multiplier: 1.0, constant: 0)
    let collectionViewRightConstraint = NSLayoutConstraint(item: self.collectionView, attribute: .TrailingMargin, relatedBy: .Equal, toItem: self.view, attribute: .TrailingMargin, multiplier: 1.0, constant: 0)


    self.view.addConstraints([collectionViewBottomConstraint, collectionViewTopConstraint, collectionViewRightConstraint, collectionViewLeftConstraint])


}

2 个答案:

答案 0 :(得分:4)

您需要在视图控制器上实现UICollectionViewDelegateFlowLayout协议。

viewDidLoad上,注册方向更改通知:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "orientationDidChange:", name: UIDeviceOrientationDidChangeNotification, object: nil)

请勿忘记在关闭viewController时删除观察者:

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

当方向改变时,告诉collectionView更新布局:

func orientationDidChange(notification: NSNotification) {
    collectionView!.collectionViewLayout.invalidateLayout()
}

最后,实现UICollectionViewDelegateFlowLayout协议方法。在此方法上,您可以根据需要通过引用collectionView框架宽度或设备屏幕大小来计算集合视图单元的大小:

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    // Leave 10px margin on each side, plus 10px between columns
    let columns = 2
    let margings = 20 + 10 * (columns - 1)
    let width = (collectionView.frame.size.width - margings) / columns
    let height = width // square cells
    return CGSizeMake(width, height)
}

在我的情况下,我希望单元格始终与纵向屏幕比例成比例。我也有可变数量的列,但可能不是你的情况。

答案 1 :(得分:0)

尝试覆盖updateViewConstraints,检查UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation / UIInterfaceOrientationIsLandscape(UIApplication.sharedApplication().statusBarOrientation的当前方向,并更新constant逻辑中某些约束的NSAutoLayout。< / p>