更改 uitextfield 的边框颜色 [英] Change the border color of uitextfield

查看:82
本文介绍了更改 uitextfield 的边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式为 UITextField 添加了底部边框:

I added bottom border to UITextField this way:

let bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, self.frame.height, self.frame.width, CGFloat(borderWidth))
bottomLine.backgroundColor = borderColor.CGColor
self.borderStyle = UITextBorderStyle.None
self.layer.addSublayer(bottomLine)

如何在 UITextField 调用 textFieldDidBeginEditing() 时更改此底线的边框颜色,然后在调用 textFieldDidEndEditing() 时将背景颜色更改为默认值

How can I change the border color of this bottom line when UITextField call textFieldDidBeginEditing() and then change back color to default when it calls textFieldDidEndEditing()

我正在使用 UITextField

推荐答案

Take Action methods of textfield as editing did begin and editor did end

Take Action methods of textfield as editing did begin and editing did end

这个没问题,我已经测试过了

This will work fine, I have tested it

   @IBAction func didbegin(_ sender: AnyObject) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.green.cgColor
        border.frame = CGRect(x: 0, y: txtfield.frame.size.height - width, width:  txtfield.frame.size.width, height: txtfield.frame.size.height)

        border.borderWidth = width
        txtfield.layer.addSublayer(border)
        txtfield.layer.masksToBounds = true
    }


    @IBAction func endediting(_ sender: AnyObject) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.black.cgColor
        border.frame = CGRect(x: 0, y: txtfield.frame.size.height - width, width:  txtfield.frame.size.width, height: txtfield.frame.size.height)

        border.borderWidth = width
        txtfield.layer.addSublayer(border)
        txtfield.layer.masksToBounds = true

    }

希望这对你有帮助.

这篇关于更改 uitextfield 的边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