简体   繁体   English

如何检测 UITabBarController 中的选项卡数量何时随方向发生变化?

[英]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.

相关问题 如何在 iOS 上检测设备的方向? - How Do I detect the orientation of the device on iOS? Swift - 如何检测方向变化 - Swift - How to detect orientation changes 如何检测用户何时更改JTCalendar中的月份或星期? - How do I detect when a user changes the month or week in JTCalendar? 如何添加约束,以便在方向改变时视图的尺寸不会改变? - How do I add constraints so that my view's dimensions do not change when the orientation changes? 如何在UITabBarController中禁用选项卡? - How to disable tabs in UITabBarController? 使用自定义过渡时,如何处理呈现的UIViewController中的方向变化 - How do I handle orientation changes in the presenting UIViewController when using a custom transition 当用户更改方向时,如何从 0 开始重新计算 yOffSet,用于 SwiftUI 中滚动视图中的项目 - How do I recalculate the yOffSet starting from 0, for items in a scrollview in SwiftUI when the user changes orientation 如何更正此语法以检测ipad浏览器方向 - How do I correct this syntax to detect ipad browser orientation 如何在 iOS 上使用 Phonegap 正确检测方向变化? - How do I correctly detect orientation change using Phonegap on iOS? 如何处理UITabBarController子类中的方向更改 - How can I handle orientation change in subclass of UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM