【问题标题】:resolving the issue coming with text field and button image issue解决文本字段和按钮图像问题带来的问题
【发布时间】:2018-03-21 09:25:26
【问题描述】:

我是 swift 新手。我创建了简单的登录屏幕。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
    }

怎么做

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

-> 高亮两个文本字段的底部边框。

您可以从此链接https://drive.google.com/file/d/1zjNUBXZ-9WL4DTglhMXIlN-TpaO5IXBz/view?usp=sharing下载项目

【问题讨论】:

    标签: swift uibutton uitextfield


    【解决方案1】:

    要检查按钮中的图像,您可以使用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
    }
    

    【讨论】:

      【解决方案2】:

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

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

      然后在你的逻辑设置按钮状态的某个地方

      button.setSelected(true)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-28
        • 1970-01-01
        • 2019-01-23
        • 2012-06-22
        • 1970-01-01
        相关资源
        最近更新 更多