我有一个以编程方式创建的searchBar
。我只是想要这样,一旦单击searchBar
内部的背面图像,就会调用ViewController
内部的函数。我已经尝试了所有我知道的东西,但是我似乎无法弄清楚delegate
为什么总是返回nil。
ViewControllerClass
class ProductByCategoryView: UIViewController {
// Content container
var category: ProductCategory!
var searchHeaderBrief: SearchHeaderBrief! = SearchHeaderBrief()
lazy var container: UIStackView = {
let contentView = UIStackView(frame: .zero)
contentView.axis = .vertical
contentView.spacing = 10
contentView.willSetConstraints()
return contentView
}()
var searchHeader: SearchHeaderBrief! // Header section
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.setupInterface()
}
// MARK: SEARCH HEADER
private func setupSearch() {
self.searchHeaderBrief.backDelegate = self
self.searchHeader = SearchHeaderBrief(frame: .zero)
self.searchHeader.willSetConstraints()
self.container.addArrangedSubview(self.searchHeader)
let constraints = [
self.searchHeader.topAnchor.constraint(equalTo: self.container.topAnchor),
self.searchHeader.heightAnchor.constraint(equalToConstant:130),
self.searchHeader.widthAnchor.constraint(equalTo: self.view.widthAnchor),
]
NSLayoutConstraint.activate(constraints)
DispatchQueue.main.async {
self.searchHeader.backgroundColor = UIColor(patternImage: UIImage(named: "header_background")!.resize(to: CGSize(width: self.searchHeader.bounds.width, height: self.searchHeader.bounds.height)))
self.searchHeader.setGradientBackground(gradientTop: UIColor.constants.darkBlue.withAlphaComponent(0.9).cgColor, gradientBottom: UIColor.constants.darkBlue.cgColor, opacity: 0.9)
self.searchHeader.applyConstraints()
}
}
private func setupInterface() {
self.embedInScrollView(content: self.container)
self.setupSearch() // Search
DispatchQueue.main.async {
self.container.setBackground(color: UIColor.constants.lightGray)
}
}
}
extension ProductByCategoryView: BackDelegate {
func goBack() {
self.back()
}
}
CustomViewClass
protocol BackDelegate: AnyObject {
func goBack()
}
class SearchHeaderBrief: UIView {
lazy var searchBox: UITextField! = {
return self.searchField()
}()
// var filterDelegate: SearchHeaderDelegateBrief?
var backDelegate: BackDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.setup()
}
func setup() {
self.addSubview(self.searchBox)
self.setNeedsDisplay()
}
func searchField() -> UITextField {
let search = UITextField(frame: .zero)
search.layer.backgroundColor = UIColor.white.cgColor
// Search Icon
let searchIconView = UIView(frame: CGRect(x: 0, y: 0, width: 32, height: 40))
let searchIcon = UIImageView(frame: CGRect(x: 7.5, y: 11.5, width: 17, height: 17))
searchIcon.contentMode = .scaleAspectFill
searchIcon.image = UIImage(named: "ic_back")!
let singleTap = UITapGestureRecognizer(target: self, action: #selector(tapDetected))
searchIcon.isUserInteractionEnabled = true
searchIcon.addGestureRecognizer(singleTap)
searchIconView.addSubview(searchIcon)
search.leftView = searchIconView
search.leftViewMode = .always
// Filter icon
let filterIconView = UIView(frame: CGRect(x: 0 , y: 0, width: 40, height: 40))
filterIconView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(self.showSearchFilters)))
filterIconView.isUserInteractionEnabled = true
let filterIcon = UIImageView(frame: CGRect(x: 6.5 , y: 9.5, width: 27, height: 21))
filterIcon.image = UIImage(named: "ic_filter")!
filterIcon.contentMode = .scaleAspectFit
filterIconView.addSubview(filterIcon)
search.rightView = filterIconView
search.rightViewMode = .always
search.layer.cornerRadius = 3
search.translatesAutoresizingMaskIntoConstraints = false
search.clipsToBounds = true
search.addShadow()
return search
}
func applyConstraints() {
NSLayoutConstraint.activate([
self.searchBox.heightAnchor.constraint(equalToConstant: 43),
self.searchBox.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 15),
self.searchBox.trailingAnchor.constraint(equalTo: self.trailingAnchor, constant: -15),
self.searchBox.topAnchor.constraint(equalTo: self.topAnchor, constant: 40),
])
self.layoutIfNeeded()
}
@objc func showSearchFilters() {
// self.filterDelegate?.filter()
}
//Action
@objc func tapDetected() {
self.backDelegate?.goBack()
}
}
最佳答案
更新你的方法
// MARK: SEARCH HEADER
private func setupSearch() {
self.searchHeaderBrief.backDelegate = self
self.searchHeader = SearchHeaderBrief(frame: .zero)
self.searchHeader.willSetConstraints()
self.searchHeader.backDelegate = self
self.container.addArrangedSubview(self.searchHeader)
let constraints = [
self.searchHeader.topAnchor.constraint(equalTo: self.container.topAnchor),
self.searchHeader.heightAnchor.constraint(equalToConstant:130),
self.searchHeader.widthAnchor.constraint(equalTo: self.view.widthAnchor),
]
NSLayoutConstraint.activate(constraints)
DispatchQueue.main.async {
self.searchHeader.backgroundColor = UIColor(patternImage: UIImage(named: "header_background")!.resize(to: CGSize(width: self.searchHeader.bounds.width, height: self.searchHeader.bounds.height)))
self.searchHeader.setGradientBackground(gradientTop: UIColor.constants.darkBlue.withAlphaComponent(0.9).cgColor, gradientBottom: UIColor.constants.darkBlue.cgColor, opacity: 0.9)
self.searchHeader.applyConstraints()
}
}
关于ios - 协议(protocol)代理无法按预期方式返回nil swift ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61675216/