讓我們假設我們有一個應用程序的登錄屏幕。登錄屏幕執行UITableViewController
,其中包含UITableView
。 UITableView
有1個部分,該部分包含兩個單元格(請參閱下面的故事板)。如何在編輯第一個UITextField時將UITableView滾動到第二個UITextField?
當前行爲
當用戶開始編輯電子郵件字段 - 與佔位符輸入你的電子郵件 - 鍵盤重疊密碼字段 - 與佔位符輸入密碼。
期望的行爲:
當用戶開始編輯電子郵件字段,鍵盤應該重疊既不場和鍵盤應該坐下低於這兩個字段。
嘗試性解決方案
我試圖用我的LoginFormController : UITableViewController
以下迫使UITableView
滾動到合適的位置,但沒有成功:
-(BOOL) textFieldSHouldBeginEditing:(RCTextField *)textField
{
[[self.view viewWithTag:textField.tag+1] becomeFirstResponder];
// get the scroll index path of the last row in the first section
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:1 inSection:0];
// scroll the view there
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
return YES;
}
我LoginFormController : UITableViewController
接口:
//
// LoginFormController.h
// Zealoushacker
//
// Created by Alex Notov on 7/31/13.
// Copyright (c) 2013 Zealoushacker, Inc. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface LoginFormController : UITableViewController <UITextFieldDelegate>
@end
LoginFormController
當然有提及tableView
。
以下解決方案中的任何一種方法都不能解決內容滾動的其他問題,而不是解決問題。在這個線程中的解決方案似乎看起來比下面的答案中的任何東西都更優美,但也不能按需要工作:http://stackoverflow.com/questions/16547549/get-position-of-cell-or-text在這個單元 – zealoushacker