UINavigationBar won't show again after hiding on scroll/swipe - ios

As the title says, the navigationBar doesn't show up after hiding it on scroll. I tried setting it in xCode as well as programmatically. Either way, the problem is still there.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.hidesBarsOnSwipe = true
}
Here is what it looks like after hiding the navBar. Note that the status bar color also changed color from white to black.
In viewDidLoad I have :
//MARK: navigation bar styles
self.navigationController?.navigationBar.backgroundColor = UIColor.init(red: 26/255, green: 24/255, blue: 24/255, alpha: 1)
self.navigationController?.navigationBar.clipsToBounds = true
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont(name:"GillSans", size: 20)!]
Does anyone have a good solution for this? Thank you in advance for your help!

Try this code:
Note : As you setting your navbar background close to black. You have to change your status bar content to light.
//Update your plist with below code
View controller-based status bar appearance = NO
In your ViewController:
title = "Some Title"
navigationController?.navigationBar.barTintColor = UIColor(red: 26/255, green: 24/255, blue: 24/255, alpha: 1)
navigationController?.navigationBar.tintColor = UIColor.white
//Title Colour and Font
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white, NSFontAttributeName:UIFont(name:"Arial", size: 20)!]
navigationController?.navigationBar.clipsToBounds = false
UIApplication.shared.statusBarStyle = .lightContent
}
override func viewDidAppear(_ animated: Bool) {
navigationController?.hidesBarsOnSwipe = true
}
output:

Related

When I segue another page, the top of the navigation bar turns black SWIFT 5

When I click on the search icon, I segue another page and added the search bar to the navigation bar.But after making a segue, the top of the navigation bar turns black.
Before segue:
After segue:
class SearchViewController: UIViewController, UISearchBarDelegate {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
}
func setupSearch() {
searchController.searchBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
navigationController?.navigationBar.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
navigationItem.searchController = searchController
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
searchController.searchBar.delegate = self
}
}
Try adding edge for extended layout either from code like below
self.navigationController?.navigationBar.isTranslucent = true
self.edgesForExtendedLayout = UIRectEdge.all
Or You can do it from the XIB/Storyboard as well.
storyboard -> navigation controller - > untick the shows navigation bar and run again.
I solved the problem by adding backgroundcolor to my view.
view.backgroundColor = #colorLiteral(red: 0.1333333333, green: 0.1333333333, blue: 0.1333333333, alpha: 1)
Then I added another white view. I don't know if this is the right way, but the problem is solved.

Changing colour properties and images for navigation bar, tableview, collection view in Extension Swift 4.2

I am creating night mode using Swift 4.2(Notification Center
). I followed https://medium.com/#mczachurski/ios-dark-theme-9a12724c112d this tutorial and able change the colour for only labels. check the below list what all things need to change after changing the mode.
Navigation bar
Collection view cell images
Custom cell(.xib) background color
see the below functions
override func enableDarkMode() {
super.enableDarkMode()
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.black
navigationBarAppearace.barTintColor = UIColor.black
}
override func enableDarkMode() {
super.enableDarkMode()
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.black
navigationBarAppearace.barTintColor = UIColor.black
}
Are you using the navigation programmatically?, if not you can try with these
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
self.navigationController?.navigationBar.barTintColor = UIColor.init(red: 40/255, green: 36/255, blue: 96/255, alpha: 1.0);
Hope it will solve your navigation Problem.
For changing the cell color you can refer the link which helped me to solve the issue link
To change the background color of the xib's, refer the code
override func awakeFromNib() {
backgroundView = UIView()
backgroundView?.backgroundColor = UIColor.whiteColor()
}

How to commit changes of navigator bar translucency properly?

I have faced with such problem.
I have two VC. One of it has basic navigator bar, which inherits from main Navigator bar.
In this nav bar I use such settings to customise my nav nar
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationBar.createGradientNavigatorBar()
navigationBar.isTranslucent = false
navigationBar.barTintColor = UIColor.init(red: 0/255, green: 97/255, blue: 185/255, alpha: 1)
navigationBar.shadowImage = UIImage()
}
On the second VC I have to set custom gradient background which connected with view. For this purpose I have extended my view up to bar height and make par transculent. Here is code:
override func viewWillAppear(_ animated: Bool) {
isCurrentViewControllerAppropriate.shared.isCurrentVcAppropriate = isCurrentViewController()
let firstColor = UIColor.init(red: 0/255, green: 94/255, blue: 198/255, alpha: 1)
let secondColor = UIColor.init(red: 27/255, green: 145/255, blue: 226/255, alpha: 1)
profileBackgroundView.createGradient(firstColor: firstColor.cgColor, secondColor: secondColor.cgColor)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.backgroundColor = UIColor.clear
navigationController?.navigationBar.isTranslucent = true
}
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.isTranslucent = false
}
Everything is working fine, but I have rough transition between vc. I've attached.
As you may notice when I return to my first VC is loading it have an artefact with navigator bar color from second VC, which resolves after a certain period of time. I have no idea how to deal with it
I'm not sure whether my approach is the right one, so I'll be glad to hear any advices consider solving this issue.

