I have a UITableView
that contains UITextField
in each cell
When I click UITextField
in the cell, the keyboard will show and cover my cell. Therefore, I move my cell to the top by using.
我有一个UITableView,在每个单元格中包含UITextField当我点击单元格中的UITextField时,键盘会显示并覆盖我的单元格。因此,我将我的单元格移动到顶部。
- (void)keyboardWasShown:(NSNotification*)aNotification
{
CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);
[self.tableView setContentOffset:scrollPoint animated:YES];
}
If I use single click on each cell, my application work fine.
However, I use double click on each cell (it mean I tap on it 2 times very quickly), my cell will stop scroll to the top.
如果我在每个单元格上单击一次,我的应用程序就可以正常工作了。但是,我使用双击每个单元格(这意味着我快速点击它2次),我的单元格将停止滚动到顶部。
7 个解决方案
#1
3
In this line
在这条线
CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);
can be 2 errors.
可以2错误。
Firstly, you have to make sure that inputView that you have captured is the view you want. Secondly, you should convert fetched point to correct view using appropriate method.
首先,您必须确保已捕获的inputView是您想要的视图。其次,您应该使用适当的方法将获取点转换为正确的视图。
See reference: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW52
见参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/ / / apple_ref / doc / uid / TP40006816-CH3-SW52
For further investigation need more context.
进一步的调查需要更多的背景。
#2
2
TPKeyboardAvoiding seems to be a solution.
tpkeyboardavoidance似乎是一种解决方法。
#3
2
Are you sure that the scrollPoint is correct?
你确定滚动点是正确的吗?
I just created a sample project with a fixed offset and everything works perfectly fine. The table view scrolls upwards after single and double tap on the UITextField
of a certain UITableViewCell
.
我刚刚创建了一个带有固定偏移量的示例项目,一切都很好。在一个特定的UITableViewCell的UITextField上单击一次和两次之后,表格视图向上滚动。
- (void)keyboardDidShow:(NSNotification *)notification {
CGPoint point = CGPointMake(0.0, 30.0);
[self.tableView setContentOffset:point animated:YES];
}
#4
1
rather then doing code for manually adjusting your tableview frame , Let me give you a suggestion, you can use IQKeyboardManager whose pod is available in cocoapods and it will manage of when clicking on cell's textfield , tableview will automatically scroll.
我给你一个建议,你可以使用IQKeyboardManager,它的pod可以在cocoapods中使用,当点击cell的textfield时,它会自动滚动。
#5
1
Try to use third party library IQKeyboardManager it will help you to solve this issue.
尝试使用第三方图书馆IQKeyboardManager它将帮助您解决这个问题。
https://github.com/hackiftekhar/IQKeyboardManager
https://github.com/hackiftekhar/IQKeyboardManager
Replace following code in your AppDelegate.m file and then you don't need to use scrollview method to handle keyboard :-
在AppDelegate中替换以下代码。m文件,然后您不需要使用scrollview方法来处理键盘:-
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[IQKeyboardManager sharedManager] setEnable:YES];
[self.window makeKeyAndVisible];
return YES;
}
#6
1
Have you created separate UITableViewCell class ?
您创建了单独的UITableViewCell类吗?
I believe as you scroll your UITableView your UI freezes or when you tab twice frequently your UITableView stops responding or taking time to respond.
我相信当你滚动你的UITableView你的UITableView会冻结,或者当你频繁标签两次时你的UITableView会停止响应或花时间去响应。
#7
0
Here is a working example. You use your scroll view's contentInset and scrollIndicatorInsets properties to avoid the keyboard when it appears. You should register for keyboard notifications. Use the info in the notifications to determine the size of the keyboard--you never know what that will be.
这里有一个工作示例。您可以使用滚动视图的contentInset和scrollIndicatorInsets属性来避免出现键盘。你应该注册键盘通知。使用通知中的信息来确定键盘的大小——你永远不知道那会是什么。
Using content inset is the correct way to handle this for scroll views. If you subsequently need to scroll your editing row into view, use UITableView.scrollToRowAtIndexPath(_:, atScrollPosition:, animated:)
对于滚动视图,使用content inset是正确的处理方法。如果随后需要将编辑行滚动到视图中,请使用UITableView。scrollToRowAtIndexPath(_:atScrollPosition:动画:)
Notice that the does the right thing when the user hides/shows the completions bar.
请注意,当用户隐藏/显示完成栏时,执行的操作是正确的。
import UIKit
import CoreGraphics
// our Cell class
class Cell : UITableViewCell
{
// cell reuse identifier for table view
static let Identifier = "Cell"
// the object the cell represents/displays. Could be anything you like
var value:AnyObject? {
didSet {
// update our text field when our cell value is set.
self.textField.text = value as? String
}
}
// use a text field to display our contents, since that allows editing and showing the keyboard
lazy var textField:UITextField = {
let textField = UITextField()
self.contentView.addSubview( textField )
return textField
}()
override func layoutSubviews() {
super.layoutSubviews()
self.textField.frame = contentView.bounds.insetBy(dx: 20, dy: 4 )
}
}
// table view data source class
class DataSource : NSObject, UITableViewDataSource
{
var numberOfRows:Int { return items.count }
let items = [ "Seoul", "São Paulo", "Bombay", "Jakarta", "Karachi", "Moskva", "Istanbul", "Mexico", "Shanghai", "Tokyo", "New" York, "Bangkok", "Beijing", "Delhi", "London", "*", "Cairo", "Tehran", "Bogota", "Bandung", "Tianjin", "Lima", "Rio de Janeiro", "Lahore", "Bogor", "Santiago", "St Petersburg", "Shenyang", "Calcutta", "Wuhan" ]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numberOfRows
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier( Cell.Identifier ) as? Cell ?? Cell()
cell.value = items[ indexPath.row ]
return cell
}
}
class ViewController : UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// register for notifications when the keyboard appears:
NSNotificationCenter.defaultCenter().addObserver( self, selector: "keyboardWillShow:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}
override func viewDidLayoutSubviews() {
tableView.frame = view.bounds
}
lazy var tableView:UITableView = {
let tableView = UITableView()
self.view.addSubview( tableView )
tableView.dataSource = self.dataSource
tableView.delegate = self
return tableView
}()
lazy var dataSource : DataSource = DataSource()
// Handle keyboard frame changes here.
// Use the CGRect stored in the notification to determine what part of the screen the keyboard will cover.
// Adjust our table view's contentInset and scrollIndicatorInsets properties so that the table view content avoids the part of the screen covered by the keyboard
@objc func keyboardWillShow( note:NSNotification )
{
// read the CGRect from the notification (if any)
if let newFrame = (note.userInfo?[ UIKeyboardFrameEndUserInfoKey ] as? NSValue)?.CGRectValue() {
let insets = UIEdgeInsetsMake( 0, 0, newFrame.height, 0 )
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
}
}
}
// need to conform to UITableViewDelegate protocol since we are the table view's delegate
extension ViewController : UITableViewDelegate
{
}
// App set up stuff here:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var window:UIWindow? = UIWindow()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
}
#1
3
In this line
在这条线
CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);
CGPoint scrollPoint = CGPointMake(0, self.activeInputView.frame.origin.y);
can be 2 errors.
可以2错误。
Firstly, you have to make sure that inputView that you have captured is the view you want. Secondly, you should convert fetched point to correct view using appropriate method.
首先,您必须确保已捕获的inputView是您想要的视图。其次,您应该使用适当的方法将获取点转换为正确的视图。
See reference: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/#//apple_ref/doc/uid/TP40006816-CH3-SW52
见参考:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/ / / apple_ref / doc / uid / TP40006816-CH3-SW52
For further investigation need more context.
进一步的调查需要更多的背景。
#2
2
TPKeyboardAvoiding seems to be a solution.
tpkeyboardavoidance似乎是一种解决方法。
#3
2
Are you sure that the scrollPoint is correct?
你确定滚动点是正确的吗?
I just created a sample project with a fixed offset and everything works perfectly fine. The table view scrolls upwards after single and double tap on the UITextField
of a certain UITableViewCell
.
我刚刚创建了一个带有固定偏移量的示例项目,一切都很好。在一个特定的UITableViewCell的UITextField上单击一次和两次之后,表格视图向上滚动。
- (void)keyboardDidShow:(NSNotification *)notification {
CGPoint point = CGPointMake(0.0, 30.0);
[self.tableView setContentOffset:point animated:YES];
}
#4
1
rather then doing code for manually adjusting your tableview frame , Let me give you a suggestion, you can use IQKeyboardManager whose pod is available in cocoapods and it will manage of when clicking on cell's textfield , tableview will automatically scroll.
我给你一个建议,你可以使用IQKeyboardManager,它的pod可以在cocoapods中使用,当点击cell的textfield时,它会自动滚动。
#5
1
Try to use third party library IQKeyboardManager it will help you to solve this issue.
尝试使用第三方图书馆IQKeyboardManager它将帮助您解决这个问题。
https://github.com/hackiftekhar/IQKeyboardManager
https://github.com/hackiftekhar/IQKeyboardManager
Replace following code in your AppDelegate.m file and then you don't need to use scrollview method to handle keyboard :-
在AppDelegate中替换以下代码。m文件,然后您不需要使用scrollview方法来处理键盘:-
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[IQKeyboardManager sharedManager] setEnable:YES];
[self.window makeKeyAndVisible];
return YES;
}
#6
1
Have you created separate UITableViewCell class ?
您创建了单独的UITableViewCell类吗?
I believe as you scroll your UITableView your UI freezes or when you tab twice frequently your UITableView stops responding or taking time to respond.
我相信当你滚动你的UITableView你的UITableView会冻结,或者当你频繁标签两次时你的UITableView会停止响应或花时间去响应。
#7
0
Here is a working example. You use your scroll view's contentInset and scrollIndicatorInsets properties to avoid the keyboard when it appears. You should register for keyboard notifications. Use the info in the notifications to determine the size of the keyboard--you never know what that will be.
这里有一个工作示例。您可以使用滚动视图的contentInset和scrollIndicatorInsets属性来避免出现键盘。你应该注册键盘通知。使用通知中的信息来确定键盘的大小——你永远不知道那会是什么。
Using content inset is the correct way to handle this for scroll views. If you subsequently need to scroll your editing row into view, use UITableView.scrollToRowAtIndexPath(_:, atScrollPosition:, animated:)
对于滚动视图,使用content inset是正确的处理方法。如果随后需要将编辑行滚动到视图中,请使用UITableView。scrollToRowAtIndexPath(_:atScrollPosition:动画:)
Notice that the does the right thing when the user hides/shows the completions bar.
请注意,当用户隐藏/显示完成栏时,执行的操作是正确的。
import UIKit
import CoreGraphics
// our Cell class
class Cell : UITableViewCell
{
// cell reuse identifier for table view
static let Identifier = "Cell"
// the object the cell represents/displays. Could be anything you like
var value:AnyObject? {
didSet {
// update our text field when our cell value is set.
self.textField.text = value as? String
}
}
// use a text field to display our contents, since that allows editing and showing the keyboard
lazy var textField:UITextField = {
let textField = UITextField()
self.contentView.addSubview( textField )
return textField
}()
override func layoutSubviews() {
super.layoutSubviews()
self.textField.frame = contentView.bounds.insetBy(dx: 20, dy: 4 )
}
}
// table view data source class
class DataSource : NSObject, UITableViewDataSource
{
var numberOfRows:Int { return items.count }
let items = [ "Seoul", "São Paulo", "Bombay", "Jakarta", "Karachi", "Moskva", "Istanbul", "Mexico", "Shanghai", "Tokyo", "New" York, "Bangkok", "Beijing", "Delhi", "London", "*", "Cairo", "Tehran", "Bogota", "Bandung", "Tianjin", "Lima", "Rio de Janeiro", "Lahore", "Bogor", "Santiago", "St Petersburg", "Shenyang", "Calcutta", "Wuhan" ]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numberOfRows
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier( Cell.Identifier ) as? Cell ?? Cell()
cell.value = items[ indexPath.row ]
return cell
}
}
class ViewController : UIViewController
{
override func viewDidLoad() {
super.viewDidLoad()
// register for notifications when the keyboard appears:
NSNotificationCenter.defaultCenter().addObserver( self, selector: "keyboardWillShow:", name: UIKeyboardWillChangeFrameNotification, object: nil)
}
override func viewDidLayoutSubviews() {
tableView.frame = view.bounds
}
lazy var tableView:UITableView = {
let tableView = UITableView()
self.view.addSubview( tableView )
tableView.dataSource = self.dataSource
tableView.delegate = self
return tableView
}()
lazy var dataSource : DataSource = DataSource()
// Handle keyboard frame changes here.
// Use the CGRect stored in the notification to determine what part of the screen the keyboard will cover.
// Adjust our table view's contentInset and scrollIndicatorInsets properties so that the table view content avoids the part of the screen covered by the keyboard
@objc func keyboardWillShow( note:NSNotification )
{
// read the CGRect from the notification (if any)
if let newFrame = (note.userInfo?[ UIKeyboardFrameEndUserInfoKey ] as? NSValue)?.CGRectValue() {
let insets = UIEdgeInsetsMake( 0, 0, newFrame.height, 0 )
tableView.contentInset = insets
tableView.scrollIndicatorInsets = insets
}
}
}
// need to conform to UITableViewDelegate protocol since we are the table view's delegate
extension ViewController : UITableViewDelegate
{
}
// App set up stuff here:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy var window:UIWindow? = UIWindow()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
window!.rootViewController = ViewController()
window!.makeKeyAndVisible()
return true
}
}