I need four things [Swift]
- Use my custom button inverse image
- Do not show text (for example, "back" or "last controller header")
- The width of the system, for example. all the back buttons look like this.
- DRY (don't repeat yourself)
Also, since I do it a lot, and it is so strangely complicated, every time I watch it, I created a category.
in AppDelegate.appDidFinishLaunching .... name it:
UINavigationBar.customBackButtonSetup("iconBackArrow", hideText: true)
Add this extension to your own file, for example UINavigationBar + CustomBackButton:
extension UINavigationBar { // call this once at app start class func customBackButtonSetup(backButtonImageName: String, hideText: Bool) { // Custom back button let insets = UIEdgeInsets(top: 0, left: 0, bottom: -2.5, right: 0) // might have to modify this to perfect vertical alignments let backIndicator = UIImage(named: backButtonImageName)!.imageWithRenderingMode(.AlwaysOriginal).imageWithAlignmentRectInsets(insets) UINavigationBar.appearance().backIndicatorImage = backIndicator; UINavigationBar.appearance().backIndicatorTransitionMaskImage = backIndicator if hideText { let barAppearace = UIBarButtonItem.appearance() barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -100), forBarMetrics:UIBarMetrics.Default) } } }
source share