根据iOS中的位置按x顺序对UIView子视图进行排序
我需要找到UITabBarButton位置,我已经实现了一个方法,用于获取tabbutton的所有值和传递的索引。但是,初始选项卡栏按钮的获取在子视图中是不排序的。下面是我的代码。请帮我点这道菜根据iOS中的位置按x顺序对UIView子视图进行排序,ios,iphone,swift,uitabbar,Ios,Iphone,Swift,Uitabbar,我需要找到UITabBarButton位置,我已经实现了一个方法,用于获取tabbutton的所有值和传递的索引。但是,初始选项卡栏按钮的获取在子视图中是不排序的。下面是我的代码。请帮我点这道菜 func frameForTabAtIndex(index: Int) -> CGRect { guard let tabBarSubviews = tabBarController?.tabBar.subviews else { return CGRect.zero
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>>
func frameForTabAtIndex(索引:Int)->CGRect{
guard let tabBarSubviews=tabBarController?.tabBar.subviews else{
返回CGRect.zero
}
var allItems=[UIView]()
对于TabBarSubView中的tabBarItem{
如果tabBarItem.isKind(of:NSClassFromString(“UITabBarButton”)!){
allItems.append(tabBarItem)
}
}
let item=allItems[索引]
返回item.superview!.convert(item.frame,to:view)
}
- 0 :
- 1 :
- 2 :
- 3 :
- 4 :
您可以按如下方式对数组所有项进行排序:
allItems.sort { $0.frame.origin.x < $1.frame.origin.x }
希望能有帮助
let item = allItems[index]