Changing textfield font of UISearchBar also changes it's height - swift

I am changing UISearchBar's UITextField font size:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [.font: UIFont.systemFont(ofSize: 17)]
or
searchBar.searchTextField.font = .systemFont(ofSize: 17)
This is the same font size value as the default. But UISearchBar also changes its height. Looks like it's a little bit bigger than the default height value. How I can fix it?
Default UISearchBar height:
After font size change (pay attention that actual font size is the same as on screenshot above):
UPDATE:
I have a storyboard with UITabBarViewController in it. Then I have two UIViewControllers connected to UITabBarViewController. One of them is empty and another one contains UINavigationController with connected sample VIewController which is UITableViewController. Here is a sample code of test ViewController:
import UIKit
class ViewController: UITableViewController {
private lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.autocapitalizationType = .none
searchController.searchBar.placeholder = "Search"
searchController.searchBar.barStyle = .black
searchController.searchBar.tintColor = .black
searchController.searchBar.keyboardAppearance = .light
return searchController
}()
override func viewDidLoad() {
super.viewDidLoad()
title = "TEST"
navigationItem.searchController = searchController
navigationItem.searchController?.searchBar.searchTextField.font = .systemFont(ofSize: 17)
definesPresentationContext = true
}
}

Related

How to achieve the auto-shrink SwiftUI Navigation Title with UIKit?

I am trying to achieve the auto-shrink large nav title like the Navigation Title in SwiftUI, but no matter what my nav title always stay shrinked and never appear in large title mode. How do i fix this? (code below)
func setup() {
table.translatesAutoresizingMaskIntoConstraints = false
table.dataSource = self
table.delegate = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
table.separatorColor = .white
table.backgroundColor = .black
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
appearance.configureWithOpaqueBackground()
navigationItem.title = "Todo List"
navigationItem.scrollEdgeAppearance = appearance
navigationItem.standardAppearance = appearance
view.addSubview(table)
}
As well as providing a configuration for large titles, you need to instruct your navigationController to use large titles
navigationController.navigationBar.prefersLargeTitles = true
The default configuration for a view controller should be to automatically use large titles if that is the preferred choice. However you can set it explicitly per view controller:
navigationItem.largeTitleDisplayMode = .always // or .automatic or .never
If the nav controller has prefersLargeTitle = false then large titles will never be displayed whatever the largeTitleDisplayMode setting.

Remove extra space on top of UISearchController in UINavBar

I added a UISearchController in the navigation bar. But it has this extra top padding which I don't want. I used UIsearchController instead of a UITextField because I need its searchResultsController. Is there a way to remove the extra space, or a way to copy the searchResultsController behavior with UITextField. I created the view programmatically btw.
override func viewDidLoad() {
setupSearchBar()
}
func setupSearchBar(){
let searchController = UISearchController(searchResultsController: ResultController)
searchController.searchResultsUpdater = self
searchController.searchBar.placeholder = "Search"
navigationItem.searchController = searchController
navigationController?.navigationBar.backgroundColor = .yellow
}
Set 'automaticallyAdjustsScrollViewInsets' to false
Setting the automaticallyAdjustsScrollViewInsets value to false

Is this code correct for enable large titles?

import UIKit
class GoalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Goal"
view.backgroundColor = .white
}
I have ever made Large Titles on this way yet, but now, the VC is completely white, can someone tell me why?
Have you tried this:
navigationItem.largeTitleDisplayMode = .always
You can also use .never for never showing large title or .automatic for inheriting the mode from the previous navigation item

Need to show navigation title while using searchBar in navigationBar

I need to show large navigation title while search with searchBar, but when I'm clicking on Searchbar or starting to type my NavigationTitle is replaced with SearchBar. I checked methods for navigationItem and didn't find any suitable.
Does anybody know how can we always show title, even while searching?
Here is my code
class FavouriteVC: UIViewController {
let searchController = UISearchController(searchResultsController: nil)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.title = "Favourite"
navigationItem.searchController = searchController
navigationItem.largeTitleDisplayMode = .always
}
}
And here are shots
screen with NavTitle
screen without NavTitle
Seems like UISearchController has a hidesNavigationBarDuringPresentation that prevents the navigation bar from hiding when set to false
Include:
searchController.hidesNavigationBarDuringPresentation = false
in your viewDidLoad and if I've understood your issue right, this should do what you need.

Cant change colour of searchBar and it jumps to top of view when selected

I'm trying to attach a search bar to the top of my tableView and change its attributes (eg. colour, placeholder). However, I can't figure out how. I've tried embedding the tableView in another view but that didn't help. Any ideas?
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.tintColor = UIColor.red
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
navigationItem.searchController = search
tableView.tableHeaderView = search.searchBar
}
This function is called in the viewDidLoad() and the tableView is added but not with the right colour or placeholder and jumps to the top of the screen when selected.
Any help would be appreciated.
This is the updated code for setupSearch (everything is working fine except the bar jumps to the top when selected):
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
tableView.tableHeaderView = search.searchBar
}
I declare the search bar at the start using:
let search = UISearchController(searchResultsController: nil)
Any ideas on how to stop the bar jumping to the top?
Just add search bar as tableview's headerview not with navigation's item searchcontroller(not add search bar with both(tableview and navigation) as in your code). You can try with updated code below:
func setupSearch(){
search.delegate = self
search.automaticallyShowsCancelButton = false
search.searchBar.barTintColor = UIColor.red
search.obscuresBackgroundDuringPresentation = false
search.hidesNavigationBarDuringPresentation = false
search.searchBar.placeholder = "Type something here to search"
tableView.tableHeaderView = search.searchBar
self.definesPresentationContext = true
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearch()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil) // be notified when the keyboard changes your table View frame
}
#objc func keyboardWillShow(notification: NSNotification) {
tableView.frame.origin.y = 0 // reset the table view to its original coordinates
}
func setupSearch(){
// for iOS 12 and lower, you can change the placeholder like this :
let textFieldSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldSearchBar?.textColor = .red
let searchBarLabel = textFieldSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldSearchBarLabel?.textColor = .red
}