๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“ฑ Mobile/iOS

[iOS - UIKit] TextField ์ž…๋ ฅ์— ๋”ฐ๋ผ Button ํ™œ์„ฑํ™” ์œ ๋ฌด

 

TextField ์ž…๋ ฅ ์‹œ์ž‘ ์‹œ Button ์ƒํƒœ ๋ณ€๊ฒฝ

 

Button์˜ ํ™œ์„ฑํ™”์™€ UI๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ํ•จ์ˆ˜

 

Code

// textField ์ƒํƒœ์— ๋”ฐ๋ผ LoginButton ์ƒํƒœ ํ™œ์„ฑํ™” ์œ 
func textFieldDidChangeSelection(_ textField: UITextField) {
    if (loginView.idTextField.text?.count ?? 0 < 1) || (loginView.passwordTextField.text?.count ?? 0 < 1) {
        updateLoginButtonState(isEnabled: false, backgroundColor: .tvingBlack, borderColor: .tvingGray4)
    } else {
        updateLoginButtonState(isEnabled: true, backgroundColor: .tvingRed, borderColor: .clear)
    }
}

func updateLoginButtonState(isEnabled: Bool, backgroundColor: UIColor, borderColor: UIColor) {
    loginView.loginButton.isEnabled = isEnabled
    loginView.loginButton.backgroundColor = backgroundColor
    loginView.loginButton.layer.borderColor = borderColor.cgColor
}

 

 


TextField ์ž…๋ ฅ ์‹œ์ž‘ํ• ๋•Œ ํ…Œ๋‘๋ฆฌ ์ ์šฉ ๋ฐ ํ•ด์ œ 

 

 

Code

// textField๊ฐ€ ํ„ฐ์น˜๊ฐ€ ๋˜๋ฉด ํ…Œ๋‘๋ฆฌ ์„ค์ •
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    textField.layer.borderColor = UIColor.white.cgColor
    textField.layer.borderWidth = 1
    return true
}

// textField ํ„ฐ์น˜ ํ•ด์ œ ์‹œ ํ…Œ๋‘๋ฆฌ ํ•ด์ œ
func textFieldDidEndEditing(_ textField: UITextField) {
    textField.layer.borderColor = .none
    textField.layer.borderWidth = 0
}

 

 

 

 

728x90