Software Keyboard
software keyboard
➥Return key
Return Keyチェックの自動有効化->Return Keyの無効化->入力の再有効化
import UIKit
class ReturnKeyViewController: UIViewController {
@IBOutlet weak var firstInputField: UITextField!
@IBOutlet weak var secondInputField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
secondInputField.enablesReturnKeyAutomatically = true
}
}
// 리턴키를 탭할때마다 호출됨
extension ReturnKeyViewController: UITextFieldDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// 두 텍스트 필드가 동일한 뷰 컨트롤러를 델리게이트를 지정했기때문에 첫번째 파라미터를 기준으로 분기해야함 switch
switch textField {
case firstInputField:
secondInputField.becomeFirstResponder()
case secondInputField:
guard let keyword = secondInputField.text else { return true }
guard let url = URL(string: "http://www.google.com/m/search?q=\(keyword)") else {
return true
}
// 위에 url를 모바일 사파리로 전달해서 검색하는 기능 구현
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
default:
break
}
return true // 보통 true를 리턴
}
}
Keyboard Notification
import UIKit
class KeyboardNotificationViewController: UIViewController {
@IBOutlet var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillShow, object: nil, queue: OperationQueue.main) { (noti) in
guard let userInfo = noti.userInfo else { return }
guard let bounds = userInfo[UIKeyboardFrameEndUserInfoKey] as? CGRect else { return }
var inset = self.textView.textContainerInset
// inset.bottom = 350
inset.bottom = bounds.height
self.textView.textContainerInset = inset
self.textView.scrollIndicatorInsets = inset
}
NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardWillHide, object: nil, queue: OperationQueue.main) { (noti) in
var inset = self.textView.textContainerInset
inset.bottom = 8
self.textView.textContainerInset = inset
self.textView.scrollIndicatorInsets = inset
}
}
}
Reference
この問題について(Software Keyboard), 我々は、より多くの情報をここで見つけました https://velog.io/@din0121/Software-Keyboardテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol