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.
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.)
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
}
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
}
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 UINavigationBar UISearchBar and UIBarButtonItem overlap
Try DispatchQueue.main.async
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
DispatchQueue.main.async {
if self.traitCollection.horizontalSizeClass == .regular {
self.navigationItem.leftBarButtonItem = self.button
} else {
self.navigationItem.leftBarButtonItem = nil
}
}
}
Related Topics
Generating an Unsigned IPA iOS Application
Uitextview Content Size Different in iOS7
iOS 12 Terminates Apps in the Background for No Reason
Does Firebase Cloud Messaging Support Voip Pushkit Services
Ios: Determine If Device Language Is Right to Left (Rtl)
Service Workers and iOS/Safari
Using Custom Fonts in Interface Builder
Button Tap and Long Press Gesture
Xcode8 List of Simulators Not Showing
How to Change Uibutton Title Color
Ios: Delete All Core Data Swift
Programmatic Uiscrollview with Autolayout
How to Populate Uitableview from the Bottom Upwards
Read a Text File Line by Line in Swift
Right Aligned Uitextfield Spacebar Does Not Advance Cursor in iOS 7