NavigationItem TitleView 约束不起作用
NavigationItem TitleView constraint not working
我想使用 UITextField 创建一个搜索栏。我尝试将 NavigationItem 的 TitleView 设置为文本字段并设置相应的约束。但是导航栏中心的文本字段仍然很短。如何让文本框填满整个导航栏?这是我的代码:
cancelbtn = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CourseTableViewController.searchCancelled))
self.navigationItem.leftBarButtonItem = cancelbtn
let searchField = UITextField()
searchField.translatesAutoresizingMaskIntoConstraints = false
searchField.borderStyle = .roundedRect
self.navigationItem.titleView = searchField
self.navigationItem.titleView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: cancelbtn.width)
self.navigationItem.titleView!.widthAnchor.constraint(equalToConstant: view.frame.size.width - cancelbtn.width)
它看起来是这样的 rn:
UIView
您应该阅读 Apple 相关指南。从 iOS11 您应该将 searchBar 安装到 navigationItem 中。尝试使用此代码:
import UIKit
class ViewController: UIViewController, UISearchControllerDelegate {
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = false
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
// Customisation
searchController.searchBar.tintColor = .black
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
}
我解决了我的问题。我没有使用 titleView,而是将文本字段放在右侧的 barButtonItem 中,并将其宽度设置为 view.frame.size.width。这给了我一个占据整个导航的文本字段。我在这里附上了代码以防有人也想这样做。
searchBar = UISearchBar()
searchBar.showsCancelButton = false
searchBar.placeholder = "Course name"
searchBar.delegate = self
cancelbtn = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CourseTableViewController.searchCancelled))
self.navigationItem.leftBarButtonItem = cancelbtn
searchTextField = UITextField()
searchTextField.borderStyle = .roundedRect
searchTextField.widthAnchor.constraint(equalToConstant: view.frame.size.width - cancelbtn.width)
searchTextField.translatesAutoresizingMaskIntoConstraints = false
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchTextField)
self.navigationItem.rightBarButtonItem?.width = view.frame.size.width
我想使用 UITextField 创建一个搜索栏。我尝试将 NavigationItem 的 TitleView 设置为文本字段并设置相应的约束。但是导航栏中心的文本字段仍然很短。如何让文本框填满整个导航栏?这是我的代码:
cancelbtn = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CourseTableViewController.searchCancelled))
self.navigationItem.leftBarButtonItem = cancelbtn
let searchField = UITextField()
searchField.translatesAutoresizingMaskIntoConstraints = false
searchField.borderStyle = .roundedRect
self.navigationItem.titleView = searchField
self.navigationItem.titleView!.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: cancelbtn.width)
self.navigationItem.titleView!.widthAnchor.constraint(equalToConstant: view.frame.size.width - cancelbtn.width)
它看起来是这样的 rn: UIView
您应该阅读 Apple 相关指南。从 iOS11 您应该将 searchBar 安装到 navigationItem 中。尝试使用此代码:
import UIKit
class ViewController: UIViewController, UISearchControllerDelegate {
var searchController = UISearchController()
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = false
searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
// Customisation
searchController.searchBar.tintColor = .black
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
}
}
我解决了我的问题。我没有使用 titleView,而是将文本字段放在右侧的 barButtonItem 中,并将其宽度设置为 view.frame.size.width。这给了我一个占据整个导航的文本字段。我在这里附上了代码以防有人也想这样做。
searchBar = UISearchBar()
searchBar.showsCancelButton = false
searchBar.placeholder = "Course name"
searchBar.delegate = self
cancelbtn = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(CourseTableViewController.searchCancelled))
self.navigationItem.leftBarButtonItem = cancelbtn
searchTextField = UITextField()
searchTextField.borderStyle = .roundedRect
searchTextField.widthAnchor.constraint(equalToConstant: view.frame.size.width - cancelbtn.width)
searchTextField.translatesAutoresizingMaskIntoConstraints = false
self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: searchTextField)
self.navigationItem.rightBarButtonItem?.width = view.frame.size.width