RecessedButton
avoids reimplementing all the time a UIButton
acting like a toggle switch.
RecessedBarButton
provides this button for UIBarButtonItem
in Navigation Bars.
When tapped, it enters selected mode, and has a rounded rect background.
Just import the file TNRecessedButton.swift
in your project.
/* 1. Create the button */
let button = RecessedButton()
/* 2. Customize it like any button */
button.setTitle("Button", for: .normal)
button.setImage(anIcon, for: .normal)
/* 3. Selected state can be customized as well */
button.setTitle("Selected", for: .selected)
button.setImage(aSelectedIcon, for: .selected)
/* 4. If you really need,
you can adjust the corner radius when selected */
button.layer.cornerRadius = 7
button.clipsToBounds = true
/* 5. To use in a Navigation Bar */
navigationItem.rightBarButtonItem = RecessedBarButton(button: button)
Take a look at the illustrations on top of this README.
Notice the background of selected (i) icon is not a square. This is awful.
Whereas the background of Tramigo's time icon is (second example, red background).
Make sure you have a square as well.
To do so for your icon-only buttons, play with their (content, image, title) insets.
Alternatively, you can ajdust the width of your PNG icon file by adding extra transparent pixels.
An Xcode project demonstrating RecessedButton
is included under Example folder.
- Swift 4
- iOS 9 or later
Initial version
- RecessedBarButton background and icon don't resize on iPhone landscape
- Background color could be customized
- Background corner radius as well
Written by Thomas Naudet, feel free to give me your feedback, or even to tell me you're using this π.
Available under the MIT license.
See the LICENSE file for more info.