@@ -12,7 +12,7 @@ import QuartzCore
1212/// Simple UITextfield Subclass with state
1313@IBDesignable
1414open class UnderLineTextField : UITextField {
15-
15+ private var isLayoutCalled = false
1616 //============
1717 // MARK: - inits
1818 //============
@@ -25,6 +25,7 @@ open class UnderLineTextField: UITextField {
2525 initilize ( )
2626 }
2727 private func initilize( ) {
28+ neededConstraint. append ( heightConstraint)
2829 borderStyle = . none
2930 placeholder = super. placeholder
3031 tintColor = super. tintColor
@@ -70,6 +71,7 @@ open class UnderLineTextField: UITextField {
7071 guard contentStatus != oldValue else {
7172 return
7273 }
74+ layoutIfNeeded ( )
7375 if contentStatus == . empty {
7476 setPlaceholderPlace ( isUp: false , isAnimated: true )
7577 } else {
@@ -86,8 +88,6 @@ open class UnderLineTextField: UITextField {
8688 open var animationDuration : Double = 0.3
8789 /// constraints that will be activated upon initilization
8890 private var neededConstraint = [ NSLayoutConstraint] ( )
89- /// set height of control
90- private var heightConstraint : NSLayoutConstraint !
9191 /// should show error label
9292 private var errorLabelColor : UIColor {
9393 switch self . status {
@@ -147,6 +147,16 @@ open class UnderLineTextField: UITextField {
147147 //=====================
148148 // MARK: Lazy Loadings
149149 //=====================
150+ /// set height of control
151+ private lazy var heightConstraint : NSLayoutConstraint = {
152+ return NSLayoutConstraint ( item: self ,
153+ attribute: . height,
154+ relatedBy: . equal,
155+ toItem: nil ,
156+ attribute: . notAnAttribute,
157+ multiplier: 1 ,
158+ constant: 0 )
159+ } ( )
150160 private lazy var clearButton : UIButton = {
151161 let button = UIButton ( type: . custom)
152162 let bundle = Bundle . init ( for: UnderLineTextField . self)
@@ -441,8 +451,8 @@ extension UnderLineTextField {
441451 override open func layoutSubviews( ) {
442452 super. layoutSubviews ( )
443453 lineLayer. path = createLinePath ( ) . cgPath
444- setNeedsDisplay ( )
445454 changeSementics ( )
455+ isLayoutCalled = true
446456 }
447457
448458 override open func becomeFirstResponder( ) -> Bool {
@@ -498,17 +508,8 @@ extension UnderLineTextField {
498508 private func adjustHeight( ) {
499509 let heightLine = ( font? . pointSize ?? 0 ) + 8
500510 let height = heightLine + heightLine * 1.6 + 14
501- if heightConstraint == nil {
502- heightConstraint = NSLayoutConstraint ( item: self ,
503- attribute: . height,
504- relatedBy: . equal,
505- toItem: nil ,
506- attribute: . notAnAttribute,
507- multiplier: 1 ,
508- constant: height)
509- heightConstraint. isActive = true
510- }
511511 heightConstraint. constant = height
512+ guard isLayoutCalled else { return }
512513 layoutIfNeeded ( )
513514 }
514515
0 commit comments