iPhone UIButton with UISwitch functionality
UIButton already supports a “switch” functionality. Just set a different image in Interface Builder for the “Selected State Configuration”, and use the selected property of UIButton to toggle its state.
UIButton already supports a “switch” functionality. Just set a different image in Interface Builder for the “Selected State Configuration”, and use the selected property of UIButton to toggle its state.
If you are just supporting iOS 7 you can use tintColor and UIImageRenderingModeAlwaysTemplate This article covers that: https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming If you need to support an earlier version you may want to consider this thread How would I tint an image programmatically on the iPhone?
button.backgroundColor = UIColor.blue Or any other color: red, green, yellow ,etc. Another option is RGBA color: button.backgroundColor = UIColor(red: 0.4, green: 1.0, blue: 0.2, alpha: 0.5)
You can do it using SymbolConfiguration like in the sample code below: let largeConfig = UIImage.SymbolConfiguration(pointSize: 140, weight: .bold, scale: .large) let largeBoldDoc = UIImage(systemName: “doc.circle.fill”, withConfiguration: largeConfig) button.setImage(largeBoldDoc, for: .normal)
I just implemented this. It work’s like a charm! And it wasn’t even hard. typedef void (^ActionBlock)(); @interface UIBlockButton : UIButton { ActionBlock _actionBlock; } -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action; @end @implementation UIBlockButton -(void) handleControlEvent:(UIControlEvents)event withBlock:(ActionBlock) action { _actionBlock = action; [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event]; } -(void) callActionBlock:(id)sender{ _actionBlock(); } @end
It looks to me like you forgot in your code to use the “style” object that you set up.. you just instantiated it. You should modify your code to look like this: NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; [style setAlignment:NSTextAlignmentCenter]; [style setLineBreakMode:NSLineBreakByWordWrapping]; UIFont *font1 = [UIFont fontWithName:@”HelveticaNeue-Medium” size:20.0f]; UIFont *font2 = [UIFont fontWithName:@”HelveticaNeue-Light” size:20.0f]; NSDictionary … Read more
Still valid for: iOS 12/Xcode 10/Swift 4.2/OSX 10.13.2 iOS 9.1/Xcode 7.1/Swift 2.1/OSX 10.10.5: The method titleEdgeInsets didn’t work for me. My button’s frame tightly hugs the text (I didn’t specify a frame size), and the frame has a red background color. After doing: myButton.titleEdgeInsets = UIEdgeInsetsMake(10,10,10,10) the red background shrunk inwards by 10 pixels on … Read more
The values are in the 0.0 to 1.0 range. E.g. divide by 255., but remember the decimal dot so you get floating point division and not integer division. Like selectedColor = [UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1];
With iOS 14 Apple has finally added this feature to UIKit. However, someone might still want to use this extension because Apple’s method signature is suboptimal. iOS 14: extension UIControl { func addAction(for controlEvents: UIControl.Event = .touchUpInside, _ closure: @escaping()->()) { addAction(UIAction { (action: UIAction) in closure() }, for: controlEvents) } } pre-iOS 14: extension … Read more
Have you tried setImage instead of setBackgroundImage?