공부/iOS

[UIKit]키보드 위에 수정 제안을 비활성화하기

안토니1 2023. 12. 13. 19:27

오류 발생 원인

 

자동으로 맞춤법 교정해주거나 사용자의 입력을 예측해서 입력을 돕는 기능은 유용하지만 때에 따라 필요하지 않는 경우도 존재합니다.

아래의 코드를 textfield에 추가해주면 아래와 같이 제거할 수 있습니다.

textfield.autocorrectionType = .no
textfield.spellCheckingType = .no

 

그러면 다음과 같이 UIKit에서 맞춤법 검사를 무시할 수 있습니다.

 

 

코드 사용 예시

import UIKit

class PlaneTextField: UITextField {

    init(placeholder: String) {
        super.init(frame: .zero)
        
        self.placeholder = placeholder
        self.autocapitalizationType = .none
        self.autocorrectionType = .no
        self.spellCheckingType = .no
        self.borderStyle = .roundedRect
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        print("deinitted textfield \(self.placeholder ?? "") \(self.text ?? "")")
    }
}