简体   繁体   English

更改textbegin上文本字段的底视图颜色?

[英]Change bottom view color of textfield on textbegin?

I have created below UI for a login screen.I have added the UIView below the every text field & changed some color view. 我在用户界面的下方创建了UI,并在每个文本字段下方添加了UIView并更改了一些颜色视图。 在此处输入图片说明

To change view's some initial color i have created a custom class for UIView and added below code in it 为了更改视图的某些初始颜色,我为UIView创建了一个自定义类,并在其中添加了以下代码

import Foundation

class BottomView:UIView {

    override func draw(_ rect: CGRect) {

        let topRect = CGRect(x:0, y:0, width : 20, height: rect.size.height);
        // Fill the rectangle with grey
        UIColor.darkGray.setFill()
        UIRectFill(topRect)

    }
    }

Now i want when user tap or focus a textfield color of textfield should be changed as full color.To do that i used below code. 现在我想当用户点击或聚焦时,将文本框的文本框颜色更改为全色。为此,我在下面的代码中使用了该方法。

 func textFieldDidBeginEditing(_ textField: UITextField) {

        if textField == textFieldFirstName {

            UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
                self.bottomViewEmail.backgroundColor = UIColor.green
            }, completion:nil)

        }


    }

Now it changes the color of view but not from begining.it changes color of UIView from where there was already a color. 现在它会更改视图的颜色,但不会从一开始就更改。它会从已有颜色的地方更改UIView的颜色。

So please suggest me how can i do ? 所以请建议我该怎么办?

Also i want when user move to password the color of previous textfield view should be same as it was initially. 我也想当用户移动到密码时,先前文本字段视图的颜色应与最初的颜色相同。

I think background color and Fill Color has some conflicts 我认为背景色和填充色有一些冲突

Try This way only Use fill color add it to sub class of your View 仅尝试通过这种方式使用填充颜色将其添加到视图的子类中

func fillColor(with color:UIColor,width:CGFloat) {

    let topRect = CGRect(x:0, y:0, width : width, height: self.bounds.height);

    color.setFill()
    UIRectFill(topRect)

}

override func draw(_ rect: CGRect) {

    self.fillColor(with: .gray, width: 20)

}

From UIViewController class 从UIViewController类

func textFieldDidBeginEditing(_ textField: UITextField) {

    if textField == textFieldFirstName {

        UIView.animate(withDuration: 1.0, delay: 0.0, options:[.repeat, .autoreverse], animations: {
            self.bottomViewEmail.fillColor(with: .green, width: self.bottomViewEmail.bounds.width)

    }


}

Hope it is helpful to you 希望对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM