当两个输入框都输入文本。点击键盘的确定键后 让键盘变为灰色
新建view
//
// CustomView.swift
// DemoApp
//
// Created by liuan on 2020/5/3.
// Copyright © 2020 anguo.com. All rights reserved.
//
import UIKit
//自定义文本视图
protocol DemoDelegate {
func checkForm()
}
class CustomView: UIView,UITextFieldDelegate {
var textField:UITextField!
var delegate:DemoDelegate?
override init(frame:CGRect){
super.init(frame: frame)
textField=UITextField(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))
textField.font=UIFont.boldSystemFont(ofSize: 14)
textField.textColor = .purple
textField.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
textField.layer.shadowOpacity=0.45
textField.layer.shadowRadius=3
textField.backgroundColor = .lightGray
textField.delegate=self
self.addSubview(textField)
}
//键盘回车被按下
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.delegate?.checkForm()
return true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//
// ViewController.swift
// DemoApp
//
// Created by liuan on 2020/4/23.
// Copyright © 2020 anguo.com. All rights reserved.
//
import UIKit
class ViewController: UIViewController,DemoDelegate {
var nameFiled:CustomView!
var passwordField:CustomView!
var submitButton:UIButton!
var animationView:UIView!
override func viewDidLoad(){
super.viewDidLoad()
let width=Int(self.view.frame.size.width)-40
let height=40
self.nameFiled = CustomView(frame: CGRect(x: 20, y: 60, width: width, height: height))
self.nameFiled.delegate=self
self.view.addSubview(self.nameFiled)
self.passwordField = CustomView(frame: CGRect(x: 20, y: 140, width: width, height: height))
self.passwordField.delegate=self
self.view.addSubview(self.passwordField)
self.submitButton = UIButton(frame: CGRect(x: 20, y: 240, width: width, height: height))
self.submitButton.setTitle("提交", for: .normal)
self.submitButton.backgroundColor = .gray
self.submitButton.addTarget(self, action: #selector(ViewController.submitForm(_:)), for: .touchUpInside)
self.view.addSubview(self.submitButton)
}
@objc func submitForm(_ sender:UIButton){
print("submitForm(_:)")
}
func checkForm() {
if self.nameFiled.textField.text != "" && self.passwordField.textField.text != ""
{
self.submitButton.isEnabled=true
self.submitButton.backgroundColor = .orange
}else{
self.submitButton.isEnabled=false
self.submitButton.backgroundColor = .gray
}
}
}