포커스에 따라 텍스트필드 배경색 변경

class 파일 생성

import UIKit

//--------------------------------------------------
// TextField 입력박스 배경색 변경 확장
//--------------------------------------------------
@IBDesignable
class UITextFieldExtension: UITextField, UITextFieldDelegate {

    @IBInspectable var focusBackgroundColor: UIColor?

    private var defaultBackgroundColor: UIColor?

    func textFieldDidBeginEditing(_ textField: UITextField) {
        if focusBackgroundColor == nil {
            print("오류 - focusBackgroundCOlor 설정")
            return
        }
        defaultBackgroundColor = self.backgroundColor
        self.backgroundColor = focusBackgroundColor
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        self.backgroundColor = defaultBackgroundColor
    }
}

사용할 페이지에서

Custom class에 위의 class명을 선택해 준다

Text Field Extention이 생기면 백그라운드 칼라를 선택해 준다.

@IBOutlet weak var whereTextfield: UITextFieldExtension!
@IBOutlet weak var userTextfield: UITextFieldExtension!

override func viewDidLoad() {
        super.viewDidLoad()

        // 텍스트필드 커서에 따라 배경색 변경 처리
        whereTextfield.delegate = whereTextfield
        userTextfield.delegate = userTextfield

results matching ""

    No results matching ""