iOS 11 customise search bar in navigation bar
I just found out how to set also the rest of them: (with some help of Brandon, thanks!)
The "Cancel" text:
searchController.searchBar.tintColor = .white
The search icon:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
The clear icon:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
The search text:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
Thanks for the help @Brandon!
The placeholder:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
The white background:
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
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
Taken from here.
Show search bar in navigation bar without scrolling on iOS 11
The following makes the search bar visible at first, then allows it to hide when scrolling:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = false
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 11.0, *) {
navigationItem.hidesSearchBarWhenScrolling = true
}
}
Using isActive
didn't do what I wanted, it makes the search bar active (showing cancel button, etc.), when all I want is for it to be visible.
iOS 11 UISearchBar in UINavigationBar
Now it's what you want...
if #available(iOS 11.0, *) {
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.white
scb.barTintColor = UIColor.white
if let textfield = scb.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 = sc
navigationItem.hidesSearchBarWhenScrolling = false
}
Result:
With Rounded corner:
Animation with rounded corner is also working fine.
ios11: UISearchBar in Navigation Bar
I believe the search bar and the nav bar are two separate elements so your gradient is only effecting one. You can set them both to clear and then put another view or label element behind (add constraints) with the gradient you're looking for.
for the searchBar you want to set both the barTintColor and the backgroundImage with the following code.
override func viewDidLoad() {
self.searchBar.barTintColor = UIColor.clear
self.searchBar.backgroundImage = UIImage()
self.navBar.backgroundColor = UIColor.clear
}
iOS 11 UISearchBar in navigation bar
Look answer in UIPercentDrivenInteractiveTransition. It's using for update UIViews during UINavigationController transition in persentage value.
Other words, depends on how much UINavigationController already opened next view controller or move backwards views will have different appearance.
This value will help you update your search bar (alpha, width, etc.)
UISearchBar increases navigation bar height in iOS 11
I got black line under NavigationBar with SearchBar in iOS 11 in two cases:
when i pushed another ViewControllers from ViewController with UISearchBar
when i dismissed ViewController with UISearchBar with "drag right to dismiss"
My solution was: adding this code to my ViewController with UISearchBar:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController.view setNeedsLayout]; // force update layout
[self.navigationController.view layoutIfNeeded]; // to fix height of the navigation bar
}
Swift 4 Update
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.view.setNeedsLayout() // force update layout
navigationController?.view.layoutIfNeeded() // to fix height of the navigation bar
}
Related Topics
How to Change Uisearchbar Placeholder and Image Tint Color
Control Cursor Position in Uitextfield
Swift3 iOS - How to Make Uitapgesturerecognizer Trigger Function
How to Do Some Stuff in Viewdidappear Only Once
The Executable Gets Signed with Invalid Entitlements in Xcode
@Binding and Foreach in Swiftui
Ios 8.1 Simulator Always Uses Us Keyboard Layout Despite German Hardware Keyboard
Converting Between Nsdata and Base64 Strings
App Rejected Because of Advertisingidentifier in Facebook Sdk and Flurry Sdk
Change Text Color of Items in Uiactionsheet - iOS 8
Core Data: Do Child Contexts Ever Get Permanent Objectids for Newly Inserted Objects
Gradient Polyline with Mapkit iOS