मेरे पास एक ऐसा दृश्य है जिसमें दृश्य के शीर्ष से दृश्य फ़ील्ड दृश्य के नीचे हैं। मुझे नीचे फ़ील्ड को संपादित करते समय स्क्रॉल करने की आवश्यकता थी ताकि फ़ील्ड दिखाई दे सकें, लेकिन यह सही तरीके से काम नहीं कर रहा है।सक्रिय टेक्स्ट फ़ील्ड पर कीबोर्ड स्क्रॉल - दृश्य से बाहर स्क्रॉलिंग?
Apple docs के बाद, मैं अपने कार्यक्रम (लिस्टिंग 4-1, 4-2) में है कि कोड के सभी रखा, और मेरे हेडर फाइल को scrollView
और activeField
दुकानों जोड़ा गया है और उन्हें आईबी से जुड़ा हुआ।
समस्या जैसे ही मैं एक टेक्स्ट फ़ील्ड में क्लिक करता हूं, जब तक मैं कीबोर्ड को खारिज नहीं करता तब तक सभी टेक्स्ट फ़ील्ड दृश्य से बाहर नहीं जाते हैं। वे बहुत दूर स्क्रॉल करते हैं (फिर से, जहां तक कोई भी फ़ील्ड दिखाई नहीं दे रहा है)।
क्या किसी को पता है कि समस्या क्या हो सकती है?
मैं यहां ऐप्पल डॉक्स से कोड डाल रहा हूं ताकि आप देख सकें कि मैं किस कोड को क्लिक करने के बिना उपयोग कर रहा हूं।
//my .h
IBOutlet UIScrollView *scrollView;
IBOutlet UITextField *activeField;
//.m
// Call this method somewhere in your view controller setup code.
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
मैं क्योंकि यह संदिग्ध है, इसलिए इसमें पुन: पेश करने एप्पल से अनुमति है अपनी पोस्ट के बाहर एप्पल कोड की कॉपी-पेस्ट संपादित -
यहाँ मेरी दृष्टिकोण (थोड़ा सेब की तरह संशोधित) है। मैंने एक मॉडरेटर को देखने के लिए भी ध्वजांकित किया - मुझे यकीन नहीं है कि आधिकारिक नीति क्या है या होनी चाहिए। –
@ जोश: ["उचित उपयोग,"] (http://en.wikipedia.org/wiki/Fair_use) उद्देश्यों के लिए कोड की उस छोटी राशि शायद ठीक है। –
@Robert: ठीक है, धन्यवाद। जेम्स, मुझे लगता है कि इसका मतलब है कि अगर आप इसे चुनते हैं तो आपको इसे वापस रखना चाहिए। भाई के लिए खेद है। –