Status bar changes color when searching - ios

I'm using a UISearchController that, when clicked, makes the navigation bar rise and turn the status bar white. I want the status bar to remain green.
My UISearchController implementation:
self.resultSearchController = ({
let controller = UISearchController(searchResultsController: nil)
controller.searchResultsUpdater = self
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.sizeToFit()
controller.searchBar.placeholder = "Pesquisa por linha ou cod..."
controller.searchBar.tintColor = UIColor(netHex: 0xFFFFFF)
controller.searchBar.barTintColor = UIColor(netHex: 0x2F7C30)
controller.searchBar.clipsToBounds = true
controller.searchBar.layer.borderWidth = 1;
controller.searchBar.layer.borderColor = UIColor(netHex: 0x2F7C30).CGColor
self.navigationController!.view.backgroundColor = UIColor(netHex: 0x2F7C30)
self.tableView.tableHeaderView = controller.searchBar
return controller
})()
Before clicking on the search bar
After clicking on the search bar

Playing off of Daniel Storm's response. Adding the subview to the UISearchController's view solved the issue for me.
let statusBarView = UIView(frame: CGRectMake(0, 0,
UIApplication.sharedApplication().statusBarFrame.size.width,
UIApplication.sharedApplication().statusBarFrame.size.height))
statusBarView.backgroundColor = Colors.Primary.Regular
searchController.view.addSubview(statusBarView)

Put a UIView behind your status bar with the background color you need.
let statusBarView = UIView(frame: UIApplication.sharedApplication().statusBarFrame)
statusBarView.backgroundColor = UIColor.greenColor() // Replace with color you desire
view.addSubview(statusBarView)

By setting
scrollEdgeAppearance
buttonAppearance
backButtonAppearance
works for me fine here is the code snippet
func setNavigationBarAppreance(){
let navBar = UINavigationBar.appearance()
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithOpaqueBackground()
navigationBarAppearance.backgroundColor = .primary // replace your desired color
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.navigationBarTintColor]
navigationBarAppearance.shadowImage = UIImage()
navigationBarAppearance.backgroundImage = UIImage()
navBar.scrollEdgeAppearance = navigationBarAppearance
navBar.compactAppearance = navigationBarAppearance
navBar.standardAppearance = navigationBarAppearance
navBar.isTranslucent = false
navBar.tintColor = .navigationBarTintColor
navBar.barTintColor = .navBarColor
}
Simply use this function in the desired Viewcontrollers or you can put it in the AppDelegate file for global appearance settings

It is enough to set backgroundImage for navigationImage
navigationController?.navigationBar.setBackgroundImage(UIImage(from: UIColor(hexString: kMainColorHex, alpha: 1)), for: .any, barMetrics: .default)

Try this:
self.navigationController?.navigationBar.isTranslucent = false

For objective C version you can use the following codes:
UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIApplication sharedApplication].statusBarFrame.size.width, [UIApplication sharedApplication].statusBarFrame.size.height)];
statusBarView.backgroundColor = [UIColor searchBarBackgroundColor];
[resultSearchController.view addSubview:statusBarView];

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]

Swift UINavigationBar background image does not cover status bar when clipToBounds is true

I'm setting the background image of a large title navigation bar like this:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
self.title = "Nav bar"
let largeTitleAppearance = UINavigationBarAppearance()
largeTitleAppearance.configureWithOpaqueBackground()
largeTitleAppearance.backgroundImage = UIImage(named: "kunal-shinde--f0YLss50Bs-unsplash")
largeTitleAppearance.backgroundImageContentMode = .scaleAspectFill
largeTitleAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
largeTitleAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navigationController?.navigationBar.standardAppearance = largeTitleAppearance
navigationController?.navigationBar.scrollEdgeAppearance = largeTitleAppearance
}
However, the image overflows out of the navigation bar as shown here:
I thought clipsToBounds might work so I set it to true at the end of viewDidLoad
navigationController?.navigationBar.clipsToBounds = true
This is better, but the image does not cover the status bar! How can I get it to cover the status bar while not overflowing?
We've solved this as followed:
We set the background image on ViewController. This is the same as we use in navigation bar.
let imageView = UIImageView(image: .assetImage(.background))
imageView.contentMode = .topLeft
collectionView.backgroundView = imageView
Style the navigation bar.
// General Styling
UINavigationBar.appearance().tintColor = .assetColor(.navigationBarTintColor)
// Bar Styling
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.shadowColor = .clear
navBarAppearance.backgroundColor = UIColor(patternImage: .assetImage(.background)!)
UINavigationBar.appearance().standardAppearance = navBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
In addition we change the translucent of the navigation bar for non-root views:
if isRootview {
self.navigationController?.navigationBar.isTranslucent = true
} else {
self.navigationController?.navigationBar.isTranslucent = false
}

How do I change the background of my UISearchbar search field in Swift 5?

How do I change the background of my searchfield? It is set to white now which makes it invisible to the user.
current look
This is my code:
//CHANGE COLOR OF NAVIGATION BAR
navigationController?.navigationBar.barTintColor = UIColor.white
navigationController?.navigationBar.isTranslucent = false
//SEARCH FIELD
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self as? UISearchBarDelegate
let frame = CGRect(x: 0, y: 0, width: 300, height: 44)
let titleView = UIView(frame: frame)
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.frame = frame
titleView.addSubview(searchController.searchBar)
navigationItem.titleView = titleView
}
It is very simple. You just need to add this line :
searchController.searchBar.backgroundColor = .gray //You can set whichever color you want here

How do I set up my navigation search bar in Swift to look like the Reddit App

I just started to learn how to code with Swift in Xcode, I need some help regarding the search bar. I want to add a search field bar to my navigation bar in center, and next to it i want to add two items . So far I managed to add a UISearch to my navigation bar but once I try to add items next to it, it pushes my icons above the search field.
Pressed State
Normal State
Does anyone know what to add the two menu items next to it programmatically or in the storyboard? And how to make the search field centered and a bit thinner?
My code now:
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self as? UISearchControllerDelegate
let searchBar = searchController.searchBar
searchBar.tintColor = UIColor.white
searchBar.barTintColor = UIColor.white
if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.blue
}
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
Reddit Example
Okay, fixed it! Just used the code below. Now my next struggle is changing the background of the textfield.
//SEARCH
let searchController = UISearchController(searchResultsController: nil)
searchController.hidesNavigationBarDuringPresentation = false
searchController.dimsBackgroundDuringPresentation = false
searchController.searchBar.delegate = self as? UISearchBarDelegate
let frame = CGRect(x: 0, y: 0, width: 300, height: 44)
let titleView = UIView(frame: frame)
searchController.searchBar.backgroundImage = UIImage()
searchController.searchBar.frame = frame
titleView.addSubview(searchController.searchBar)
navigationItem.titleView = titleView
}

Navigating between view controller with search controller search bar hide and show having ui glitch

I have used simple UISearchController in UITableViewController. This has issue when I use search bar in first controller and which is hidden in next or back forth it shows UI glitch.
Here is how it looks>>
Here is below code for setting up navigation bar.
self.extendedLayoutIncludesOpaqueBars = true
let navigationBar = navigationController.navigationBar
let navigationBarTitleTextAttritbutes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.titleTextAttributes = navigationBarTitleTextAttritbutes
if #available(iOS 11.0, *) {
navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
navigationBar.prefersLargeTitles = false
}
navigationBar.isTranslucent = false
navigationBar.tintColor = UIColor.white
navigationBar.barTintColor = UIColor.purple
And For setting search controller used as below
self.searchController = UISearchController(searchResultsController: nil)
self.searchController.dimsBackgroundDuringPresentation = false
self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.hidesBottomBarWhenPushed = true
self.searchController.obscuresBackgroundDuringPresentation = false
let searchBarObj = self.searchController.searchBar
searchBarObj.delegate = self as? UISearchBarDelegate
searchBarObj.placeholder = "Search here"
searchBarObj.isTranslucent = false
searchBarObj.tintColor = .white
if #available(iOS 11.0, *) {
searchBarObj.tintColor = .white
searchBarObj.barTintColor = .white
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
let tintColor = UIColor.purple
searchBarObj.barTintColor = tintColor
searchBarObj.layer.borderColor = tintColor.cgColor
searchBarObj.layer.borderWidth = 1
if let table = tableview {
table.tableHeaderView = searchController.searchBar
}
}

Resources