2017-10-30 7 views
1

私はログイン画面のUIの下に作成しました。すべてのテキストフィールドの下にUIViewを追加しました。&は、いくつかのカラービューを変更しました。 enter image description heretextbeginのテキストフィールドの下側の色を変更しますか?

私は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) 

     } 


    } 

今では、ビューの色が変わりますがbegining.itから色がすでにあった場所からのUIViewの色を変更しません。

どうすればいいですか?

また、ユーザーがパスワードに移動したときに、以前のテキストフィールドビューの色が最初と同じである必要があります。

+0

[一部の色を変更する方法コントローラからの迅速な表示は?](https://stackoverflow.com/questions/47013997/how-to-change-color-of-some-part-of-view-from-controller-in-swift) – PGDev

+0

ドン同じ質問を複数回聞かないでください。 https://stackoverflow.com/questions/47013997/how-to-change-color-of-some-part-of-view-from-controller-in-swift/47014843#47014843 – PGDev

答えて

0

私は

は色のみ使用塗りつぶしこの方法を試してみてのUIViewControllerクラスからあなたのビュー

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) 

} 

のサブクラスに追加し、背景色を考え、色は、いくつかの競合を持って記入

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) 

    } 


} 

あなたに役立つことを願っています

+0

これも私にとっては役に立ちません – Techiee

+0

あなた同じ結果が得られていますか? –

+0

2番目のオプションは 'UIProgressView'を使用し、それに応じて進捗状況を設定する方がはるかに簡単ですか? –

関連する問題