Navigation bar changes upon returning to previous VC

I'm having a few issues with my navigation bar styling. Let's say I have two view controllers.
VC 1 has an orange bar with white text that I have set up like so:
func stylingStuffs(){
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 1)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 1)
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
self.navigationController?.navigationBar.isTranslucent = false
UIApplication.shared.statusBarStyle = .lightContent
}
VC 2 has a transparent navigation bar and the code for that is:
func stylingStuffs(){
navigationController?.setNavigationBarHidden(false, animated: true)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.barTintColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 0)
self.navigationController?.navigationBar.backgroundColor = UIColor(red: 0.94, green: 0.38, blue: 0.24, alpha: 0)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.barStyle = .default
UIApplication.shared.statusBarStyle = .lightContent
}
When I navigate from VC1 to VC2 everything works fine, when I navigate from VC2 to VC1, VC1's Navbar is now white/transparent as well as the status bar.
How can I fix this or I guess reset the styles upon return to VC1?
first view VC 1 controller viewDidAppear call stylingStuffs()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated: animated)
stylingStuffs()
}
#rmaddy comment
You are confused because there is actually only one navigation bar. It is part of the one navigation controller that both view controllers are in
so need to reset navigation bar style for VC 1

Changing navigation bar color in Swift

I am using a Picker View to allow the user to choose the colour theme for the entire app.
I am planning on changing the colour of the navigation bar, background and possibly the tab bar (if that is possible).
I've been researching how to do this but can't find any Swift examples. Could anyone please give me an example of the code I would need to use to change the navigation bar colour and navigation bar text colour?
The Picker View is set up, I'm just looking for the code to change the UI colours.
Navigation Bar:
navigationController?.navigationBar.barTintColor = UIColor.green
Replace greenColor with whatever UIColor you want, you can use an RGB too if you prefer.
Navigation Bar Text:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
Replace orangeColor with whatever color you like.
Tab Bar:
tabBarController?.tabBar.barTintColor = UIColor.brown
Tab Bar Text:
tabBarController?.tabBar.tintColor = UIColor.yellow
On the last two, replace brownColor and yellowColor with the color of your choice.
Here are some very basic appearance customization that you can apply app wide:
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();
Swift 5.4.2:
UINavigationBar.appearance().backgroundColor = .green // backgorund color with gradient
// or
UINavigationBar.appearance().barTintColor = .green // solid color
UIBarButtonItem.appearance().tintColor = .magenta
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
UITabBar.appearance().barTintColor = .yellow
More about UIAppearance API in Swift you can read here.
Updated for Swift 3, 4, 4.2, 5+
// setup navBar.....
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4.2, 5+
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
if you want to work with large title, add this line:
UINavigationBar.navigationBar.prefersLargeTitles = true
Also can check here : https://github.com/hasnine/iOSUtilitiesSource
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
Just paste this line in didFinishLaunchingWithOptions in your code.
Within AppDelegate, this has globally changed the format of the NavBar and removes the bottom line/border (which is a problem area for most people) to give you what I think you and others are looking for:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = Style.SELECTED_COLOR
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = Style.SELECTED_COLOR
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
Then you can setup a Constants.swift file, and contained is a Style struct with colors and fonts etc. You can then add a tableView/pickerView to any ViewController and use "availableThemes" array to allow user to change themeColor.
The beautiful thing about this is you can use one reference throughout your whole app for each colour and it'll update based on the user's selected "Theme" and without one it defaults to theme1():
import Foundation
import UIKit
struct Style {
static let availableThemes = ["Theme 1","Theme 2","Theme 3"]
static func loadTheme(){
let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey("Theme"){
// Select the Theme
if name == availableThemes[0] { theme1() }
if name == availableThemes[1] { theme2() }
if name == availableThemes[2] { theme3() }
}else{
defaults.setObject(availableThemes[0], forKey: "Theme")
theme1()
}
}
// Colors specific to theme - can include multiple colours here for each one
static func theme1(){
static var SELECTED_COLOR = UIColor(red:70/255, green: 38/255, blue: 92/255, alpha: 1) }
static func theme2(){
static var SELECTED_COLOR = UIColor(red:255/255, green: 255/255, blue: 255/255, alpha: 1) }
static func theme3(){
static var SELECTED_COLOR = UIColor(red:90/255, green: 50/255, blue: 120/255, alpha: 1) } ...
To do this on storyboard (Interface Builder Inspector)
With help of IBDesignable, we can add more options to Interface Builder Inspector for UINavigationController and tweak them on storyboard. First, add the following code to your project.
#IBDesignable extension UINavigationController {
#IBInspectable var barTintColor: UIColor? {
set {
guard let uiColor = newValue else { return }
navigationBar.barTintColor = uiColor
}
get {
guard let color = navigationBar.barTintColor else { return nil }
return color
}
}
}
Then simply set the attributes for navigation controller on storyboard.
This approach may also be used to manage the color of the navigation bar text from the storyboard:
#IBInspectable var barTextColor: UIColor? {
set {
guard let uiColor = newValue else {return}
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: uiColor]
}
get {
guard let textAttributes = navigationBar.titleTextAttributes else { return nil }
return textAttributes[NSAttributedStringKey.foregroundColor] as? UIColor
}
}
Swift 4:
Perfectly working code to change the navigation bar appearance at application level.
// MARK: Navigation Bar Customisation
// To change background colour.
UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)
// To change colour of tappable items.
UINavigationBar.appearance().tintColor = .white
// To apply textAttributes to title i.e. colour, font etc.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
.font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
// To control navigation bar's translucency.
UINavigationBar.appearance().isTranslucent = false
Happy Coding!
UINavigationBar.appearance().barTintColor
worked for me
SWIFT 4 - Smooth transition (best solution):
If you're moving back from a navigation controller and you have to set a diffrent color on the navigation controller you pushed from you want to use
override func willMove(toParentViewController parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = Constants.AppColor
}
instead of putting it in the viewWillAppear so the transition is cleaner.
SWIFT 4.2
override func willMove(toParent parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.tintColor = UIColor.black
}
Below Codes are working for iOS 15
if #available(iOS 15, *) {
// Navigation Bar background color
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor.yourColor
// setup title font color
let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.yourColor]
appearance.titleTextAttributes = titleAttribute
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
In Swift 4
You can change the color of navigation bar. Just use this below code snippet in viewDidLoad()
Navigation Bar color
self.navigationController?.navigationBar.barTintColor = UIColor.white
Navigation Bar Text Color
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
For iOS 11 Large Title Navigation Bar, you need to use largeTitleTextAttributes property
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
The appearance() function not always work for me. So I prefer to create a NC object and change its attributes.
var navBarColor = navigationController!.navigationBar
navBarColor.barTintColor =
UIColor(red: 255/255.0, green: 0/255.0, blue: 0/255.0, alpha: 100.0/100.0)
navBarColor.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.whiteColor()]
Also if you want to add an image instead of just text, that works as well
var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
imageView.contentMode = .ScaleAspectFit
var image = UIImage(named: "logo")
imageView.image = image
navigationItem.titleView = imageView
Swift 5 (iOS 14)
Full navigation bar customization.
// -----------------------------------------------------------
// NAVIGATION BAR CUSTOMIZATION
// -----------------------------------------------------------
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.navigationBar.tintColor = UIColor.white
self.navigationController?.navigationBar.isTranslucent = false
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = UIColor.blue
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
} else {
self.navigationController?.navigationBar.barTintColor = UIColor.blue
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
}
// -----------------------------------------------------------
// NAVIGATION BAR SHADOW
// -----------------------------------------------------------
self.navigationController?.navigationBar.layer.masksToBounds = false
self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2)
self.navigationController?.navigationBar.layer.shadowRadius = 15
self.navigationController?.navigationBar.layer.shadowOpacity = 0.7
Swift 5, an easy approach with UINavigationController extension. At the bottom of this answer are extensions and previews.
First view controller (Home):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.setTintColor(.white)
navigationController?.backgroundColor(.orange)
}
Second view controller (Details):
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.transparentNavigationBar()
navigationController?.setTintColor(.black)
}
Extensions for UINavigationController:
extension UINavigationController {
func transparentNavigationBar() {
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
}
func setTintColor(_ color: UIColor) {
self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: color]
self.navigationBar.tintColor = color
}
func backgroundColor(_ color: UIColor) {
navigationBar.setBackgroundImage(nil, for: .default)
navigationBar.barTintColor = color
navigationBar.shadowImage = UIImage()
}
}
Storyboard view:
Previews:
Use the appearance API, and barTintColor color.
UINavigationBar.appearance().barTintColor = UIColor.greenColor()
In iOS 15, UIKit has extended the usage of the scrollEdgeAppearance, which by default produces a transparent background, to all navigation bars.
Set scrollEdgeAppearance as below code.
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = < your tint color >
navigationController?.navigationBar.standardAppearance = appearance;
navigationController?.navigationBar.scrollEdgeAppearance = navigationController?.navigationBar.standardAppearance
}
This version also removes the 1px shadow line under the navigation bar:
Swift 5: Put this in your AppDelegate didFinishLaunchingWithOptions
UINavigationBar.appearance().barTintColor = UIColor.black
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
UINavigationBar.appearance().shadowImage = UIImage()
iOS 8 (swift)
let font: UIFont = UIFont(name: "fontName", size: 17)
let color = UIColor.backColor()
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName: color], forState: .Normal)
If you have customized navigation controller, you can use above code snippet.
So in my case, I've used as following code pieces.
Swift 3.0, XCode 8.1 version
navigationController.navigationBar.barTintColor = UIColor.green
Navigation Bar Text:
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
It is very helpful talks.
Swift 4, iOS 12 and Xcode 10 Update
Just put one line inside viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.red
In Swift 2
For changing color in navigation bar,
navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
For changing color in item navigation bar,
navigationController?.navigationBar.tintColor = UIColor.blueColor()
or
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]
Swift 3
UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 51/255, green: 90/255, blue: 149/255, alpha: 1)
This will set your navigation bar color like Facebook bar color :)
Swift 3
Simple one liner that you can use in ViewDidLoad()
//Change Color
self.navigationController?.navigationBar.barTintColor = UIColor.red
//Change Text Color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Swift 3 and Swift 4 Compatible Xcode 9
A Better Solution for this to make a Class for common Navigation bars
I have 5 Controllers and each controller title is changed to orange color. As each controller has 5 navigation controllers so i had to change every one color either from inspector or from code.
So i made a class instead of changing every one Navigation bar from code i just assign this class and it worked on all 5 controller Code reuse Ability.
You just have to assign this class to Each controller and thats it.
import UIKit
class NabigationBar: UINavigationBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonFeatures()
}
func commonFeatures() {
self.backgroundColor = UIColor.white;
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor:ColorConstants.orangeTextColor]
}
}
If you're using iOS 13 or 14 and large title, and want to change navigation bar color, use following code:
Refer to barTintColor not applied when NavigationBar is Large Titles
fileprivate func setNavigtionBarItems() {
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithDefaultBackground()
appearance.backgroundColor = .brown
// let naviFont = UIFont(name: "Chalkduster", size: 30) ?? .systemFont(ofSize: 30)
// appearance.titleTextAttributes = [NSAttributedString.Key.font: naviFont]
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
//navigationController?.navigationBar.compactAppearance = appearance
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = .brown
}
}
This took me 1 hour to figure out what is wrong in my code:(, since I'm using large title, it is hard to change the tintColor with largeTitle, why apple makes it so complicated, so many lines to just make a tintColor of navigationBar.
iOs 14+
init() {
let appearance = UINavigationBarAppearance()
appearance.shadowColor = .clear // gets also rid of the bottom border of the navigation bar
appearance.configureWithTransparentBackground()
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
}
iOS 10 Swift 3.0
If you don't mind to use swift frameworks then us UINeraida to change navigation background as UIColor or HexColor or UIImage and change navigation back button text programmatically, change complete forground text color.
For UINavigationBar
neraida.navigation.background.color.hexColor("54ad00", isTranslucent: false, viewController: self)
//Change navigation title, backbutton colour
neraida.navigation.foreground.color.uiColor(UIColor.white, viewController: self)
//Change navigation back button title programmatically
neraida.navigation.foreground.backButtonTitle("Custom Title", ViewController: self)
//Apply Background Image to the UINavigationBar
neraida.navigation.background.image("background", edge: (0,0,0,0), barMetrics: .default, isTranslucent: false, viewController: self)
I had to do
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barStyle = .Black
UINavigationBar.appearance().backgroundColor = UIColor.blueColor()
otherwise the background color wouldn't change
First set the isTranslucent property of navigationBar to false to get the desired colour. Then change the navigationBar colour like this:
#IBOutlet var NavigationBar: UINavigationBar!
NavigationBar.isTranslucent = false
NavigationBar.barTintColor = UIColor (red: 117/255, green: 23/255, blue: 49/255, alpha: 1.0)
Make sure to set the Button State for .normal
extension UINavigationBar {
func makeContent(color: UIColor) {
let attributes: [NSAttributedString.Key: Any]? = [.foregroundColor: color]
self.titleTextAttributes = attributes
self.topItem?.leftBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
self.topItem?.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
}
}
P.S iOS 12, Xcode 10.1

Resources