解决文本字段和按钮图像问题带来的问题

resolving the issue coming with text field and button image issue

我是 swift.I 的新手,创建了简单的登录屏幕。loginviewController.swift

有两个问题

问题 1 # 我不知道如何比较按钮中显示的两个图像。我已经使用 if checkbox.setImage(img, for: .normal) 来比较显示在侧面按钮中的图像,以便在选中和未选中之间切换操作

let img = UIImage(named:"check box@1x.png")
    let img2 = UIImage(named:"uncheck box.png")

    @IBOutlet weak var checkbox: UIButton!
    @IBAction func checkbox(_ sender: Any) {
        if checkbox.setImage(img, for: .normal)
        {
        checkbox.setImage(img , for: .normal)
        }
        else{
            checkbox.setImage(img2, for: .normal)
        }
        }

问题 2 # 我正在尝试突出显示文本字段的底部边框。我正在编写用于突出显示两个文本字段的代码,但它只突出显示单个文本字段

func boaderSetting() {

    let border = CALayer()
    let width = CGFloat(1.0)
    border.borderColor = UIColor.orange.cgColor
    border.frame = CGRect(x: 0, y: username_input.frame.size.height - width, width:  username_input.frame.size.width, height: username_input.frame.size.height)

    border.borderWidth = width
    username_input.layer.addSublayer(border)
    username_input.layer.masksToBounds = true
    ///
    let border1 = CALayer()
    let width1 = CGFloat(1.0)
    border1.borderColor = UIColor.orange.cgColor
    border1.frame = CGRect(x: 0, y: password_input.frame.size.height - width1, width:  password_input.frame.size.width, height: password_input.frame.size.height)

    border1.borderWidth = width1
    password_input.layer.addSublayer(border)
    password_input.layer.masksToBounds = true
    }

如何

->将按钮中显示的图像与其他图像进行比较

-> 突出显示两个文本字段的底部边框。

你可以从这里下载项目 link https://drive.google.com/file/d/1zjNUBXZ-9WL4DTglhMXIlN-TpaO5IXBz/view?usp=sharing

要检查按钮中的图像,您可以使用 button.currentImage == image。在您的示例中,它看起来像这样

@IBAction func checkbox(_ sender: Any) {
    if checkbox.currentImage == img2 {
        checkbox.setImage(img , for: .normal)
    } else {
        checkbox.setImage(img2, for: .normal)
    }
}

它仅在底部文本字段边框上突出显示,因为此处的示例存在错误 password_input.layer.addSublayer(border),您希望在此处改用 border1

func boaderSetting() {
    let border = CALayer()
    let width = CGFloat(1.0)
    border.borderColor = UIColor.orange.cgColor
    border.frame = CGRect(x: 0, y: username_input.frame.size.height - width, width:  username_input.frame.size.width, height: username_input.frame.size.height)

    border.borderWidth = width
    username_input.layer.addSublayer(border)
    username_input.layer.masksToBounds = true
    ///
    let border1 = CALayer()
    let width1 = CGFloat(1.0)
    border1.borderColor = UIColor.orange.cgColor
    border1.frame = CGRect(x: 0, y: password_input.frame.size.height - width1, width:  password_input.frame.size.width, height: password_input.frame.size.height)

    border1.borderWidth = width1
    password_input.layer.addSublayer(border1)
    password_input.layer.masksToBounds = true
}

对于#1,您可以简单地为不同的状态设置图像。

button.setImage(UIImage(named: "imageForSelected"), for : .selected)
button.setImage(UIImage(named: "imageForNotSelected"), for : .normal)

然后在你的逻辑中某处设置按钮状态

button.setSelected(true)