簡體   English   中英

更改textbegin上文本字段的底視圖顏色?

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

我在用戶界面的下方創建了UI,並在每個文本字段下方添加了UIView並更改了一些顏色視圖。 在此處輸入圖片說明

為了更改視圖的某些初始顏色,我為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)

    }
    }

現在我想當用戶點擊或聚焦時,將文本框的文本框顏色更改為全色。為此,我在下面的代碼中使用了該方法。

 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)

        }


    }

現在它會更改視圖的顏色,但不會從一開始就更改。它會從已有顏色的地方更改UIView的顏色。

所以請建議我該怎么辦?

我也想當用戶移動到密碼時,先前文本字段視圖的顏色應與最初的顏色相同。

我認為背景色和填充色有一些沖突

僅嘗試通過這種方式使用填充顏色將其添加到視圖的子類中

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)

}

從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)

    }


}

希望對您有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM