I want my UINavigationBar to be completely transparent except for the title and the buttons I add to it.
I just can't seem to make it work. I've tried everything already. This made the most sense to me:
override func viewDidLoad() {
self.title = "CURRENT BALANCE"
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController!.navigationBar.shadowImage = UIImage()
self.navigationController!.navigationBar.isTranslucent = true
self.setupSideMenu()
self.topUpButton.asCircle()
self.exchangeButton.asCircle()
self.lockButton.asCircle()
}
this is my OC code to make UINavigationBar to be completely transparent:
first set the VC to UINavigationControllerDelegate, then override - (void) navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
// change the backgroudcolor black
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.000 green:0.000 blue:0.000 alpha:1.000];
// change the alpha 0.3
self.navigationController.navigationBar.alpha = 0.300;
// change the translucent YES
self.navigationController.navigationBar.translucent = YES;
mainwhile, check your code about the self.view.backgroudcolor, if any view under your UINavigationBar have a backgroudcolor, your UINavigationBar will display the view's color.
Set background color property of navigation bar
self.navigationController!.navigationBar.backgroundColor = UIColor.clear
Update:
self.navigationController!.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetricsDefault)
This is transparent NavigationController code for swift 4:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationBar.shadowImage = UIImage()
self.navigationBar.isTranslucent = true
}
Related
I have a view controller with a navigation bar with a large title. When I push the controller, only on iOS 13 is a line visible under the Navigation bar. How can I solve it?
I have already tried several solutions on Stack but they have not worked like:
let navigationBar = navigationController?.navigationBar
let navigationBarAppearence = UINavigationBarAppearance()
navigationBarAppearence.shadowColor = .clear
navigationBar?.scrollEdgeAppearance = navigationBarAppearence
With this snippet, even if I change the "clear color" with red color it is visible only in the first controller, in the pushed controller it is always gray.
How can I solve it?
Edit
I've solved with:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.shadowColor = nil
navigationController?.navigationBar.standardAppearance = appearance
}
import UIKit
public protocol HideableHairlineHelper {
func hideHairline()
func showHairline()
}
extension HideableHairlineHelper where Self: UIViewController {
public func hideHairline() {
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.toolbar.setShadowImage(UIImage(), forToolbarPosition: .any)
}
public func showHairline() {
self.navigationController?.navigationBar.shadowImage = nil
}
}
I've tried the above-suggested ones and failed to remove the navigation separator line. Eventually, I've figured it out that to use TransparentBackground
The trick is to initialize UINavigationBarAppearance with TransparentBackground. Then you could easily remove the horizontal line of the navigation bar.
let appearance = UINavigationBarAppearance()
appearance.configureWithTransparentBackground()
appearance.backgroundColor = UIColor.green // Required background color
Then add the appearance changes to the navigation item as the apple suggested.
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
self.navigationItem.compactAppearance = appearance
This is my NavBar template I use on my controllers, there's no line.
navigationController?.setNavigationBarHidden(false, animated: false)
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
let titleAttributes =
[
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18),
NSAttributedString.Key.foregroundColor: UIColor.red,
]
self.navigationController?.navigationBar.titleTextAttributes = titleAttributes
I obviously change the font size and colour around but that's the basic function I put for each controller to get it clear(translucent).
When the push/pop transition is performed on my view controller(which has large title and search bar embedded in navigation item), the black line appears briefly, like this:
I've basically tried changing all the navigation bar colour related things, but nothing helped.
Any help would be appreciated :)
extension UINavigationBar {
var customStyle: NavigationBarCustomStyle {
set(style) {
switch style {
case .clear:
self.setBackgroundImage(UIImage(), for: .default)
self.shadowImage = UIImage()
self.tintColor = .white
self.isTranslucent = false
break
case .bottomLine:
self.tintColor = .gray
self.backgroundColor = .yellow
self.isTranslucent = false
break
}
}
get {
return self.customStyle
}
}
}
enum NavigationBarCustomStyle {
case clear
case bottomLine
// case white
}
at ViewController >> viewDidLoad method put below line:
self.navigationController?.navigationBar.customStyle = .clear
Try set navbar's background color to white (depend on your case) fix the problem for me, though there still another glitch but its better :)
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.view.backgroundColor = .white
}
How to change the background color and text color of done button? Is there a way that I can change the navigationbar color and navigation bar title color and bottom bar color also? Attached screenshot for reference:
I solved it. Here is the code working for me perfectly:
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
UINavigationBar.appearance().barTintColor = Colors.redColor()
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white, NSFontAttributeName: UIFont.systemFont(ofSize: 14, weight: UIFontWeightBold)]
return self
}
It's a little hacky as its relying on the fact that QLPreviewController is the class implementing the UIDocumentInteractionController but something like this is the least intrusive solution. Do it before you display the UIDocumentInteractionController
import QuickLook
UIBarButtonItem.appearance(whenContainedInInstancesOf [QLPreviewController.self]).tintColor = UIColor.black
I have a idear to change the bar color:
let allNavigationBar = UINavigationBar.appearance()
allNavigationBar.barTintColor = UIColor.red // change the bar background color
allNavigationBar.tintColor = UIColor.black // change the Done button's tintColor
let alloolbar = UIToolbar.appearance()
allToolbar.barTintColor = UIColor.red // dones't work, try backgroundImage
allToolbar.backgroundColor = UIColor.blue // dones't work
allToolbar.tintColor = UIColor.brown // change the toolbar's item tint color
but this method has a great effect,all your UINavigationBarand UIToolBar will make that change.
Hope anyone else can give a better solusion.
You can change the tint color of the window temporally.
func presentDocument() {
//present the controller here
self.appDelegate.window.tintColor = UIColor.red
}
Then change it back later:
func documentInteractionControllerDidEndPreview(documentInteractionController) { //replace parameter with your uidocumentviewinteractioncontroller
self.appDelegate.window.tintColor = UIColor.white
}
#Dee. I guess you have asked this part in one of your other question. In that case you were not able to show that preview controller. In that question suggested answer is to return "self" from that delegate method. If you implement that correctly then your preview will use same navigation bar colour as its parent controller is using. I mean if you have opened UIDocumentInteractionController directly from some ViewController then UIDocumentInteractionController will use its parent viewController's navigation bar colour. This may help you to change Done button colour
Try this : (You need to implement UIDocumentInteractionControllerDelegate)
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.navigationController ?? self
}
let QLNavAppearance = UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self])
QLNavAppearance.tintColor = UIColor.red // some
QLNavAppearance.barTintColor = UIColor.red // some
QLNavAppearance.backgroundColor = UIColor.red // some
I am implementing a dark mode in my app. Here is my code (that I call when the screen is double tapped):
if darkMode == false {
UINavigationBar.appearance().tintColor = UIColor(hexString: "#3A3A3A")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
} else {
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
UINavigationBar.appearance().barTintColor = UIColor(hexString: "#FFFDF3")
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]
The only thing that updates is my status bar, but the navigation bar does update after I go into another view and return back to the main view. Why is that? Is there something I'm doing wrong?
I was just dealing with the same issue, turns out if you change appearance() proxy at runtime it doesn't have any effect. You need to change directly the properties of instances. So what you need to do is have subclassed UINavigationBarController with method where you set the colors and status bar appearance, for instance:
class ColorNavigationController: UINavigationController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
setupForColor(UIFactory.sharedInstance.tintColor) //provides default color
}
func setupForColor(color: UIColor) {
navigationBar.tintColor = color
navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
}
}
Then when you double tap the screen:
if let colorNavigationController = self.navigationController as? ColorNavigationController {
colorNavigationController.setupForColor(UIColor.redColor) // based on your current scheme
}
Got it. You can't change appearance() at runtime, but you can just do navigationController?.navigationBar.tintColor = UIColor.redColor()
I want to develop UINavigationBar and also set background color for that. I have created the UINavigationBar but I have problem with setting backgroundcolor. anyone please help me. Thanks.
[self.navigationController.navigationBar setBackgroundColor:[UIColor redColor]];
Try like this. I think it will be helpful to you.
Edit: updated the code to actually compile.
In the new iOs this it how it works:
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
self.navigationController.navigationBar.barTintColor =[UIColor colorAzulNavegacion];
I have to look this up every time so adding my answer here (Swift). The code below is setting this for all navigation bars in the app. You could set each of these on individual navigation bars too if you wanted to.
You can set the translucency, title text color, background color (this is called barTintColor, thanks, Apple!), and bar button item foreground color, like so:
// Title text color Black => Text appears in white
UINavigationBar.appearance().barStyle = UIBarStyle.Black
// Translucency; false == opaque
UINavigationBar.appearance().translucent = false
// BACKGROUND color of nav bar
UINavigationBar.appearance().barTintColor = UIColor.redColor()
// Foreground color of bar button item text, e.g. "< Back", "Done", and so on.
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
You could use the tint property of the UINavigationBarto change it's color. Check this article about it. There is also UIAppearance, that allows you to change the background of every UINavigationBar of your application, which is quite powerfull in my opinion. You can check this.
You can set the tint color by using navbar.tintColor = [UIColor redColor];
See the reference here: apple docs
Try this:
navigationBar.tintColor = [UIColor blackColor];
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
self.navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
You can customize a UINavigationBar with the following propertys:
#property(nonatomic, assign) UIBarStyle barStyle
#property(nonatomic, retain) UIColor *tintColor
setBackgroundImage:forBarMetrics:
#property(nonatomic, copy) UIColor *backgroundColor
For more methods and propertys please check the class reference of UINavigationBar and UIView
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
Here it is in the context of doing something useful.
In this case programmatically creating/configuring a Navigation Bar item and item and
setting the background to black and the title to light gray.
Swift 5, iOS 15
#objc func addButtonPushed() {
print("Add button!")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let rightButton = UIBarButtonItem(image: UIImage(systemName: "plus"), style: .plain,
target:self, action: #selector(addButtonPushed))
let standaloneItem = UINavigationItem()
standaloneItem.title = "Herding Cats"
standaloneItem.rightBarButtonItem = rightButton
navBar.items = [standaloneItem]
navBar.delegate = self
navBar.barStyle = UIBarStyle.default
navBar.isTranslucent = true
navBar.barTintColor = .black
navBar.titleTextAttributes = [.foregroundColor: UIColor.lightGray]
.
.
.
}