अपने ViewController
पर अपनी बाधा के लिए आउटलेट बनाएं। मैं इसे अब के लिए yourConstraint
के रूप में संदर्भित करूंगा। फिर कुंजीपटल दिखाए जाने पर और जब इसे खारिज किया जा रहा है, तो यह पता लगाने के लिए कोड जोड़ें। वहां आप बाधा के constant
को तदनुसार बदलते हैं। यह आपको बाधाओं का उपयोग करने की अनुमति देता है।
viewWillAppear
में:
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil) // <=== Replace YourViewController with name of your view controller
NotificationCenter.default.addObserver(self, selector: #selector(YourViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil) // <=== Replace YourViewController with name of your view controller
viewWillDisappear
में:
NotificationCenter.default.removeObserver(self)
में अपने UIViewController
@objc private func keyboardWillShow(notification: Notification) {
guard let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
yourConstraint.isActive = false // <===
view.layoutIfNeeded()
return
}
let duration: TimeInterval = ((notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue) ?? 0.4
yourConstraint.constant = newValue // <===
UIView.animate(withDuration: duration) {
self.view.layoutIfNeeded()
}
}
@objc private func keyboardWillHide(notification: Notification) {
let duration: TimeInterval = ((notification.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue) ?? 0.4
yourConstraint.constant = oldValue
UIView.animate(withDuration: duration) {
self.view.layoutIfNeeded()
}
}
UIView.animate
आवश्यक नहीं (ब्लॉक के अंदर है) है, लेकिन यह संक्रमण अच्छा लग रहा है।
जैसे कुछ के साथ डिवाइस के बीच अंतर कर सकते हैं धन्यवाद ब्रो। मैं केवल इस तर्क को लागू करने जा रहा था लेकिन आपने ठीक से जवाब दिया। क्या यह थोड़ा अजीब नहीं है कि हमें ओएस की जांच करनी है? 'UIKeyboardFrameEndUserInfoKey 'को सुरक्षित क्षेत्र इंसेट काटने में ऊंचाई वापस करनी चाहिए। – Amit
@ प्रवेश करें यदि आपको आईओएस 11 से पहले 'सुरक्षित एरियाइन्ससेट' मौजूद नहीं है, तो आपको अपना डिप्लॉयमेंट लक्ष्य <11.0 है, तो आपको एक संस्करण जांच करना होगा। – nathan