点击图像时显示工具栏 SWIFT
Show toolbar when image is tapped SWIFT
我有一个带有图标的文本字段:
我可以在用户点击文本字段时显示工具栏。但我需要对图标做同样的事情。我尝试使用手势 rezognizer,但无法显示工具栏。有什么问题吗?
func createToolbar() {
toolBar.sizeToFit()
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let tamamButton = UIBarButtonItem(title: NSLocalizedString("done", comment: ""), style: .plain, target: self, action: #selector(loadNewPriceAndReload))
toolBar.setItems([spaceButton,tamamButton], animated: false)
toolBar.isUserInteractionEnabled = true
pickerTextField.inputAccessoryView = toolBar
}
func createPickerView() {
pickerView.delegate = self
pickerView.dataSource = self
pickerView.backgroundColor = #colorLiteral(red: 0.1834549492, green: 0.1834549492, blue: 0.1834549492, alpha: 1)
pickerView.tintColor = .white
pickerTextField.inputView = pickerView
}
我尝试在点击图标时调用函数,但它不起作用。
这是将图标放在文本字段右侧的扩展
extension UITextField {
func setIcon(_ image: UIImage,tap:UIGestureRecognizer) {
let iconView = UIImageView(frame:
CGRect(x: 7, y: 11, width: 15, height: 10))
iconView.image = image
iconView.isUserInteractionEnabled = true
iconView.addGestureRecognizer(tap)
let iconContainerView: UIView = UIView(frame:
CGRect(x: -50, y: 0, width: 30, height: 32))
iconContainerView.layer.cornerRadius = 3
iconContainerView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
iconContainerView.addSubview(iconView)
iconContainerView.backgroundColor = #colorLiteral(red: 0.04554035515, green: 0.5175096393, blue: 0.7420636415, alpha: 1)
rightView = iconContainerView
rightViewMode = .always
}
}
enter image description here
为了显示 pickerView 是由于 textField 还是其他任何原因,我总是倾向于采用这种方法:
let picker = UIPickerView()
picker.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(picker)
picker.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
picker.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
picker.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
我有一个带有图标的文本字段:
我可以在用户点击文本字段时显示工具栏。但我需要对图标做同样的事情。我尝试使用手势 rezognizer,但无法显示工具栏。有什么问题吗?
func createToolbar() {
toolBar.sizeToFit()
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let tamamButton = UIBarButtonItem(title: NSLocalizedString("done", comment: ""), style: .plain, target: self, action: #selector(loadNewPriceAndReload))
toolBar.setItems([spaceButton,tamamButton], animated: false)
toolBar.isUserInteractionEnabled = true
pickerTextField.inputAccessoryView = toolBar
}
func createPickerView() {
pickerView.delegate = self
pickerView.dataSource = self
pickerView.backgroundColor = #colorLiteral(red: 0.1834549492, green: 0.1834549492, blue: 0.1834549492, alpha: 1)
pickerView.tintColor = .white
pickerTextField.inputView = pickerView
}
我尝试在点击图标时调用函数,但它不起作用。
这是将图标放在文本字段右侧的扩展
extension UITextField {
func setIcon(_ image: UIImage,tap:UIGestureRecognizer) {
let iconView = UIImageView(frame:
CGRect(x: 7, y: 11, width: 15, height: 10))
iconView.image = image
iconView.isUserInteractionEnabled = true
iconView.addGestureRecognizer(tap)
let iconContainerView: UIView = UIView(frame:
CGRect(x: -50, y: 0, width: 30, height: 32))
iconContainerView.layer.cornerRadius = 3
iconContainerView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMaxXMaxYCorner]
iconContainerView.addSubview(iconView)
iconContainerView.backgroundColor = #colorLiteral(red: 0.04554035515, green: 0.5175096393, blue: 0.7420636415, alpha: 1)
rightView = iconContainerView
rightViewMode = .always
}
}
enter image description here
为了显示 pickerView 是由于 textField 还是其他任何原因,我总是倾向于采用这种方法:
let picker = UIPickerView()
picker.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(picker)
picker.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
picker.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
picker.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true