When I push a UIViewController, it has some title in back button at new UIViewController, if the title has a lot of text, It does not look good in iPhone 4s So I want to remove it.
If I add some code in prepareForSegue function, it is going to be a trouble.
Any better way to achieve this?
If you want back arrow so following code put into AppDelegate file into didFinishLaunchingWithOptions method.
For Objective-C
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
For Swift
let BarButtonItemAppearance = UIBarButtonItem.appearance()
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
Another option give below.
In Objective C
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil];
In Swift
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
UPDATE :
let BarButtonItemAppearance = UIBarButtonItem.appearance()
let attributes: [NSAttributedStringKey: Any] = [
BarButtonItemAppearance.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: .normal)
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 0.1),
NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
UPDATE SWIFT 4.1 :
let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 0.1)!, NSAttributedStringKey.foregroundColor: UIColor.clear]
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
BarButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
Using Offset
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000, 0), for:UIBarMetrics.default)
Work's like charm on Swift 3
self.navigationController?.navigationBar.topItem?.title = " "
I'm using this line of code in AppDelegate file into didFinishLaunchingWithOptions method to remove the backbutton title.
Swift 2.x
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default)
Swift 3.x
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
Swift 4.x
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.clear], for: UIControlState.highlighted)
Just need go to your Parent ViewController from where your other ViewControllers are dependent.
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)}
Just copy this code in didFinishLaunchingWithOptions launchOptions
Swift 5
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -1000.0, vertical: 0.0), for: .default)
Swift 4
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffsetMake(-1000.0, 0.0), for: .default)
it is simple. put a space in the title of the back button and ready.
Remember that it has to be in the previous view where you want to remove the text.
On iOS 14 is now present the backButtonDisplayMode property in UINavigationItem class. So, to remove back button title you can use
navigationItem.backButtonDisplayMode = .minimal
in the viewDidLoad func of the viewController where you want remove it.
To remove it in all navigationBar I used the swizzling technique
import UIKit
private let swizzling: (UIViewController.Type, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
if let originalMethod = class_getInstanceMethod(forClass, originalSelector), let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) {
let didAddMethod = class_addMethod(forClass, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))
if didAddMethod {
class_replaceMethod(forClass, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
} else {
method_exchangeImplementations(originalMethod, swizzledMethod)
}
}
}
extension UIViewController {
static func swizzle() {
let originalSelector1 = #selector(viewDidLoad)
let swizzledSelector1 = #selector(swizzled_viewDidLoad)
swizzling(UIViewController.self, originalSelector1, swizzledSelector1)
}
#objc open func swizzled_viewDidLoad() {
if let _ = navigationController {
if #available(iOS 14.0, *) {
navigationItem.backButtonDisplayMode = .minimal
} else {
// Fallback on earlier versions
navigationItem.backButtonTitle = ""
}
}
swizzled_viewDidLoad()
}
}
And in application(_:didFinishLaunchingWithOptions:) call
UIViewController.swizzle()
You could create a subclass for all UIViewControllers you want this behavior for, and in the subclass's viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.backBarButtonItem = UIBarButtonItem(
title: "", style: .plain, target: nil, action: nil)
}
This way, you can choose which controllers you want the behavior for, without duplicating code. I prefer my controllers to just say "Back", rather than the title of the previous controller, so I set that title here.
You can use xcode 8 and swift 3.0
self.navigationController?.navigationBar.backItem?.title = " "
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), for:UIBarMetrics.default)
used this line of code in swift 3.0
Taking inspiration from rordulu's answer here, I ended up creating a custom UINavigationController and UINavigation bar which seems to handle all cases of this tricky problem.
1) Initialise new UINavigationController with your custom UINavigationBar:
class CustomNavigationController: UINavigationController {
convenience init() {
self.init(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
}
}
2) Set the backItem.title property of the navigation bar to an empty string, every time the view lays itself out
class CustomNavigationBar: UINavigationBar {
override func layoutSubviews() {
backItem?.title = ""
super.layoutSubviews()
}
}
Now every time you use this navigation controller and bar combination, it will never have back button text! 🎉
Note: this should work fine if using storyboards also, just ensure to drop the custom navigation bar component into the view
Simple Solution :
While you are pushing 2nd controller from 1st controller, put self.navigationItem.title = "" in viewWillDisappear of 1st controller. It hides back button title from 2nd controller.
Above statment hides 1st controllers title, hence when we came back we want title for 1st controller again. For that we have add title for 1st controller in viewWillAppear method of 1st controller.
Refer following methods (of 1st controller)
override func viewWillDisappear(_ animated: Bool) {
self.navigationItem.title = ""
}
override func viewWillAppear(_ animated: Bool) {
self.navigationItem.title = "Title"
}
Works on Swift 5:
self.navigationItem.backBarButtonItem?.title = ""
Please note it will be effective for the next pushed view controller not the current one on the display, that's why it's very confusing!
Also, check the storyboard and select the navigation item of the previous view controller then type something in the Back Button (Inspector).
A method for iOS13.
let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.backButtonAppearance = backButtonAppearance
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
I usually add or change the back button in viewDidLoad of the UIViewController.
Something like that should work:
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: "closeView:")
self.navigationItem.leftBarButtonItem = leftButton
Don't forget to change and implement the function that it's called to close the view.
Even easier, just change the title:
self.navigationItem.leftBarButtonItem.title = "Back"
if #available(iOS 14.0, *) {
navigationItem.backButtonDisplayMode = .minimal
} else {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
Swift 3:
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
Adding a second answer here as my first only partially works. This method is less elegant in the fact that it requires calling a method in each view in the application, however it works without any side-effects.
So firstly, create a UIViewController extension class with a function to remove back button text and add a custom back button:
extension UIViewController {
func setBackButton() {
navigationController?.navigationBar.backIndicatorImage = R.image.backArrow()
navigationController?.navigationBar.backIndicatorTransitionMaskImage = R.image.backArrow()
navigationItem.backBarButtonItem = UIBarButtonItem(title: " ", style: .plain, target: nil, action: nil)
}
Secondly, we can simply call out to this function in the viewDidLoad of each view controller you need it in.
Swift 4.2
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .normal)
Updated Answer For Swift 4.2
Working with UIAppearance is a cleaner way of solving the problem but it would cause all the UIBarButtonItem to have a clear text. An improved version of the solution could be to check if the UIBarButtonItem is contained in a UINavigationBar.
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
Just create extension of UIViewController with override function awakeFromNib() and make UIBarButtonItem with an empty title and give to navigation backBarButtonItem.
extension UIViewController {
open override func awakeFromNib() {
let backBarBtnItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
navigationItem.backBarButtonItem = backBarBtnItem
}
}
if you want to remove back button title when you open next screen
do this inside the function initialising and pushing a new screen:
navigationItem.backButtonTitle = ""
Full usage:
let view = SomeView()
let controller = UIHostingController(rootView: view)
navigationItem.backButtonTitle = ""
navigationController?.pushViewController(controller, animated: true)
But to customise back buttons for all navigation bars in your app you need to do this:
func setupNavBarAppearance() {
let backButtonImage = Images.west.image.withAlignmentRectInsets(UIEdgeInsets(top: -5, left: -15, bottom: -5, right: -15))
let backButtonAppearance = UIBarButtonItemAppearance(style: .plain)
backButtonAppearance.normal.titleTextAttributes = [.foregroundColor: UIColor.clear]
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
appearance.backButtonAppearance = backButtonAppearance
appearance.setBackIndicatorImage(backButtonImage, transitionMaskImage: backButtonImage)
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().isTranslucent = false
}
You can call it from your AppDelegate.swift
Swift 4.2 & 5
Instead of playing with the navigation bar tint color which will have side effects if you are using image picker anytime later in your code.
Use below code:
extension UIViewController {
open override func awakeFromNib() {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
}
Call it from your first ViewController:
self.awakeFromNib()
Put the below code in any of the UIViewcontroller extension it will hide all the UIViewcontroller back text
open override func awakeFromNib() {
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .plain, target: nil, action: nil)
}
I have a simple solution for those, who don't want to use method swizzling or duplicating a similar code in different view controllers.
To remove back button title, create a UINavigationController subclass and override pushViewController(_, animated:) method:
final class CustomNavigationController: UINavigationController {
override func pushViewController(_ viewController: UIViewController, animated: Bool) {
super.pushViewController(viewController, animated: animated)
let backBarButtonItem = UIBarButtonItem()
backBarButtonItem.title = nil
viewController.navigationItem.backBarButtonItem = backBarButtonItem
}
}
I don't know why but I found problem with hiding back button title in iPhone pluses but in device without plus shows correct with
leftBarButtonItem.title = ""
So I found simple way. It is set tint color to clear in NavigationBar of NavigationViewController in autolayout. It may be problem if you use icons or text tiles with tint. But in my case I don't use it as all.
Just use this:
func removeBackButton(vc:UIViewController) {
let button = UIButton.init(type: .custom)
button.setImage(UIImage.init(named:""), for: .normal)
let leftBarButton = UIBarButtonItem.init(customView: button)
vc.navigationItem.leftBarButtonItem = leftBarButton
}
So call this method in viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
removeBackButton(vc:self)
}
You can add this extension to UIViewController
And then call this function in every viewDidLoad()
like : self.updateBackButton()
extension UIViewController {
func updateBackButton(){
if self.navigationController != nil {
self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .done, target: self, action: nil)
}
}}
I would like to share a solution that works for me. Also, it can be adjusted base on your needs and requirements.
Note, in my case, I use a storyboard to specify CustomNavigationBar
Swift 4.2
class CustomNavigationBar: UINavigationBar {
override func awakeFromNib() {
super.awakeFromNib()
guard let topItem = topItem else { return }
removeBackButtonTitle(for: topItem)
}
override func pushItem(_ item: UINavigationItem, animated: Bool) {
removeBackButtonTitle(for: item)
super.pushItem(item, animated: animated)
}
func removeBackButtonTitle(for item: UINavigationItem) {
item.backBarButtonItem = UIBarButtonItem()
}
}
Works for Swift 4.2
Using the line of code in AppDelegate file into didFinishLaunchingWithOptions
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .highlighted)
Related
Our application has a deployment target of iOS 10. We're hiding all back button titles (for the entire app) with the following code
let attributes: [NSAttributedString.Key : Any] = [
.font : UIFont.systemFont(ofSize: 0.001),
.foregroundColor: UIColor.clear
]
let barButtonItemAppearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self])
barButtonItemAppearance.setTitleTextAttributes(attributes, for: .normal)
barButtonItemAppearance.setTitleTextAttributes(attributes, for: .highlighted)
This worked up until iOS 13, where the back button titles are no longer hidden.
I saw the WWDC 2019 video about the new apperance APIs, but as far as I can tell it only told me how to use the new APIs for a single navigation bar (and not for the entire app).
How can I achieve this?
Add in viewDidLoad() method of the parent(previous in the stack) controller.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
In the next screen, only the arrow image will be presented.
or you can put this code before pushViewController method, for example
func presentNextScreen(_ controller: UIViewController){
// Suppress title in the back button on the next screen.
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
self.navigationController?.pushViewController(controller, animated: true)
}
For me, the easiest way to hide the title in the back button is to configure the appearance of UINavigationBar
if #available(iOS 13.0, *) {
let standartAppearence = UINavigationBarAppearance()
standartAppearence.configureWithDefaultBackground()
let backButtonAppearence = UIBarButtonItemAppearance()
let titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.clear]
backButtonAppearence.normal.titleTextAttributes = titleTextAttributes
backButtonAppearence.highlighted.titleTextAttributes = titleTextAttributes
standartAppearence.backButtonAppearance = backButtonAppearence
UINavigationBar.appearance().standardAppearance = standartAppearence
UINavigationBar.appearance().compactAppearance = standartAppearence
UINavigationBar.appearance().scrollEdgeAppearance = standartAppearence
}
In addition, here you can add any other configuration of the navigation bar
A workaround:
Create a base class: BaseViewController.
class BaseViewController: UIViewController {
func hideBackButton() {
self.navigationItem.leftBarButtonItem = nil
}
func hideCloseButton() {
self.navigationItem.rightBarButtonItem = nil
}
override public func viewDidLoad() {
super.viewDidLoad()
addNavigationButtons()
}
func addNavigationButtons() {
let backButton = UIBarButtonItem(image: UIImage(named: "back"), style: .plain, target: self, action: #selector(goBack))
self.navigationItem.leftBarButtonItem = backButton
let closeButton = UIBarButtonItem(image: UIImage(named: "close"), style: .plain, target: self, action: #selector(closeTheFlow))
self.navigationItem.rightBarButtonItem = closeButton
}
#objc func goBack() {
//Go Back
}
#objc func closeTheFlow() {
//Close
}
Make every ViewController a subclass of BaseViewController.
You could do anything with the right and left buttons in the entire app.
I hide title gloabal. a trick
UIBarButtonItem.appearance().setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -UIScreen.main.bounds.size.width, vertical: 0), for: UIBarMetrics.default)
rootViewController -> tabviewcontroller -> viewController1 -> viewController2
In such order, back button works fine but when viewController2 pops back to viewController1, viewController1's backbutton is not shown, so I cannot go back to tabviewcontroller.
What is the possible reason? Thanks for help.
Here is my storyboard
//after viewWillAppear
func initNavigation(){
//topItem is self.navigationController?.navigationBar.topItem
guard topItem != nil else {
return
}
//custom letButtonBar just make tabviewcontroller title at left
//other viewcontroller title is "" so position still at center
let leftButton = UIBarButtonItem(title: "", style:UIBarButtonItem.Style.plain, target: self, action: nil)
let attr = [NSAttributedString.Key.foregroundColor:UIColor.white, NSAttributedString.Key.font: UIFont(name: "iconfont", size: UIFont.buttonFontSize)!]
leftButton.setTitleTextAttributes(attr, for: .normal)
leftButton.setTitleTextAttributes(attr, for: .highlighted)
leftButton.setTitleTextAttributes(attr, for: .disabled)
leftButton.tintColor = UIColor.white
leftButton.isEnabled = false
topItem?.backBarButtonItem?.tintColor = UIColor.white
topItem?.setLeftBarButton(leftButton, animated: true)
topItem?.title = ""
}
//tabviewcontroller:
override func initNavigation() {
super.initNavigation()
topItem?.leftBarButtonItem?.title = "tabviewcontrollerTitleAtLeft"
}
//viewcontroller1
override func initNavigation() {
super.initNavigation()
self.navigationItem.title = "viewController1TitleAtCenter"
}
//viewcontroller2
override func initNavigation() {
super.initNavigation()
self.navigationItem.title = "viewController2TitleAtCenter"
}
I tried many times, found a solution, I delete super.initNavigation() in viewcontroller1, it works fine finally, still do not know the reason.
It seems maybe you can have another UINavigationController between VCs like the following:
Hide the navigation bar of the first one. and use the second navigation bar as the root.
You should check how you are presenting and popping VCs. There is a possibility that it's caused by something relating to that.
I've watched a lot of questions like this and didn't find an answer for my question.
That how i do now:
APPDELEGATE (didFinishLaunchingWithOptions)
// Text
let barButtonItem = UIBarButtonItem.appearance()
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: UIControlState.normal)
barButtonItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.clear], for: UIControlState.highlighted)
// Image
let backImage = UIImage(named: "arrow_left"
UINavigationBar.appearance().backIndicatorImage = backImage
UINavigationBar.appearance().backIndicatorTransitionMaskImage = backImage
And this almost fit to what i need, but the screen title shifted to the right as there is an invisible back button text. And it definetly is (the root controller's title has 9 characters length):
The question is: How to change image, hide text and keep standart back action for every appearance of back button in ios 9.0 ?
There are three ways to do what you want.
I recommend: Create your own Navigation Class by extending and UINavigationController and override backbuttonItem (UIBarButtonItem) property to customise it according to your requirement. And use the same Navigation Controller class in your project.
Create a custom backBarButton by extending UIBarButtonItem and manually set the same as a back button of default Navigation Controller class, in all view controller.
Hide default navigation bar from root controller and create your own navigation bar using UIView and UIButton in all view controllers. (I always use this choice, that makes customization of navigation bar very easy for me. And I can set my view according to my requirement)
Here is how you can add Custom button for your navigation bar
let btnleft : UIButton = UIButton(frame: CGRect(x:0, y:0, width:35, height:35))
btnleft.contentMode = .center
btnleft.setImage(Set_Local_Image("arrow_left"), for: .normal)
btnleft.addTarget(self, action: #selector(YOUR_ACTION), for: .touchDown)
let backBarButon: UIBarButtonItem = UIBarButtonItem(customView: btnleft)
self.navigationItem.setLeftBarButtonItems([menuBarButon], animated:false)
instead of "arrow_left" You can use any image you want
For Default back action you can create function(YOUR_ACTION) and use in selector of back button
navController.popViewController(animated: true)
I can suggest you 2 options. Both require BaseViewController class as a superclass of all your view controllers.
If you are ok with native back button image, just want to remove back button text you can use this subclass:
class BaseViewController: UIViewController {
var navigationTitle: String = ""
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if !navigationTitle.isEmpty {
navigationItem.title = navigationTitle
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationTitle = navigationItem.title ?? ""
navigationItem.title = ""
}
}
If you want to use your custom icon for back button, you should create UIBarButtonItem with your image, add target, selector, handle action of the button. Sample BaseViewController class below:
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let backImage = UIImage(named: "arrow_left")
navigationItem.hidesBackButton = true
guard let navigationController = navigationController else {
return
}
if navigationController.viewControllers.count > 1 {
// we have to set back button only when we have at least 1 controller to go back
navigationItem.leftBarButtonItem = UIBarButtonItem(image: backImage, style: .plain, target: self, action: #selector(backBarButtonAction(sender:)))
}
}
// MARK: Actions
func backBarButtonAction(sender: UIBarButtonItem) {
navigationController?.popViewController(animated: true)
}
}
According to https://stackoverflow.com/a/16831482/5790492 there is a way to do this without appearance.
Swift 3.0
extension UIViewController {
func setupCustomBackButton() {
if let controllersCount = navigationController?.viewControllers.count, controllersCount > 1 {
let backButton = UIButton(frame: CGRect(x: 0, y: 0, width: 12, height: 20))
backButton.setBackgroundImage(UIImage(named: "arrow_left"), for: .normal)
backButton.contentMode = .left
let backButtonItem = UIBarButtonItem(customView: backButton)
backButton.addTarget(self, action: #selector(self.popCurrentViewController), for: .touchUpInside)
navigationItem.leftBarButtonItem = backButtonItem
navigationItem.hidesBackButton = true
}
}
func popCurrentViewController() {
navigationController?.popViewController(animated: true)
}
}
UINavigationBar.appearance().setBackgroundImage(UIImage(), for:
UIBarPosition.any, barMetrics: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor.main
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.main
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName :
(UIFont(name: "Helvetica", size: 18))!, NSForegroundColorAttributeName:
UIColor.white]
Try this code and make changes accordingly to set image, color and other properties
You should watch Mark Moeykens' youtube series on this. He is IMHO one of the best YouTube presenters for UI Design and implementation in Swift.
The play list is https://www.youtube.com/playlist?list=PLHDMmeIMXj8WyvlX5uFmppVn2Pm0bXVr7
Create an extension for UINavigationItem
extension UINavigationItem {
func backBarButtonItem() -> UINavigationItem {
return UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
}
}
I am trying to add Home button to navigation controller. Therefore I created below class and sub classed my navigation controller. My button appears on my first view. When I navigate to other view(table view in my picture), added button disappears. I am using segues to push to another view.
class ThemedNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
var home = UIBarButtonItem(image: UIImage(named: "home"), style: UIBarButtonItemStyle.Plain, target: self, action: "doneAction")
navigationBar.topItem?.rightBarButtonItem = home
navigationBar.barTintColor = anaRenk
navigationBar.barStyle = .Black
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!]
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!],
forState: .Normal)
}
func doneAction() { // [6]
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
Before my mainViewController didn't have navigation controller. Instead each button was pushing new viewcontrollers which had separate navigation controllers and my code was working. I will appreciate if you can tell me how can I fix this issue.
You have to add the buttons in each controller as the nav bar updates for each controller when pushed on. hence why the back button label changes.
So put the same code in each controller you want a home button for.
I would not put any of the code in the Nav controller itself. Start with the first controller you want to pop home from.
Easier to drag a bar button item in using storyboard, then create an action for it which pops to root.
I think I made it. It works however I am not sure if it is the best method. I will appreciate if you comment for any possible memory leak or crash issue. Thanks.
class ThemedNavigationController: UINavigationController {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
var home:UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
home = UIBarButtonItem(image: UIImage(named: "home"), style: UIBarButtonItemStyle.Plain, target: self, action: "doneAction")
navigationBar.barTintColor = anaRenk
navigationBar.barStyle = .Black
navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!]
UIBarButtonItem.appearance().setTitleTextAttributes(
[NSFontAttributeName: UIFont(name: sansSerifName, size: 17)!],
forState: .Normal)
}
override func pushViewController(viewController: UIViewController, animated: Bool) {
var exbutton = viewController.navigationItem.rightBarButtonItem?
if exbutton == nil {
viewController.navigationItem.rightBarButtonItem = home
}
else {
viewController.navigationItem.rightBarButtonItems?.insert(home, atIndex: 0)
}
super.pushViewController(viewController, animated:animated)
}
func doneAction() { // [6]
popToRootViewControllerAnimated(true)
}
}
Code below is not working for me, can anyone help to figure it out what is wrong?
var image = UIImage(named: "10384605_10152519403846670_5189785375955620548_n.jpg") as UIImage
self.navigationController.navigationBar.setBackgroundImage(image , forBarMetrics:UIBarMetrics)
self.navigationController.navigationBar.setBackgroundImage(image,
forBarMetrics: .Default)
In AppDelegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//Image Background Navigation Bar
let navBackgroundImage:UIImage! = UIImage(named: "backgroundNB.png")
UINavigationBar.appearance().setBackgroundImage(navBackgroundImage, forBarMetrics: .Default)
return true
}
In Swift 3:
If you want to add a repeating image in the background you can make this call in AppDelegate > didFinishLaunchingWithOptions:
let image = UIImage(named: "imageNameInAsset")
UINavigationBar.appearance().setBackgroundImage(image, for: .default)
If you want to add an image to the center of the navigation bar you need to do this in the ViewController > viewWillAppear:
let titleView = UIImageView(image: UIImage(named: "imageNameInAsset"))
self.navigationItem.titleView = titleView
If you want to fill the image in navigation bar just use the code:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named: "your_Background_Image_Name")?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: .stretch), for: .default)
let navBackgroundImage:UIImage! = UIImage(named: "navbar_bg")
[UINavigationBar .appearance().setBackgroundImage(navBackgroundImage, forBarMetrics:.Default)]
For Swift 3:
In AppDelegate.swift:
UINavigationBar.appearance().setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
OR
In viewDidLoad():
self.navigationController?.navigationBar.setBackgroundImage(UIImage(named:"pattern.png"),
for: .default)
set backgroundImage in Navigation controller
self.navigationController?.navigationBar .setBackgroundImage(UIImage(named: "cricket"), for: .default)
if You put the navigation bar hidden for the application you have to show it on view did load or appear by :
override func viewWillAppear(_ animated: Bool) {
print("\n Debugger : View will appear called")
self.navigationController?.isNavigationBarHidden = false
}
if you want your navigation bar only show in a specific view controller you have to disappear the navigation bar by :
override func viewDidDisappear(_ animated: Bool) {
print("\n Debugger : View did disapper called")
self.navigationController?.isNavigationBarHidden = true
}
Navigation bar setup Method
private func navigationBarSetup(){
print("\n Debugger : Navigation Bar setup method called")
self.navigationController!.navigationBar.setBackgroundImage(UIImage(named: "your image name "), for: .default)
let backButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
backButton.tintColor = UIColor.white
self.navigationItem.leftBarButtonItem = backButton
let rightButton = UIBarButtonItem(image: UIImage(named: "your Image name"), style: .plain, target: self, action: #selector(Your selector method))
backButton.tintColor = UIColor.white
self.navigationItem.rightBarButtonItem = rightButton
}
Add the following code to didFinishLaunchingWithOptions method in AppDelegate.swift:
if let myImage = UIImage(named: "navBarImage.jpg"){
UINavigationBar.appearance().setBackgroundImage(myImage, for: .default)
}
#benLIVE asked for how to do a back button, which I was doing when I found the accepted answer, so I thought I'd leave this here too (if a bit late) b/c if you are going to replace a nav bar icon, you may as well replace all of them!
let cubeIcon = UIImageView(image: yourImage)
cubeIcon.contentMode = .scaleAspectFit
self.navigationItem.backBarButtonItem?.image = cubeIcon.image