How to highlight a custom UIButton that has no text or background?
For a possible solution, you can modify the alpha of the button in the didSet
of your button's overridden isHighlighted
.
override var isHighlighted: Bool {
didSet {
alpha = isHighlighted ? 0.7 : 1
}
}
Highlight entire UIButton, not only image at the left side?
Set the image as "background image" and the title as it is , now select the option "Shows Touch On Highlight". Hope this help and will highlight the complete button.
UIButton not showing highlight when touched
To get the white text on highlight just do:
[cancelButton setTitleColor:[UIColor whiteColor]
forState:UIControlStateHighlighted];
To get the background to change you need to either do setBackgroundImage:forState: and use a UIImage with the pattern color, or subclass UIButton and set the appropriate background color in the setHighlight: method.
EDIT: Swift 2.x version
cancelButton.setTitleColor(.whiteColor(), forState: Highlighted)
EDIT: Swift 3.0 version
cancelButton.setTitleColor(.white, for: .highlighted)
ObjectiveC - UIButton remains highlighted/selected and background color and font color changes when highlighted/selected
I was able to achieve the function you are working on and below is how i did it.
I created the design via storyboard and connected all the 9 button's actions methods to a single Selector method, inside the action method with the help sender parameter we can get the selected buttons reference and use it.
- (IBAction)btnPressed:(UIButton*)sender {
/* Below for loop works as a reset for setting the default colour of button and to not select the same one twice*/
for (UIButton* button in buttons) {
[button setSelected:NO];
[button setBackgroundColor:[UIColor whiteColor]];
[button setUserInteractionEnabled:true];
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
}
NSInteger tag = sender.tag; // Here we get the sender tag, which we can use for our needs. Also we can directly use the sender and get the title or whatsoever needed.
/*Now below line works as a toggle for the button where multiple buttons can't be selected at the same time.*/
sender.selected = ! sender.selected;
if(sender.selected)
{
/* Here we set the color for the button and handle the selected function*/
[sender setSelected:YES];
[sender setUserInteractionEnabled:false];
[sender setBackgroundColor:[UIColor magentaColor]];
}
}
You can also add custom layer for the button by using the "sender.Layer" property.
The Whole code is added below,
All the button's action needs to be connected to a single selector method,
- (IBAction)btnPressed:(UIButton*)sender;
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *mainViewOL;
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;
@property (weak, nonatomic) IBOutlet UIButton *btn6;
@property (weak, nonatomic) IBOutlet UIButton *btn7;
@property (weak, nonatomic) IBOutlet UIButton *btn8;
@property (weak, nonatomic) IBOutlet UIButton *btn9;
@end
@implementation ViewController
NSArray* buttons;
- (void)viewDidLoad {
[super viewDidLoad];
buttons = [NSArray arrayWithObjects:_btn1, _btn2, _btn3,_btn4,_btn5,_btn6,_btn7,_btn8,_btn9,nil];
self.mainViewOL.layer.shadowRadius = 5;
self.mainViewOL.layer.shadowColor = [UIColor colorWithRed:211.f/255.f green:211.f/255.f blue:211.f/255.f alpha:1.f].CGColor;
self.mainViewOL.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
self.mainViewOL.layer.shadowOpacity = 0.9f;
self.mainViewOL.layer.masksToBounds = NO;
/* I Have added the 9 button's in an array and used it to reduce the lines of code and for easy understanding as well*/
for (UIButton* button in buttons) {
button.layer.borderColor = [UIColor lightGrayColor].CGColor;
button.layer.borderWidth =1.0f;
}
}
- (IBAction)btnPressed:(UIButton*)sender {
for (UIButton* button in buttons) {
[button setSelected:NO];
[button setBackgroundColor:[UIColor whiteColor]];
[button setUserInteractionEnabled:true];
// [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; //Based on your needs and colour variant you cant add properties to the button for different control states.
[button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
}
NSInteger tag = sender.tag;
sender.selected = ! sender.selected;
if(sender.selected)
{
[sender setSelected:YES];
[sender setUserInteractionEnabled:false];
[sender setBackgroundColor:[UIColor purpleColor]];
sender.backgroundColor = [UIColor magentaColor];
}
}
@end
And the Final Result
Ignore the delay in button selection, it is caused by the video to gif conversion.
Hope This helps.
UIButton title won't highlight when an image / background image is set
Setting the button type to System
instead of Custom
solved the issue. Now they both highlight like they should.
UIButton background color for highlighted/selected state issue
I found the issue, UIButton
was set to System.I simply changed it to Custom, it started working as expected.
Related Topics
In App Purchase in Skscene Not Working
Creating Decoration View as Custom Column in UIcollection View
Issue with Optional Core Data Relationship Using Nspersistentcloudkitcontainer
iOS Coredata Compatible with Both iOS 9 and iOS 10
Adding Data to a Specific UId in Firebase
Avoid Equatable and Hashable Boilerplate, Swift 4.2
Sklabelnode Text with Two Different Fonts and Colour. How Is This Possible
Sandbox Entitlement to Script Itunes via Nsapplescript
Call Function on App Termination in Swift
Protocol Variable Implementing Another Protocol
Pfuser That Created Object Cannot Delete Object Despite Acl Writing Access
How to Observe Torchlevel in Swift
Weird Behaviour in Swiftui+Combine When Class -> Struct
How to Add Skspritenode in a Loop
How to Set Local Notifications Between 8Am and 8Pm Every Day
Possible Bug? I Can Create Generic Instance Ignoring Constraint