[英]How do I detect when the number of tabs changes on orientation in UITabBarController?
On an iPhone 13 pro max, 5 tabs are showing in portrait and 8 tabs are showing in landscape.在 iPhone 13 pro max 上,纵向显示 5 个标签,横向显示 8 个标签。
I want to detect when there is a change in the number of tabs (ex: 8 to 5 or 5 to 8).我想检测标签数量何时发生变化(例如:8 到 5 或 5 到 8)。
Sometimes the new tabs are getting rendered after deviceOrientationDidChange where tabBar.items?.count
still points to the old value.有时,新选项卡在 deviceOrientationDidChange 之后呈现,其中
tabBar.items?.count
仍然指向旧值。
One solution is to override viewDidLayoutSubviews
.一种解决方案是覆盖
viewDidLayoutSubviews
。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("didLayout:", self.tabBar.items!.count)
}
That code assumes self
is a class that extends UITabBarController
.该代码假定
self
是一个扩展UITabBarController
的 class。
One issue with this solution is that viewDidLayoutSubviews
is likely to be called more than once (probably two times) when the device is rotated so you have to adjust your logic to handle this.此解决方案的一个问题是,当设备旋转时,
viewDidLayoutSubviews
可能会被调用不止一次(可能两次),因此您必须调整逻辑来处理此问题。
Another possible solution is to override viewWillTransition
:另一种可能的解决方案是覆盖
viewWillTransition
:
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
coordinator.animate(alongsideTransition: nil) { context in
print("didTransition:", self.tabBar.items!.count)
}
}
The trailing closure is a completion block that is called after the rotation animation completes.尾随闭包是一个完成块,在旋转 animation 完成后调用。
One issue with this solution is that it is not called initially.此解决方案的一个问题是它最初未被调用。 It is only called after the device is rotated or if the split view mode is changed.
它仅在设备旋转或拆分视图模式更改后调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.