I wan to stick my navigation bar on top for every pages that i have in app.
I have my Code below
navigationController?.hidesBarsOnSwipe = true
navigationController?.hidesBarsOnTap = true
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?.navigationBar.opaque = true
navigationController?.navigationBar.translucent=true
navigationController?.navigationBar.alpha = 0.4
navigationController?.navigationBar.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.4)
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
self.navigationController?.setNavigationBarHidden(false, animated: false)
What can i do to just stick navigation bar?
Thanks,
Dhaval.
Ok, here some guide, assume that you're a do-everything-by-code guy.
In app delegate:
let tempVC = UIViewController()
tempVC.backgroundColor = UIColor.redColor()
let navVC = UINavigationController(rootViewController:tempVC)
window?.rootViewController = navVC
it should work.
Then you do your navigationBar customization code in here or in your viewcontroller.
Issue resolved
Code below
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
navigationController?.navigationBar.barStyle = UIBarStyle.BlackTranslucent
navigationController?. navigationBar. hidden=false
Related
I want to change navigation bar color of QLPreviewController in swift 3. I have used below code to change color but its not working
viewQLPreview = QLPreviewController()
viewQLPreview.dataSource = self
viewQLPreview.delegate = self
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red
I have used below code to Change navigation bar color of QLPreviewController in swift 3.0
UINavigationBar.appearance().barTintColor = UIColor.red
UINavigationBar.appearance(whenContainedInInstancesOf: [QLPreviewController.self]).backgroundColor = UIColor.red
Use below code before present QLPreviewController :
UINavigationBar.appearance().tintColor = UIColor.red
UINavigationBar.appearance().barTintColor = UIColor.blue
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.red]
UINavigationBar.appearance().setBackgroundImage(fromColor(color: UIColor.blue), for: .default)
UINavigationBar.appearance().isTranslucent = false
func fromColor (color: UIColor) -> UIImage{
let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
UIGraphicsBeginImageContext(rect.size)
let context: CGContext? = UIGraphicsGetCurrentContext()
context?.setFillColor(color.cgColor)
context?.fill(rect)
let image: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image ?? UIImage()
}
Put below code in viewDidLoad of QLPreviewController.
viewQLPreview.navigationController?.navigationBar.isTranslucent = false
viewQLPreview.navigationController?.navigationBar.tintColor = UIColor.red
Also make sure that viewQLPreview.navigationController != nil
If you are pushing QLPreviewController then this code will work....
If you are presenting QLPreviewController then you need to make sure that rootController should be navigation controller, in your case..
let viewQLPreview = QLPreviewController()
let nav = UINavigationController(rootViewController: viewQLPreview)
self.present(nav, animated: true, completion: nil)
There is a strange thing that occurs when I transition from one view with a large title navigation bar that has been customized with a different shadow to another view that has a regular height navigation bar. You can see the black box in this gif
Here is the related code that lives in both the main view and detail view
Main view:
func setupNavBar() {
// Set the nav bar to have large titles. This is on a per instance basis
navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.layer.shadowColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha: 0.3).cgColor
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
navigationController?.navigationBar.layer.shadowRadius = 6.0
navigationController?.navigationBar.layer.shadowOpacity = 1.0
navigationController?.navigationBar.layer.masksToBounds = false
}
Detail view:
func setupNavBar() {
self.title = colorPalette?.title
navigationController?.navigationBar.prefersLargeTitles = false
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.tintColor = UIColor.black
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.layer.shadowColor = UIColor(red:0.87, green:0.87, blue:0.87, alpha: 0.3).cgColor
navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
navigationController?.navigationBar.layer.shadowRadius = 6.0
navigationController?.navigationBar.layer.shadowOpacity = 1.0
navigationController?.navigationBar.layer.masksToBounds = false
}
There is no view that has a black background in interface builder either. I'm not sure why this is happening.
Instad of
navigationController?.navigationBar.isTranslucent = false
use
navigationController?.navigationBar.isTranslucent = true
Check ✓ Extended Edges Under Opaque Bars on storyboard, or set
extendedLayoutIncludesOpaqueBars = true
Or set the navigationBar's translucent to true.
navigationController?.navigationBar.isTranslucent = true
I have transparent navigation bar with a background image for view controller, But when I add a bar button item to navigation bar, it becomes like in the second picture. How do I have bar button items also fully transparent navigation bar.
I used these code below to make the navigation bar transparent;
extension UINavigationController {
public func presentTransparentNavigationBar() {
navigationBar.setBackgroundImage(UIImage(), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = true
navigationBar.shadowImage = UIImage()
setNavigationBarHidden(false, animated:true)
}
public func hideTransparentNavigationBar() {
setNavigationBarHidden(true, animated:false)
navigationBar.setBackgroundImage(UINavigationBar.appearance().backgroundImageForBarMetrics(UIBarMetrics.Default), forBarMetrics:UIBarMetrics.Default)
navigationBar.translucent = UINavigationBar.appearance().translucent
navigationBar.shadowImage = UINavigationBar.appearance().shadowImage
}
}
This should create a transparent UINavigationBar with items in it. It's currently working fine for me.
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.tintColor = UIColor.whiteColor()
navigationBarAppearace.translucent = true
navigationBarAppearace.shadowImage = UIImage()
navigationBarAppearace.backgroundColor = UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
navigationBarAppearace.setBackgroundImage(UIImage(), forBarMetrics: .Default)
Try:
if let navBar = self.navigationController?.navigationBar {
extendedLayoutIncludesOpaqueBars = true
navigationBar.translucent = true
navigationBar.backgroundColor = UIColor.clearColor()
navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default)
navigationBar.shadowImage = UIImage()
}
I used a navBar which is created programmatically with an UISearchController.
When I start editing the UISearchBar:
- the navBar stays behind the dim view
- the tableView hides half of the navbar
- when the tabeView appeared, the cancel buttons are not selectable.
My UISearchController :
let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("SearchTableViewController") as! SearchTableViewController
resultSearchController = UISearchController(searchResultsController: locationSearchTable)
resultSearchController.searchResultsUpdater = locationSearchTable
resultSearchController.view.backgroundColor=UIColor.clearColor()
locationSearchTable.delegate = self
let searchBar = self.resultSearchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchBar.searchBarStyle = .Minimal
searchBar.barStyle = .Default
searchBar.translucent = true
searchBar.barTintColor = UIColor.whiteColor()
searchBar.setImage(UIImage(named:"search"), forSearchBarIcon: .Search, state : .Normal)
searchBar.delegate=self
resultSearchController.hidesNavigationBarDuringPresentation = false
resultSearchController.dimsBackgroundDuringPresentation = true
definesPresentationContext = true
My navBar :
let newNavBar : UINavigationBar = UINavigationBar.init(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 64.0))
func styleNavBar (){
UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.Default
self.navigationController?.setNavigationBarHidden(true, animated: false)
let newItem : UINavigationItem = UINavigationItem.init()
newItem.titleView = self.resultSearchController.searchBar
newNavBar.barTintColor = UIColor(red: 243/255, green: 242/255, blue: 238/255, alpha: 1.0)
newNavBar.translucent = false
if let font = UIFont(name: "Avenir-Black", size: 16.0) {
let navBarAttributesDictionary : [String : AnyObject]? = [
NSForegroundColorAttributeName: UIColor(red: 74/255, green: 74/255, blue: 74/255, alpha: 0.51),
NSFontAttributeName: font
]
newNavBar.titleTextAttributes = navBarAttributesDictionary
}
newNavBar.setItems([newItem], animated: false)
self.view.addSubview(newNavBar)
self.view.bringSubviewToFront(newNavBar)
}
How can I fix it, please?
Thanks!
Try using this :
self.navigationController?.navigationBar.layer.zPosition = 1
you can use stackview for it . select search and tableview and click on stackview and constraint stackview from top/left/right/bottom
I have a MKMap in my ViewController and I allow my users to switch map types between "Standard", "Hybrid", and "Satellite" - much like in the Maps app. Like in the maps app I have a bottom bar, and then I have a navigation bar at the top. When the user switches between map types, the bars should change to a black background with white buttons for "Hybrid" and "Satellite", and a white background with blue buttons for "Standard.
I have the bottom bar changing colors correctly but am unable to get the navigation bar to change at all. Here is my function for when a user changes map types:
func changeMapType(sender:UISegmentedControl){
if mapType.selectedSegmentIndex == 0{
mapView.mapType = MKMapType.Standard
toolbar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
toolbar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor(red:1.0, green:1.0, blue:1.0, alpha:1.0)
self.navigationController?.navigationBar.tintColor = UIColor(red:0.0, green:122.0/255.0, blue:1.0, alpha:1.0)
defaults.setValue("Standard", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 1{
mapView.mapType = MKMapType.Hybrid
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Hybrid", forKey: "initialMapType")
}
else if mapType.selectedSegmentIndex == 2{
mapView.mapType = MKMapType.Satellite
toolbar.barTintColor = UIColor.blackColor()
toolbar.tintColor = UIColor.whiteColor()
self.navigationController?.navigationBar.translucent = false
self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
defaults.setValue("Satellite", forKey: "initialMapType")
}
}
Does anyone know what I must do differently to get my nav bar to change colors?
It is very probably because your self.navigationController is empty. Try that first! It is o-so-very common problem. Maybe you are calling it from wrong view controller that in fact does not have NC. You can go around that problem by using .appearance() interface, like this:
To change bar tint color (background of navigation bar):
UINavigationBar.appearance().barTintColor = UIColor.whiteColor()
To change color of the texts:
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.whiteColor()]
Be aware that this changes navigation bar for entire application (basically, appearance is the way how to change default setting), so it might not be appropriate. If you are interested, here you can read more about appearance.
Hope it helps!
Edit: Easy way how to check for empty navigation controller would be force-unwrapping the variable, self.navigationController!.navigationBar instead of ?, if your app crashes, your problem lies here (but don't tell to anyone I suggested that as it is not very slick solution how to find the problem, though fast)
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
Also can check here : https://github.com/hasnine/iOSUtilitiesSource