वह ब्लॉग पोस्ट बहुत अच्छा है, लेकिन मैं इसमें कुछ सुधार सुझा सकता हूं। सबसे पहले, आप फ्रेम परिवर्तनों का निरीक्षण करने के लिए पंजीकरण कर सकते हैं, इसलिए आपको शो और छुपाएं नोटिफिकेशन दोनों का निरीक्षण करने के लिए पंजीकरण करने की आवश्यकता नहीं है। दूसरा, आपको निर्देशांक देखने के लिए स्क्रीन से कुंजीपटल के लिए सीजीआरईटी को परिवर्तित करना चाहिए। अंत में, आप कुंजीपटल के लिए आईओएस द्वारा उपयोग किए जाने वाले सटीक एनीमेशन वक्र की प्रतिलिपि बना सकते हैं, इसलिए कीबोर्ड और ट्रैकिंग दृश्य सिंक्रनाइज़ेशन में जाते हैं।
यह सबको एक साथ रखें, तो आपको निम्न मिलता है:
@interface MyViewController()
// This IBOutlet holds a reference to the bottom vertical spacer
// constraint that positions the "tracking view",i.e., the view that
// we want to track the vertical motion of the keyboard
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomVerticalSpacerConstraint;
@end
@implementation MyViewController
-(void)viewDidLoad
{
[super viewDidLoad];
// register for notifications about the keyboard changing frame
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification
object:self.view.window];
}
-(void)keyboardWillChangeFrame:(NSNotification*)notification
{
NSDictionary * userInfo = notification.userInfo;
UIViewAnimationCurve animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// convert the keyboard's CGRect from screen coords to view coords
CGRect kbEndFrame = [self.view convertRect:[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]
fromView:self.view.window];
CGRect kbBeginFrame = [self.view convertRect:[[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue]
fromView:self.view.window];
CGFloat deltaKeyBoardOrigin = kbEndFrame.origin.y - kbBeginFrame.origin.y;
// update the constant factor of the constraint governing your tracking view
self.bottomVerticalSpacerConstraint.constant -= deltaKeyBoardOrigin;
// tell the constraint solver it needs to re-solve other constraints.
[self.view setNeedsUpdateConstraints];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
// within this animation block, force the layout engine to apply
// the new layout changes immediately, so that we
// animate to that new layout. We need to use old-style
// UIView animations to pass the curve type.
[self.view layoutIfNeeded];
[UIView commitAnimations];
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillChangeFrameNotification
object:nil];
}
@end
यह काम करेंगे, जब तक आप ओरिएंटेशन बदलने नहीं है, जबकि कुंजीपटल निर्भर है।
How to mimic Keyboard animation on iOS 7 to add "Done" button to numeric keyboard? पर यह एक उत्तर था कि कुंजीपटल एनीमेशन वक्र की नकल कैसे करें।
इन सभी अधिसूचना-आधारित समाधानों के संबंध में सावधान रहना एक आखिरी बात है: यदि आपके ऐप में कुछ अन्य स्क्रीन कीबोर्ड का भी उपयोग करती है तो वे अप्रत्याशित प्रभाव उत्पन्न कर सकते हैं, क्योंकि आपका व्यू कंट्रोलर तब भी अधिसूचनाएं प्राप्त करेगा जब तक कि आपका व्यू कंट्रोलर तब तक अधिसूचनाएं प्राप्त करेगा को हटाया नहीं गया है, भले ही इसका विचार उतार दिया गया हो। इसके लिए एक उपाय अधिसूचना हैंडलर में एक सशर्त डालना है ताकि यह सुनिश्चित किया जा सके कि दृश्य नियंत्रक स्क्रीन पर ही काम करता है।
लिंक से उदाहरण लें तो मैं की गणना 'keyboardHeight' के लिए बाहर कोड refactor हैं और' willRotateToInterfaceOrientation जोड़ें: 'और/या' willAnimateRotationToInterfaceOrientation: 'कीबोर्ड ऊंचाई बाधा अद्यतन करने के लिए के रूप में उपयुक्त कहता है। –
'UIKeyboardWillShowNotification' अधिसूचना तब भी पोस्ट की जाती है जब कीबोर्ड का आकार रोटेशन के कारण बदलता है, इसलिए आपको इसके बारे में चिंता करने की आवश्यकता नहीं है। रोचक लिंक – omz
+1 यह ऑटोलायआउट के साथ संपादन फ़ॉर्म को संभालने के लिए एक सुरुचिपूर्ण समाधान है। – memmons