为什么在使用“登录ID”和“密码”命中文本字段时图像不会更改?我是iOS的新手,我不知道这个代码的问题是什么。当我想点击LogID和密码字段时,图标不会改变。有人可以告诉我如何解决这个问题。
UITextField(扩展名)
class TextField
{
static func imgTFEmail1(tfLgnID: UITextField)
{
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad
{
//Set image within username textfield
tfLgnID.leftViewMode = .always
imgViewLgn = UIImageView(frame: CGRect(x: tfLgnID.frame.origin.x + 3, y: tfLgnID.frame.origin.y + 20, width: 35, height: 30))
imgViewLgn.contentMode = .scaleAspectFit
imgLogin = UIImage(named: "user1")!
imgViewLgn.image = imgLogin
tfLgnID.leftView = imgViewLgn
//Text and Placeholder Font Size Large
let attributes = [
NSAttributedStringKey.foregroundColor: UIColor.lightGray,
NSAttributedStringKey.font : UIFont(name: "Avenir-Heavy", size: 25)!
]
tfLgnID.attributedPlaceholder = NSAttributedString.init(string: "Login Id", attributes: attributes)
//Label Detail increase the font weight and family
tfLgnID.font = UIFont(name: "Avenir-Heavy", size: 28)
}
else if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone
{
//Set image within username textfield
tfLgnID.leftViewMode = .always
imgViewLgn = UIImageView(frame: CGRect(x: -5, y: 0, width: 25, height: 22))
imgViewLgn.contentMode = .scaleAspectFit
imgLogin = UIImage(named: "user1")!
imgViewLgn.image = imgLogin
tfLgnID.leftView = imgViewLgn
}
}
static func imgTFPwd1(tfPwd: UITextField)
{
if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad
{
//Set image within password textfield
tfPwd.leftViewMode = .always
imgViewPwd = UIImageView(frame: CGRect(x: tfPwd.frame.origin.x + 3, y: tfPwd.frame.origin.y + 20, width: 35, height: 30))
imgViewPwd.contentMode = .scaleAspectFit
imgPwd = UIImage(named: "password1")!
imgViewPwd.image = imgPwd
tfPwd.leftView = imgViewPwd
//Text and Placeholder Font Size Large
let attributes = [
NSAttributedStringKey.foregroundColor: UIColor.lightGray,
NSAttributedStringKey.font : UIFont(name: "Avenir-Heavy", size: 25)!
]
tfPwd.attributedPlaceholder = NSAttributedString.init(string: "Password", attributes: attributes)
//Label Detail increase the font weight and family
tfPwd.font = UIFont(name: "Avenir-Heavy", size: 28)
}
else if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone
{
//Set image within password textfield
tfPwd.leftViewMode = .always
imgViewPwd = UIImageView(frame: CGRect(x: -5, y: 0, width: 25, height: 22))
imgViewPwd.contentMode = .scaleAspectFit
imgPwd = UIImage(named: "password1")!
imgViewPwd.image = imgPwd
tfPwd.leftView = imgViewPwd
}
}
的ViewController
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
//Show the horizontal line in the bottom of text fields
self.designTextField()
TextField.imgTFEmail1(tfLgnID: tfLgnID)
TextField.imgTFPwd1(tfPwd: tfPwd)
}
func textFieldDidBeginEditing(_ textField: UITextField)
{
//Move the screen up when tap on TextField
self.view.frame.origin.y -= 80
if textField == self.tfLgnID
{
bottomTFEmail.backgroundColor = UIColor(red: 11/255, green: 174/255, blue: 250/255, alpha: 1).cgColor
TextField.imgTFEmail2(tfLgnID: tfLgnID)
TextField.imgTFPwd2(tfPwd: tfPwd)
}
else if textField == self.tfPwd
{
imgPwd = UIImage(named: "password2")!
bottomTFPwd.backgroundColor = UIColor(red: 11/255, green: 174/255, blue: 250/255, alpha: 1).cgColor
}
}
func textFieldDidEndEditing(_ textField: UITextField)
{
self.view.frame.origin.y = 0
if textField == self.tfLgnID
{
bottomTFEmail.backgroundColor = UIColor.lightGray.cgColor
imgLogin = UIImage(named: "user1")!
imgViewLgn.image = imgLogin
tfLgnID.leftView = imgViewLgn
if !(Utility.validateEmail(candidate: self.tfLgnID.text!))
{
let alertController = UIAlertController.alertCtrl(title: "Warning", message: "Please enter your correct email address", buttonTitle: "OK")
self.present(alertController, animated: true, completion: nil)
}
}
else if textField == self.tfPwd
{
bottomTFPwd.backgroundColor = UIColor.lightGray.cgColor
imgPwd = UIImage(named: "password1")!
imgViewPwd.image = imgPwd
tfPwd.leftView = imgViewPwd
}
}
结果
没有击中
点击