Ad
Custom UIButton : How To Set Background Color On Selection Change?
I have created a custom UIButton
and I want its background to be red when selected, otherwise white.
So I tried this:
class MyCustomButton: UIButton {
override var isSelected: Bool {
didSet {
backgroundColor = isSelected ? .red : .white
}
}
}
But it doesn't work, I've set a breakpoint but it never gets called. How can I do so? Thanks
Ad
Answer
How about :
override open var isHighlighted: Bool {
didSet {
backgroundColor = isHighlighted ? UIColor.red : UIColor.white
}
}
Note : The button type should be custom. It won't work on button with type system.
Ad
source: stackoverflow.com
Related Questions
- → Function Undefined in Axios promise
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Click to navigate on mobile devices
- → Playing Video - Server is not correctly configured - 12939
- → How to allow api access to android or ios app only(laravel)?
- → Axios array map callback
- → Access the Camera and CameraRoll on Android using React Native?
- → Update React [Native] View on Day Change
- → Shopify iOS SDK - issue converting BuyProductVariant to BuyProduct
- → BigCommerce and shopify API
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → React Native - Differences between Android and IOS
- → What is the difference between React Native and React?
Ad