我需要找到 UITabBarButton 位置,我已经实现了用于获取 tabbutton 的所有值并传递索引的方法。然而,初始标签栏按钮在 subview 中的获取方式是未排序的。下面是我的代码。请帮我订购
func frameForTabAtIndex(index: Int) -> CGRect {
guard let tabBarSubviews = tabBarController?.tabBar.subviews else {
return CGRect.zero
}
var allItems = [UIView]()
for tabBarItem in tabBarSubviews {
if tabBarItem.isKind(of: NSClassFromString("UITabBarButton")!) {
allItems.append(tabBarItem)
}
}
let item = allItems[index]
return item.superview!.convert(item.frame, to: view)
}
- 0 : <_UIBarBackground: 0x7ff96060ccb0; frame = (0 0; 414 49); userInteractionEnabled = NO; layer = <CALayer: 0x6040000395a0>>
- 1 : <UITabBarButton: 0x7ff96060a4b0; frame = (106 1; 99 48); alpha = 0.25; opaque = NO; layer = <CALayer: 0x604000039da0>>
- 2 : <UITabBarButton: 0x7ff96060bb90; frame = (209 1; 100 48); alpha = 0.25; opaque = NO; layer = <CALayer: 0x604000039bc0>>
- 3 : <UITabBarButton: 0x7ff96060c420; frame = (313 1; 99 48); opaque = NO; layer = <CALayer: 0x604000039ae0>>
- 4 : <UITabBarButton: 0x7ff961b39160; frame = (2 1; 100 48); alpha = 0.25; opaque = NO; layer = <CALayer: 0x60c000229f80>>
最佳答案
您可以按如下方式对数组 allItems 进行排序:
allItems.sort { $0.frame.origin.x < $1.frame.origin.x }
这将为您提供排序后的数组,并且由于它是可变的,因此它将根据 UITabBarButton 的 x 值对对象进行排序
就在代码上方,您可以在其中从数组中获取项目。
let item = allItems[index]
希望对你有帮助
关于ios - 根据 iOS 中的位置对 UIView subview 进行 x 顺序排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299428/