如何获取 UITABBAR 中选定选项卡的索引

How to get index of selected tab in UITABBAR

如何在 swift 中获取 UITabBar 项目的选定索引 我不是在谈论 UITabBarController.

我知道我可以使用 UITabBarItem.tag。

但对我来说,我不能为选定的索引使用标签,因为它已经用于其他目的。 有没有其他方法我可以在 swift.

中获取所选选项卡的索引

https://developer.apple.com/documentation/uikit/uitabbar

UITabBar 有 2 个属性 "items" 和 "selectedItem",都是 UITabBarItem 类型。

所以你可以通过在"items"中获取"selectedItem"的索引来获取所选项目的索引。

items.index(of: selectedItem)