私のアプリケーションでは、ビューの上部と下部にヘッダーとバナーの画像が含まれており、15 個以上の UITextfields を持つスクロールビューも含まれています。
私はanyW、anyH自動レイアウトを使用し、親ビューの高さは高さ2500のフリーフォームとして設定されています。
以下のコードを使用して、画面上のキーボードを非表示および表示しました。正常に動作し、以下のコードを使用してアクティブなテキスト フィールドをキーボードの上に移動しました。
ここで私が直面している問題は、ビューの自由形式の高さに基づいて、現在アクティブなテキスト フィールドが間違った x、y 位置を取ることです。
以下は Firstname テキストフィールドの x、y 位置ですが、実際には画面の上部に配置され、フレームは a(100 250; 270 28); です。
UITextField: 0x1918a820; フレーム = (14.5 1405; 270 28); テキスト = ''; clipsToBounds = YES; 不透明 = いいえ; 自動サイズ変更 = RM+BM; ジェスチャレコグナイザー = NSArray: 0x191a5900; レイヤー = CALayer: 0x19189f40
FirstName フィールドにテキストを入力し始めると、firstname フィールド フレームの位置が (14.5 1405; 270 28) になり、2 番目の画面のように上にスクロールします。
現在、Y 位置は 1415 です。アクティブなテキスト フィールドの x、y 位置が間違っているのはなぜですか?
アクティブなテキスト フィールドのフレーム位置が間違っているのはなぜですか? この問題を解決するにはどうすればよいですか?
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Registering for keyboard events");
// Register for the events
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];
// Setup content size
_scrollViewAddContact.contentSize = CGSizeMake(self.view.frame.size.width,_scrollViewAddContact.frame.size.height);
//Initially the keyboard is hidden
keyboardVisible = NO;
}
-(void) viewWillDisappear:(BOOL)animated {
NSLog (@"Unregister for keyboard events");
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(BOOL) textFieldShouldBeginEditing:(UITextField*)textField {
activeField = textField;
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void) keyboardDidShow: (NSNotification *)notif {
NSLog(@"Keyboard is visible");
// If keyboard is visible, return
if (keyboardVisible) {
NSLog(@"Keyboard is already visible. Ignore notification.");
return;
}
// Get the size of the keyboard.
NSDictionary* info = [notif userInfo];
NSValue* aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [aValue CGRectValue].size;
// Save the current location so we can restore
// when keyboard is dismissed
offset = _scrollViewAddContact.contentOffset;
// Resize the scroll view to make room for the keyboard
CGRect viewFrame = _scrollViewAddContact.frame;
viewFrame.size.height -= keyboardSize.height;
_scrollViewAddContact.frame = viewFrame;
CGRect textFieldRect = [activeField frame];
textFieldRect.origin.y += 10;
[_scrollViewAddContact scrollRectToVisible:textFieldRect animated:YES];
NSLog(@"ao fim");
// Keyboard is now visible
keyboardVisible = YES;
}
-(void) keyboardDidHide: (NSNotification *)notif {
// Is the keyboard already shown
if (!keyboardVisible) {
NSLog(@"Keyboard is already hidden. Ignore notification.");
return;
}
// Reset the frame scroll view to its original value
_scrollViewAddContact.frame = CGRectMake(_scrollViewAddContact.frame.origin.x, _scrollViewAddContact.frame.origin.y, self.view.frame.size.width, _scrollViewAddContact.frame.size.height);
// Reset the scrollview to previous location
_scrollViewAddContact.contentOffset = offset;
// Keyboard is no longer visible
keyboardVisible = NO;
}