本文介绍: 前言项目为实现监听用户是否在某个TextFiled输入然后改变边框颜色,这里就需要或者到当前的响应者。效果图:一、Swift代码extension UIView { var firstResponder: UIView? { guard !isFirstResponder else { return self } for subview in subviews { if let firstResponder = subview.fi
前言
项目为实现监听用户是否在某个TextFiled输入然后改变边框颜色,这里就需要或者到当前的响应者。
效果图:
一、Swift代码
extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else { return self }
for subview in subviews {
if let firstResponder = subview.firstResponder {
return firstResponder
}
}
return nil
}
}
二、OC代码
@implementation UIView (FindFirstResponder)
- (id)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.view.subviews) {
if ([subView isFirstResponder]) {
return subView;
}
}
return nil;
}
或者
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
三、效果代码
class ViewController: UIViewController {
var textFielddd: UITextField?
var textFieldcc: UITextField?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let textField = UITextField(frame: CGRect(x:20, y:100, width:self.view.bounds.width - 40, height:30))
textField.borderStyle = UITextField.BorderStyle.none
textField.layer.borderWidth = 1.0
textField.layer.cornerRadius = 8
textField.layer.borderColor = UIColor.black.cgColor
textFielddd = textField
view.addSubview(textField)
let textField1 = UITextField(frame: CGRect(x:20, y:150, width:self.view.bounds.width - 40, height:30))
textField1.borderStyle = UITextField.BorderStyle.none
textField1.layer.borderWidth = 1.0
textField1.layer.cornerRadius = 8
textField1.layer.borderColor = UIColor.black.cgColor
textFieldcc = textField1
view.addSubview(textField1)
//键盘弹起
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(aNotification:)), name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHied(aNotification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
//监听键盘弹出
@objc func keyboardWillShow(aNotification: NSNotification) {
if let firstResponder = view.window?.firstResponder {
if firstResponder == textFielddd {
print("打印一下当前响应者:ss--------")
textFielddd?.layer.borderColor = UIColor.red.cgColor
textFieldcc?.layer.borderColor = UIColor.black.cgColor
}else if firstResponder == textFieldcc {
print("打印一下当前响应者:tttt--------")
textFielddd?.layer.borderColor = UIColor.black.cgColor
textFieldcc?.layer.borderColor = UIColor.red.cgColor
}
}
}
//监听键盘收回
@objc func keyboardWillHied(aNotification: NSNotification){
textFielddd?.layer.borderColor = UIColor.black.cgColor
textFieldcc?.layer.borderColor = UIColor.black.cgColor
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else { return self }
for subview in subviews {
if let firstResponder = subview.firstResponder {
return firstResponder
}
}
return nil
}
}
END.
原文地址:https://blog.csdn.net/smileKH/article/details/122088775
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_10155.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。