2017-09-08 5 views
0

バリデーションメソッドを任意の数のテキストフィールドとUIColorを入力として受け入れたいと思っています。私はその特定の色のボトムラインをメソッド内のすべてのテキストフィールドに追加するつもりです。Swiftでバリデーションメソッドを作成する

私はSwiftでこのような方法を作ることができますか?

答えて

2

おそらくこのような何かをするだろう:

func applyColour(toTextfields textfields: UITextField..., colour: UIColor) { 
    for textfield in textfields { 
     // Apply the colour here. 
    } 
} 
+0

スウィフトドキュメントから: 可変長パラメータに渡された値が適切な型の配列として、関数の本体内で使用可能になります。たとえば、数値の名前とDouble型の可変パラメータは、関数の本体内で、[Double]型のnumbersという定数配列として使用できます。 https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html –

2

enter image description here

Common.addBottonLine(color: UIColor.white, userNameTextField, passwordTextField) 

メソッドの定義

class func addBottonLine(color: UIColor, _ txtFields: UITextField...) -> Void { 

    for textF in txtFields { 

     var bottomLine = CALayer() 

     bottomLine.frame = CGRect.init(origin: CGPoint.init(x: 0, y: textF.frame.height - 2) , size: CGSize.init(width: textF.frame.width, height: 2)) 

     bottomLine.backgroundColor = color.cgColor 
     textF.borderStyle = .none 
     textF.layer.addSublayer(bottomLine) 

    } 

} 
0

一番下の行を追加するには、この機能を使用します
func addBottomLabel(_ color: UIColor) { 
let lbl1 = UILabel() 
lbl1.backgroundColor = color 
addSubview(lbl1) 
addVisualConstraints(["H:|[label]|", "V:[label(1)]|"], forSubviews: ["label": lbl1]) 
} 

用途:

let textFields = [your textfield array] 
for textFields in textFields 
{ 
textFields.addBottomLabel(.yourcolor) 
} 
関連する問題