单击更改多个UIButton的背景颜色

时间:2016-07-19 07:38:49

标签: ios objective-c iphone swift uibutton

UIButton中有16 UIViewController,而且一次只需要selected

我创建了一个UIButton集合,并通过执行backgroundColor来更改for loop。它工作正常。

但是我需要一两行的快速解决方案,或者我可以说一次点击改变颜色的有效方法。

我认为我的方式不高效,因为如果UIViewController有50 UIButton该怎么办。

P.S。不要告诉我采取UICollectionView,因为我可以很容易地做到这一点!

1 个答案:

答案 0 :(得分:1)

无论如何,你需要枚举你的按钮。但你可以在扩展中执行此操作一次:

extension Array where Element: UIButton {
    func setBackgroundColor(color: UIColor) {
        for element in self {
            element.backgroundColor = color
        }
    }
}