简体   繁体   中英

Change Background Color of multiple UIButton in Single Click

I have 16 UIButton in a UIViewController and only one need to be selected at a time.

I have created a UIButton collection and changing backgroundColor by performing the for loop . It's working fine.

But I need a quick solution in one / two line or I can say the efficient way to change color in a single click.

I don't think my way is efficient because what if the UIViewController has 50 UIButton .

PS Don't tell me to take UICollectionView as I can easily do with that!

You will anyway need enumerate your buttons. But you can do this once in extension:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM