To trochę obejście problemu.
Przede wszystkim trzeba się pole tekstowe paska wyszukiwania i centrum wyrównanie tekstu:
let textFieldOfSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField textFieldOfSearchBar?.textAlignment = .center
Po tym, trzeba zmienić ustawienie elementu zastępczego. Z jakiegoś powodu nie zmienia się z wyrównaniem pola tekstowego. Należy to zrobić poprzez dodanie dopełnienie do lewej widzenia tekstowym tylko gdy sterownik wyszukiwania jest aktywny, więc używać go za metody Delegat:
func presentSearchController(_ searchController: UISearchController) {
//Is active
let width: CGFloat = 100.0 //Calcualte a suitable width based on search bar width, screen size, etc..
let textfieldOfSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
let paddingView = UIView(x: 0, y: 0, w: width, h: searchController.searchBar.frame.size.height)
textfieldOfSearchBar?.leftView = paddingView
textfieldOfSearchBar?.leftViewMode = .unlessEditing
}
func willDismissSearchController(_ searchController: UISearchController) {
let textfieldOfSearchBar = searchController.searchBar.value(forKey: "searchField") as? UITextField
textfieldOfSearchBar?.leftView = nil
}
Powodzenia
Tak chcę, aby umieścić go w środku też. Jak ciężko Apple pozwala nam ustawić searchBar.placeHolder.textAlignment = NSTextAlignmentCenter? – coolcool1994