ios - 根据 iOS 中的位置对 UIView subview 进行 x 顺序排序

标签 ios iphone swift uitabbar

我需要找到 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/

相关文章:

ios - 将文件从 bundle 复制到 Libraries 文件夹(IOS 10、Swift 3、xcode 8.2)

ios - 不为特定设备下载点播资源

swift - 什么是单| swift 意味着

objective-c - 在我的 Swift 应用程序中使用来 self 的 XPC 服务的 Objective-C 协议(protocol)

ios - iOS开发中的社交网络集成哪个更好?

ios - 在苹果开发者账户(企业账户)中创建应用标识符时出错

iphone - 在iPhone上播放内置声音

ios - NSManagedObjectContext - FetchRequest 死锁

ios - 如何以编程方式插入右键图像 UINavigationbar swift 3

ios - 核心数据后台插入大数据