Tôi đã tạo lớp tùy chỉnh điều hướng và tôi muốn trang trí nó, tôi đã sử dụng lớp NavDecoration.swift và phần mở rộng đã được hiển thị trong mã bên dưới, tôi đã thêm mã vạch tìm kiếm cũng như chức năng này, tôi muốn đặt đại biểu thanh tìm kiếm mở rộng nhưng lỗi cho nó không thể gán kiểu 'UIViewController' gõ 'UISearchBarDelegateLàm thế nào để thiết lập đại biểu của thanh tìm kiếm của viewController bằng cách sử dụng phần mở rộng?
extension UINavigationController {
func makeBlackNavigationbar (viewController: UIViewController, animated: Bool) {
viewController.navigationController?.navigationBar.backgroundColor? = UIColor.blackColor()
let add = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
let play = UIBarButtonItem(title: "Play", style: .Plain, target: viewController, action: "addTapped")
viewController.navigationItem.rightBarButtonItems = [add, play]
let left = UIBarButtonItem(barButtonSystemItem: .Add, target: viewController, action: "addTapped")
viewController.navigationItem.leftBarButtonItems = [left]
let searchBar = UISearchBar(frame: CGRectZero)
searchBar.placeholder = "Search"
searchBar.delegate = viewController
viewController.navigationItem.titleView = searchBar
}}
Tôi đã làm tất cả đã sẵn sàng và khi tôi thực hiện searchBar.delegate = viewController như thế nó cho tôi lỗi tương tự không thể gán kiểu 'UIViewController' để nhập 'UISearchBarDelegate – IOSDev
Bạn chỉ định UiViewController thay vì UINAvigationController làm đại biểu. Nếu phần mở rộng của bạn như trên bạn phải đặt searchBar.delegate = viewController.navigationController. – stefos