// 메인뷰 하단에 위치하기 위한 bottom constraint
@IBOutlet weak var textFieldLayoutBottomConstraint: NSLayoutConstraint!


// 키보드 이벤트 등록
NotificationCenter.default.addObserver(self, 
		selector: #selector( MyViewController.keyboardWillShow ),
    name: UIResponder.keyboardWillShowNotification,
    object: nil )

NotificationCenter.default.addObserver(
    self,
    selector: #selector( MyViewController.keyboardWillHide ),
    name: UIResponder.keyboardWillHideNotification,
    object: nil )    

// 키보드 이벤트 해제
NotificationCenter.default.removeObserver(
     self,
     name: UIResponder.keyboardWillShowNotification,
     object: nil)

NotificationCenter.default.removeObserver(
	    self,     
			name: UIResponder.keyboardWillHideNotification,
	    object: nil)

// 이벤트 핸들러
@objc func keyboardWillShow( notification: NSNotification ) {
    changeTextFieldBottomConstraint( notification: notification )
}

@objc func keyboardWillHide( notificaiton: NSNotification ) {
    changeTextFieldBottomConstraint( notification: notification )
}

// text filed bottom constraint 변경
func changeTextFieldBottomConstraint( notification:NSNotification ) {
    let userInfo = notification.userInfo!
    let keyboardAnimationDuration = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue    
		let keyboardFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue    
		let convertedFrame = self.view.convert( keyboardFrame from: self.view.window )
    let rawAnimationCurve = (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber).uintValue << 16
    let animationCurve = UIView.AnimationOptions( rawValue: UInt( rawAnimationCurve ))
        textFieldLayoutBottomConstraint.constant = self.view.bounds.maxY - keyboardFrame.minY
	    UIView.animate(
        withDuration: animationDuration,
        delat: 0.0,
        options: UIView.AnimationOptions(rawValue: UIView.AnimationOptions.beginFromCurrentState.rawValue|animationCurve.rawValue),
        animations: { 
			self.view.layoutIfNeeded()
		}, completion: nil)
}

// NSNotification userInfo 의 키보드 관련 내용
public class let keyboardWillShowNotification: NSNotification.Name
public class let keyboardDidShowNotification: NSNotification.Name
public class let keyboardWillHideNotification: NSNotification.Name
public class let keyboardDidHideNotification: NSNotification.Name

public class let keyboardFrameBeginUserInfoKey: String // NSValue of CGRect
public class let keyboardFrameEndUserInfoKey: String // NSValue of CGRect
public class let keyboardAnimationDurationUserInfoKey: String // NSNumber of double
public class let keyboardAnimationCurveUserInfoKey: String // NSNumber of NSUInteger (UIViewAnimationCurve)
public class let keyboardIsLocalUserInfoKey: String // NSNumber of BOOL

// Like the standard keyboard notifications above, these additional notifications include
// a nil object and begin/end frames of the keyboard in screen coordinates in the userInfo dictionary.
public class let keyboardWillChangeFrameNotification: NSNotification.Name
public class let keyboardDidChangeFrameNotification: NSNotification.Name

+ Recent posts