SoftButton
About
This pod allows you to add neumorphic design to your UIButton in Swift. It supports both dark and light mode, respond to taps, and also offers customization options for the button's corner radius and bevel. Usage details are outlined below and an example project is included in the repo.
Requirements
- iOS 13.0+
- Swift 5.0+
- UIKit framework
Installation
SoftButton is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SoftButton'
Usage
import SoftButton
at the top of your Swift file.
1. Storyboard Setup (If using storyboards)
- Edit outlet to conform to SoftButton
@IBOutlet weak var btn:
SoftButton!
- Also ensure to provide SoftButton as the Custom Class in the storyboard file
Otherwise: let btn = SoftButton()
2. Setup the Button
Then, simply call in viewDidAppear():
btn.makeNeuromorphic(superView: self.view)
Passing the view is important since the background cannot be pure black/white for the effect to work. The pod will setup the view's background for you.
Add Dark Mode Support
Add this block to dynamically change the button style based on light/dark mode. The pod will handle the rest.
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
btn.makeNeuromorphic(superView: self.view) // Add this line
}
Customization
You can also optionally specify the button's corner radius, setup the view individually, and provide the bevel amount
btn.bevel = 5 // Changes button's 'pop-out' amount
btn.makeNeuromorphic(cornerRadius: 25)
// Option 1:
btn.makeNeuromorphic(superView: self.view)
// Option 2:
btn.makeNeuromorphic()
self.view.backgroundColor = btnText.backgroundColor // Set view background manually
Important Notes
makeNeuromorphic()
has to be called fromviewDidAppear()
if any constraints are being used sinceviewDidLoad()
does not have the button's bounds available to it. If the button has a fixed width and height however, then you may setup the button inviewDidLoad()
.- Kindly ignore the misspelling of 'Neumorphic' in the function name. Thank you :)
Example
To run the example project, clone the repo and test on Xcode, or view the ViewController.swift file.
Author
Pallav Agarwal, [email protected]
License
SoftButton is available under the MIT license. See the LICENSE file for more info.