Swift - Changing Scope Bar Tint Color - ios

I created a search controller with a scope bar programmatically and I couldn't change the scope bar's tint color. I created the search controller with the following code:
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search Buildings or Codes"
navigationItem.searchController = searchController
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["Building Names", "Building Codes"]
searchController.searchBar.delegate = self
There is also a cancel button for the search bar and I changed the tint color using this code:
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
Thank you so much for your help.

let darkRed = UIColor(red: 0.392, green: 0.012, blue: 0.020, alpha: 1.00)
searchController.searchBar.tintColor = darkRed

Related

How to change color of navigation bar of CNContactPickerViewController?

I want to change color of navigation bar shows on CNContactPickerViewController.
I have tried the following code, but it doesn't work.
let contactPicker = CNContactPickerViewController()
// let searchController = UISearchController()
contactPicker.delegate = self
contactPicker.modalPresentationStyle = .fullScreen
// UISearchBar.appearance().text = "SEARCH".localized
// contactPicker.navigationController?.isNavigationBarHidden = true
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .white
contactPicker.displayedPropertyKeys =
[CNContactPhoneNumbersKey, CNContactEmailAddressesKey]
contactPicker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.#count > 0 || phoneNumbers.#count > 0")
present(contactPicker, animated: true)
you can change naviagtion bar colors by:
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().backgroundColor = .blue
this will change it for the whole app so you can change the color before showing the controller and change it back to original on the controller dismissing
If you want to change the color of the text in navigationBar just add these lines to willConnectTo method inside SceneDelegate file:
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().barTintColor = .orange
UINavigationBar.appearance().tintColor = .yellow
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.purple]
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.purple]

UISearchController Swift 4: Spacing Problems

I'm adding a UISearchController but I keep experiencing spacing problems. In particular, when I conduct a search in the search bar, it shifts up and leaves a black space between the tableview and the search bar.
Then, when I type the the black space disappears but there's a large white space between the search bar and the table.
I tried adding the following line but the spacing problem becomes worse:
searchController.hidesNavigationBarDuringPresentation = false
Any helpful hints would be greatly appreciated.
Here is my current code:
class SearchViewController: UITableViewController {
var searchController: UISearchController!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//Set up Table View
self.navigationController?.navigationBar.shadowImage = UIImage()
let searchResultsController = UITableViewController(style: .plain)
searchResultsController.tableView.delegate = self
searchResultsController.tableView.dataSource = self
searchResultsController.tableView.rowHeight = 65
searchResultsController.tableView.register(SearchCell.self, forCellReuseIdentifier: "SearchCell")
// Setup Search Controller
searchController = UISearchController(searchResultsController: searchResultsController)
searchController.searchBar.tintColor = UIColor.white
searchController.searchBar.barTintColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0)
searchController.searchBar.layer.borderColor = UIColor(red: 34/255, green: 167/255, blue: 240/255, alpha: 1.0).cgColor
searchController.searchBar.layer.borderWidth = 1.00
tableView.tableHeaderView?.addSubview(searchController.searchBar)
let searchBar = searchController.searchBar
searchBar.sizeToFit()
searchBar.placeholder = "Search"
searchController.searchBar.delegate = self
searchController.searchResultsUpdater = self
definesPresentationContext = true
searchController.dimsBackgroundDuringPresentation = false
tableView.tableHeaderView = searchController.searchBar
}
try to Add
self.automaticallyAdjustsScrollViewInsets = false
self.extendedLayoutIncludesOpaqueBars = true
in viewDidload.
or uncheck "Adjust Scroll View Insets"

iOS 11 navigation bar transparent with large titles

I have default preferences for uinavigationbar:
UINavigationBar.appearance().barTintColor = .red
UINavigationBar.appearance().titleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes = [ NSAttributedStringKey.foregroundColor:#colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)]
}
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 0.99997437, blue: 0.9999912977, alpha: 1)
Structure: UINaviagtionController -> PostsTableViewController -> PostTableViewController.
I want to have large navigation bar on PostsViewController (with opaque background color) and transparent navigation bar on PostViewController.
In PostTableViewController i add:
self.navigationController?.navigationBar.backgroundColor = UIColor.clear
self.navigationController?.navigationBar.barTintColor = .clear
self.automaticallyAdjustsScrollViewInsets = false
//self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
//self.navigationController?.navigationBar.shadowImage = UIImage()
if #available(iOS 11.0, *) {
print("asd")
self.tableView.insetsContentViewsToSafeArea = false
self.tableView.contentInsetAdjustmentBehavior = .never
} else {
self.automaticallyAdjustsScrollViewInsets = false
}
Navigation bar is BLACK. I dont understand how make it transparent...
UPDATE:
git hub repository: https://github.com/Mazorati/testnavbar
i set isTranslucent = true, but large navigation becomes also transparent. Only default navigation is ok.
But i want navigationbar color to be opaque, so:
self.navigationController?.view.backgroundColor = .red
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .automatic
As the result:
3D layers:
have you tried changing it to translucent?
self.navigationController?.navigationBar.isTranslucent = true
UPDATE:
You must remember that the navigation bar belongs to the UINavigationController which holds the views inside it, so that when you change it in one it changes for all.
The solution would be to change it to translucent in the second view controller's viewWillAppear method and change it to not translucent in the viewDidDissapear method. Play with the viewWill and viewDid appear methods until you find the right one for your needs.
Try this, this make your navigation bar transparent.
let img = UIImage()
navigationController?.navigationBar.shadowImage = img
navigationController?.navigationBar.setBackgroundImage(img, for: .default)

Transparent/translucent navigation bar with button items in Swift

I'm trying to get an almost transparent navigation bar. But I don't want the buttons on it to be transparent.
This is my code:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor()
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true
navigationController?.navigationBar.alpha = 0.3
This does make it translucent but it also makes the buttons translucent/faded. How can I have a translucent bar but still have the buttons opaque?
Try this:
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3)
navigationController?.navigationBar.barTintColor = UIColor.blackColor()
navigationController?.navigationBar.translucent = true

Changing the Color of Navigation Bar in Swift

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

Resources