Simple and customizable button in Swift.
pod 'SwiftyButton'
let button = SwiftyButton()
button.buttonColor = .cyanColor()
button.highlightedColor = .blueColor()
button.shadowHeight = 0
button.cornerRadius = 5
let button = SwiftyButton()
button.buttonColor = .cyanColor()
button.shadowColor = .blueColor()
button.shadowHeight = 5
button.cornerRadius = 5
Here is a list of all the properties of SwiftyButton that you can modify. Those are all editable directly from Interface Builder. See SwiftyButtonDefaults
to set defaults for those properties.
button.buttonColor = UIColor.cyanColor()
button.highlightedColor = UIColor.cyanColor()
button.shadowColor = UIColor.blueColor()
button.disabledButtonColor = UIColor.grayColor()
button.disabledShadowColor = UIColor.darkGrayColor()
button.shadowHeight = 10
button.cornerRadius = 8
button.buttonPressDepth = 0.5 // In percentage of shadowHeight
Add a UIButton
as usual, modify the underlying class to SwiftyButton
, and make sure that the button type is set to Custom
:
You can set defaults that will be applied for any new instance of SwiftyButton by modifying the SwiftyButtonDefaults
structure:
SwiftyButtonDefaults.buttonColor = UIColor.cyanColor()
SwiftyButtonDefaults.cornerRadius = 8
...
Use SwiftyCustomContentButton
to add custom content in a Swifty Button.
This is a subclass of SwiftyButton
that exposes a content view that moves when the button state changes. All you have to do is add your views inside button.customContentView
and setup layout constraints relative to this view.
pod `SwiftyButton/CustomContent`
Here is how you would create a button similar to the one above (here we used PureLayout for constraints):
let button = SwiftyCustomContentButton()
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .White)
button.customContentView.addSubview(indicator)
indicator.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 0), excludingEdge: .Right)
indicator.startAnimating()
let label = UILabel()
button.customContentView.addSubview(label)
label.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 10), excludingEdge: .Left)
label.autoPinEdge(.Left, toEdge: .Right, ofView: indicator, withOffset: 10)
label.text = "Loading..."
label.textColor = UIColor.whiteColor()
Look at the Examples folder to see more button examples.
This project is copyrighted under the MIT license. Complete license can be found here: https://github.com/TakeScoop/SwiftyButton/blob/master/LICENSE
- Inspired by HTPressableButton: https://github.com/herinkc/HTPressableButton
- Colors used in examples come from https://flatuicolors.com/