- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在使用 UICollectionView
创建全屏图片库。当用户旋转设备时,我会在
UICollectionView
执行更新
func viewWillTransition(调整大小:CGSize,协调器:UIViewControllerTransitionCoordinator)
我以模态方式显示此 UIViewController
并有一个 UICollectionView
占据整个屏幕。在 viewDidLoad
中,我将流布局创建为:
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = 0
flowLayout.minimumLineSpacing = 0
photosCollectionView.isPagingEnabled = true
photosCollectionView.setCollectionViewLayout(flowLayout, animated: true)
我的尺寸也是:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return photosCollectionView.frame.size
}
当我旋转我的设备时,永远不会调用 viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
,这会导致 UICollectionViewLayout
不更新。当我旋转设备时,我确实收到消息:
The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.
我已经在线阅读,我可以添加:
self.automaticallyAdjustsScrollViewInsets = false
到 UIViewController
,但这没有任何影响。 UICollectionView
没有内容或部分插入。
我还在函数中调用了 super.viewWillTransition
。任何人都可以帮助我解决可能导致此问题的原因吗?
最佳答案
如果您只关心设备旋转时的布局,请使用:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView.collectionViewLayout.invalidateLayout()
collectionView.layoutIfNeeded()
}
来自苹果文档:
public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator)
This method is called when the view controller's view's size ischanged by its parent (i.e. for the root view controller when itswindow rotates or is resized).If you override this method, you should either call super to propagate the change to children or manually forward the change tochildren.
我猜你可能会在那个 View 的父 View 上调用这个函数而不调用 super
解决方法还包括注册设备轮换:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(deviceOrientationDidChange), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
@objc func deviceOrientationDidChange(_ notification: Notification) {
let orientation = UIDevice.current.orientation
print(orientation)
}
关于ios - 未调用 Swift viewWillTransition,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42170373/
我正在制作自定义键盘。我想根据 View 的宽度和高度生成键(UIButtons)的宽度和高度。 最初加载键盘时,viewDidAppear 会正确确定 View 的高度。 (375 x 216) o
我正在使用 UICollectionView 创建全屏图片库。当用户旋转设备时,我会在 中对 UICollectionView 执行更新 func viewWillTransition(调整大小:CG
我有一个 iOS 应用程序,有 3 个容器 View (A、B 和 C)。顶部容器 View (A) 是静态的,下面有一个容器 View (B),带有一个按钮,可将其“移动”到一侧以显示第三个容器 V
我正在使用具有两种 View 类型的日历 cocoapod(CVCalendar:https://github.com/CVCalendar/CVCalendar);周 View 和月 View 。
我正在学习开发 iOS 应用程序,目前我有 4 个 View Controller 。 在第一个 viewController 中,我使用方法 viewWillTransition 在运行时调整 UI
我使用 viewWillTransition 来更改 View 的框架,如果我的 ViewController 位于最顶层,那么一切都很好,这是旋转时打印的框架 TEST frame: (0.0, 0
我以编程方式在我的 UIInputViewController 的 viewDidLoad() 中创建了一组按钮。当方向从横向变为纵向时,我试图更改这些按钮的大小,反之亦然。 我正在尝试使用 over
我试图仅用一个 View 更改方向,其余 View 都固定为纵向。我在 AppDelegate 中设置了一个方法,如下所示 func application(_ application: UIAppl
我的应用程序是基于选项卡的导航。我在 iOS11 中使用 Xcode9。为了支持横向/纵向兼容性,我在所有 viewController 中使用以下方法。 override func viewWill
我必须检查我的设备在 iOS 8+ 中是否改变了方向。 我的做法是: override func viewWillTransition(to size: CGSize, with coordinato
这是我如何重写我的 View Controller ,但是,如果有另一种更好的方法来做到这一点,那么我的第二个 View Controller 不符合重写请指导我,任何建议都会很好. override
我有一个名为 myView 的 UIView 子类,当设备方向发生变化时,它应该重新配置自身。我在 UIViewController 中使用 viewWillTransition(to size: C
我正在制作一个 iMessage 应用程序。我已经实现了 viewWillTransition(to:with:) 以调整/重新排列我的应用程序中的 View 。当我的应用程序的 presentati
我是一名优秀的程序员,十分优秀!