IBOutletCollection set ordering in Interface Builder

前端 未结 10 1090
我寻月下人不归
我寻月下人不归 2020-11-30 22:20

I am using IBOutletCollections to group several Instances of similar UI Elements. In particular I group a number of UIButtons (which are similar to buzzers in a quiz game) a

10条回答
  •  长情又很酷
    2020-11-30 23:03

    Here's an extension I created on Array to sort by tag, e.g., useful when working w/ IBOutletCollections.

    extension Array where Element: UIView {
    
      /**
       Sorts an array of `UIView`s or subclasses by `tag`. For example, this is useful when working with `IBOutletCollection`s, whose order of elements can be changed when manipulating the view objects in Interface Builder. Just tag your views in Interface Builder and then call this method on your `IBOutletCollection`s in `viewDidLoad()`.
       - author: Scott Gardner
       - seealso:
       * [Source on GitHub](http://bit.ly/SortUIViewsInPlaceByTag)
       */
      mutating func sortUIViewsInPlaceByTag() {
        sortInPlace { (left: Element, right: Element) in
          left.tag < right.tag
        }
      }
    
    }
    

提交回复
热议问题