In my iPhone app, I have a UIButton which I have created in Interface Builder. I can successfully enable and disable it like this in my code ...
sendButton.e
#import "UIButton+My.h"
#import <QuartzCore/QuartzCore.h>
@implementation UIButton (My)
-(void)fade :(BOOL)enable{
self.enabled=enable;//
self.alpha=enable?1.0:0.5;
}
@end
.h:
#import <UIKit/UIKit.h>
@interface UIButton (My)
-(void)fade :(BOOL)enable;
@end
In Swift:
previousCustomButton.enabled = false
previousCustomButton.alpha = 0.5
or
nextCustomButton.enabled = true
nextCustomButton.alpha = 1.0
It looks like the following code works fine. But in some cases, this doesn't work.
sendButton.enabled = NO;
sendButton.alpha = 0.5;
If the above code doesn't work, please wrap it in Main Thread. so
dispatch_async(dispatch_get_main_queue(), ^{
sendButton.enabled = NO;
sendButton.alpha = 0.5
});
if you go with swift, like this
DispatchQueue.main.async {
sendButton.isEnabled = false
sendButton.alpha = 0.5
}
In addition, if you customized UIButton, add this to the Button class.
override var isEnabled: Bool {
didSet {
DispatchQueue.main.async {
if self.isEnabled {
self.alpha = 1.0
}
else {
self.alpha = 0.5
}
}
}
}
Thank you and enjoy coding!!!
You can use adjustsImageWhenDisabled
which is property of UIButton
(@property (nonatomic) BOOL adjustsImageWhenDisabled)
Ex:
Button.adjustsImageWhenDisabled = false
just change state config to disable and choose what you want, background Image for disabled state
Try to set the different images for UIControlStateDisabled
(disabled gray image) and UIControlStateNormal
(Normal image) so the button generate the disabled state for you.