Swift5学习之旅------UIToolbar(工具栏)、UIPickerView(选择器视图)键盘响应事件
如果能提供到帮助请点个赞再关闭吧?
GitHub还没上传,用着Dropbox先吧(可能要翻墙
https://www.dropbox.com/sh/h7po1afkvpdf5p8/AAAjTKtKSPedsnxLerJnn_iRa?dl=0
Dropbox ToolBar UIPickerView
- UIPickerView
import UIKitclass MyPickerView: UIPickerView,UIPickerViewDataSource,UIPickerViewDelegate {
override init(frame: CGRect) {
super.init(frame: frame)self.delegate = selfself.dataSource = self}required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")}//设置选择框的总列数,继承于UIPickViewDataSource协议func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2}//设置选择框的总行数,继承于UIPickViewDataSource协议func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//总行数设置为数据源的总长度。Component:0表示第一列if component == 0 {
return Data.pickerProvincesData.count}else{
return Data.pickerCitiesData.count}}//设置选项框各选项的内容,继承于UIPickViewDelegate协议func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//选择第一级数据if component == 0 {
return Data.pickerProvincesData[row]}else{
//第二级数据return Data.pickerCitiesData[row]}}//选择空间的事件选择func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if(component == 0){
//记录用户选择的值let selectedProvince = Data.pickerProvincesData[row] as String//根据第一选择的值,记录第二列的数据Data.pickerCitiesData = Data.pickerData[selectedProvince]!//刷新第二列的数据源self.reloadComponent(1)//刷新数据源后将第二组数据转到下标为0,并且启动效果self.selectRow(0, inComponent: 1, animated: true)
// print(selectedProvince)}}//设置每行的高度func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 45.0}}
- UIToolBar
import UIKit
class MyToolBar: UIToolbar,UITextFieldDelegate {
override init(frame: CGRect) {
super.init(frame: frame)inputToolbarView.frame = CGRect(x: 5, y: 5, width: self.bounds.width - 200, height: self.bounds.height - 10)inputTextField.frame = CGRect(x: 5, y: 5, width: self.bounds.width - 200, height: self.bounds.height - 10)inputTextField.borderStyle = .roundedRectinputTextField.delegate = selfinputToolbarView.addSubview(inputTextField)let barItem3 = UIBarButtonItem(customView: inputToolbarView)self.setItems([barItem3,btngap,barItem1,barItem2], animated: true)self.backgroundColor = UIColor.clearbarItem2.tintColor = UIColor.red}required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")}var barItem1 = UIBarButtonItem(image: UIImage(systemName: "square.and.arrow.up"), style: .done, target: self, action: #selector(upload(_:)))@objc func upload(_ sender: UIBarButtonItem) {
print("上传")}var inputToolbarView = UIView()let inputTextField = UITextField()var text: String! = ""var barItem2 = UIBarButtonItem(title: "输入", style: .plain, target: self, action: #selector(second(_:)))let btngap = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)@objc func second(_ sender: UIBarButtonItem) {
print(text ?? "")}func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
text = (textField.text! as NSString).replacingCharacters(in: range, with: string)return true}
- 键盘响应事件
//键盘弹起@objc func keyboardWillAppear(notification: NSNotification) {
// 获得软键盘的高
// let keyboardinfo = notification.userInfo![UIResponder.keyboardFrameBeginUserInfoKey]keyboard = 350toolBar.frame.origin.y = self.view.bounds.height - 60 - keyboardprint(keyboard)}//键盘落下@objc func keyboardWillDisappear(notification:NSNotification){
//软键盘收起的时候恢复原始偏移UIView.animate(withDuration: 0.5) {
self.keyboard = 0self.toolBar.frame.origin.y = self.view.bounds.height - 60print(self.keyboard)}}//点击屏幕键盘退出override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
view.endEditing(true)}