0
키보드가있을 때 UITextField가 올라가도록 코드를 작성하려고합니다. 나는 스택 오버 플로우에서 발견 한 것을 기반으로 코드를 작성했지만 작동하지는 않습니다. 원래 코드는 objective-c로 작성되었지만이 코드에 익숙하지 않아 신속하게 코드를 작성하기로 결정했습니다.키보드가 나타나면 UITextField를 위로 움직입니다.
누구나 내가 뭘 할 수 있는지 알려줄 수 있습니까?
{
// moving text box up when tyoing
func registerForKeyboardNotifications()
{
//Adding notifies on keyboard appearing
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWasShown(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillBeHidden(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc func keyboardWasShown(notification: NSNotification)
{
//Need to calculate keyboard exact size due to Apple suggestions
self.scrollView.isScrollEnabled = true
let info : NSDictionary = notification.userInfo! as NSDictionary
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if (yourEmail) != nil
{
if (!aRect.contains(yourEmail!.frame.origin))
{
self.scrollView.scrollRectToVisible(yourEmail!.frame, animated: true)
}
}
}
@objc func keyboardWillBeHidden(notification: NSNotification)
{
//Once keyboard disappears, restore original positions
let info : NSDictionary = notification.userInfo! as NSDictionary as NSDictionary
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.view.endEditing(true)
self.scrollView.isScrollEnabled = false
}
func textFieldDidBeginEditing(_ textField: UITextField)
{
yourEmail = textField
}
func textFieldDidEndEditing(_ textField: UITextField)
{
yourEmail = nil
}
}
가능한 복제 [키보드가 존재하는 경우의 UITextField 이동 만들려면 ?] (https://stackoverflow.com/questions/1126726/how-to-make-a-uitextfield-move-up-when-keyboard-is-present) –
안녕하세요, 리 슈우, 코드를 신속하게 작성하려고합니다. .. 플러스 나는 코드를 썼다 그러나 텍스트 필드는 위로 움직이지 않는다 키보드 뒤쪽에 숨어있는 이유는 ...? –
텍스트 필드가 scrollview 안에 있습니까? –