작동 검색 막대가 있으며 아무데도 대리자 메서드를 활성화 할 수 없게되었습니다. 프로그래밍 방식으로 검색 창을 만들었습니다.UISearchBarDelegate 메서드가 호출되지 않습니다.
다음self.searchBar.delegate = self
self.searchBar.translatesAutoresizingMaskIntoConstraints = false
self.searchBar.searchBarStyle = .minimal
self.searchBar.isUserInteractionEnabled = true
let image = self.getImageWithColor(color: UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 0.19), size: CGSize(width: self.customNavBar.frame.width * 0.9440, height: self.customNavBar.frame.height * 0.47727))
searchBar.setSearchFieldBackgroundImage(image, for: .normal)
self.customNavBar.addSubview(self.searchBar)
var horizCenter = NSLayoutConstraint(item: self.searchBar, attribute: .centerX, relatedBy: .equal, toItem: self.customNavBar, attribute: .centerX, multiplier: 1, constant: 0)
var vertConstraints = NSLayoutConstraint(item: self.searchBar, attribute: .top, relatedBy: .equal, toItem: self.customNavBar, attribute: .top, multiplier: 1, constant: 30)
var widthConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .width, relatedBy: .equal, toItem: self.customNavBar, attribute: .width, multiplier: 0.9440, constant: 0)
var heightConstraint = NSLayoutConstraint(item: self.searchBar, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.customNavBar.frame.height * 0.47727)
NSLayoutConstraint.activate([horizCenter, vertConstraints, widthConstraint, heightConstraint])
내가 위에서 언급 한 getImageWithColor 방법입니다 : 여기
내 초기화 코드이다func getImageWithColor(color: UIColor, size: CGSize) -> UIImage {
let rect = CGRect(x:0, y:0, width:size.width,height: size.height)
let path = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
color.setFill()
path.fill()
let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
그리고 검색 창 내부 텍스트 필드를 사용자 정의 계속 :
UITextField.appearance().tintColor = UIColor.white
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName : UIFont(name: "Gotham-Book", size: 14.0)!]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
let textFieldInsideSearchBar = self.searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor(red: 255/255.0, green: 255/255.0, blue: 255/255.0, alpha: 1.0)
textFieldInsideSearchBar?.font = UIFont(name: "Gotham Medium", size: 20.0)
textFieldInsideSearchBar?.clearsOnBeginEditing = true
textFieldInsideSearchBar?.borderStyle = .none
textFieldInsideSearchBar?.clearButtonMode = .whileEditing
textFieldInsideSearchBar?.isUserInteractionEnabled = true
내 모든 대표단 방법 :
인터페이스 캡처에서 볼 수 있듯이화면 캡처 이미지 :여기
extension ListViewController : UISearchBarDelegate {
func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
print("\nfunc searchBarTextDidBeginEditing\n")
self.hideDayDropDown()
UIView.animate(withDuration: 0.25) {
self.dayButton.alpha = 0.0
self.dayButtonLabel.alpha = 0.0
self.dayDropDownArrow.alpha = 0.0
self.mapButton.alpha = 0.0
self.mapButton.isEnabled = false
self.dayButton.isUserInteractionEnabled = false
self.tableView.transform = CGAffineTransform(translationX: 0.0, y: -(self.tableView.frame.minY - self.customNavBar.frame.maxY))
self.tableView.translatesAutoresizingMaskIntoConstraints = true
self.tableView.frame = CGRect(x: 0, y: self.customNavBar.frame.maxY, width: self.view.frame.width, height: 300)
self.dealOfTheDayLabel.alpha = 0.00
self.exploreSpecialView.alpha = 0.0
self.exploreSpecialsLabel.alpha = 0.0
}
searchActive = true
self.searchBar.setShowsCancelButton(true, animated: true)
UIView.animate(withDuration: 0.25) {
self.searchIconImage.alpha = 0.0
self.searchIconWhileEditing.alpha = 1.0
}
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
print("\nfunc searchBarTextDidEndEditing\n")
UIView.animate(withDuration: 0.25) {
self.dayButton.alpha = 1.0
self.dayButtonLabel.alpha = 1.0
self.dayDropDownArrow.alpha = 1.0
self.dayButton.isUserInteractionEnabled = true
self.searchIconImage.alpha = 1.0
self.searchIconWhileEditing.alpha = 0.0
self.tableView.transform = CGAffineTransform(translationX: 0.0, y: 0.0)
self.tableView.translatesAutoresizingMaskIntoConstraints = false
self.mapButton.alpha = 1.0
self.mapButton.isEnabled = true
self.dealOfTheDayLabel.alpha = 1.0
self.exploreSpecialView.alpha = 1.0
self.exploreSpecialsLabel.alpha = 1.0
}
searchActive = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
print("\nfunc searchBarCancelButtonClicked\n")
self.searchBar.setShowsCancelButton(false, animated: true)
self.searchBar.text = ""
self.searchBar.resignFirstResponder()
searchActive = false
self.searchIconImage.alpha = 1.0
self.searchIconWhileEditing.alpha = 0.0
self.dayDropDownArrow.alpha = 1.0
self.tableView.transform = CGAffineTransform(translationX: 0.0, y: 0.0)
self.dealOfTheDayLabel.alpha = 1.0
self.exploreSpecialView.alpha = 1.0
self.exploreSpecialsLabel.alpha = 1.0
tableView.reloadData()
}
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.searchResults = self.library.filter({ (bar: BarLibrary) -> Bool in
let tmp: String = bar.name!
let range = tmp.localizedLowercase.range(of: searchText.localizedLowercase)
return range != nil
})
if(self.searchResults.count == 0){
searchActive = false
} else {
searchActive = true
}
self.tableView.reloadData()
}
}
또한 검색 창 내 탐색 모음의 내 인터페이스 캡처에서 이미지입니다 정면에서보기 이름은 백업 : UITextFieldBorderView
, UISearchBarTextField
는 UIView
는 UISearchBar
참고는 잘 모르겠어요 무엇 UIView
오른쪽 0,123,635 전은
다른 사람들이 자신의 UISearchBar
대리자 방법에 액세스 할 수없는 이유에 대해 많은 질문이 있습니다. 그 해결책들 중 어느 것도 나에게 효과가 없었다. 지금까지 왜 내 대리자 메서드에 액세스 할 수 없습니까?
대리자가 절대로 호출되지 않으면 대리자 메서드의 서명을 확인하십시오. 그리고 그것은'ListViewController' 클래스의 확장으로서 당신의 세부 사항을 구현하는 좋은 코딩 스타일이 아니라고 생각합니다. – muescha
@muescha 노트는 확장 기능에서 코딩 세부 사항을 취했습니다. 내 대리자 메서드 서명이 정확하다고 생각합니다. – user3354144