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
}
}
}
iOS 8 search bar in a UIBarButtonItem?
Add a Search Bar and Display Controller next to your UITableViewController First Responder in your UI.
Add the UISearchBarDelegate and UISearchDisplayDelegate in your code and in your viewDidLoad method add the following code to display the search bar in the navigation bar:
Objective-C:
self.searchDisplayController.displaysSearchBarInNavigationBar = YES;
Swift:
self.searchDisplayController?.displaysSearchBarInNavigationBar = true
Then execute the delegate methods:
Objective-C:
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
Swift:
func searchDisplayController(controller: UISearchDisplayController!, shouldReloadTableForSearchString searchString: String!) -> Bool
func searchBar(searchBar: UISearchBar, textDidChange searchText: String)
Changing the font style for UISearchBar cancel button
Try something like this it worked for me.
UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal)
If you want to change the color as well add this to your array of attributes.
NSForegroundColorAttributeName : UIColor.whiteColor()
Note: Made available in iOS 9
How to change the default text of Cancel Button which appears in the UISearchBar +iPhone
You also need to have the "searchBar setShowsCancelButton" before the procedure.
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller
{
[theSearchBar setShowsCancelButton:YES animated:NO];
for (UIView *subView in theSearchBar.subviews){
if([subView isKindOfClass:[UIButton class]]){
[(UIButton*)subView setTitle:@"Done" forState:UIControlStateNormal];
}
}
}
Note also: use UIButton to avoid problems with Apple!
Animate UISearchBar and UIBarButtonItem simultaneously with UISearchController
In case anyone is interested, I managed to make it work using UISearchDisplayController
. See this sample project.
I'm still interested by a solution that works with UISearchController
though.
Related Topics
Swift Update Label (With HTML Content) Takes 1Min
App Delegate Accessing Environment Object
Rxswift Merge Different Kind of Observables
Mutable Binding in Swiftui Live Preview
Localizewithformat and Variadic Arguments in Swift
How to Retrieve a Random Object from Firebase Using a Sequential Id
Why Can the Keyword "Weak" Only Be Applied to Class and Class-Bound Protocol Types
Get the Last Character of a String Without Using Array
Difference Between Using Objectidentifier() and '===' Operator
Back Button Image - What Is It Called in Swift
"Unrecognized Selector Sent to Instance" in Swift
Swift String Interpolation Displaying Optional
Can an Enum Contain Another Enum Values in Swift
What Does "Get" Mean in a Protocol's Property Declaration
How to Add Kerning to a Textfield in Swiftui