Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

cocoa touch - Move view when so that keyboard does not hide text field

In my app, when I click on a text field, the keyboard hides it. Please help me -- how can I move my view up when I click on the text field. I'm using this code in textFieldDidBeginEditing:

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0);
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);

but it doesn't work.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should not trust textFieldDidBeginEditing: to adjust for the keyboard, since this method will be called even if the user is typing using a physical keyboard where an onscreen keyboard will not be displayed.

Instead listen to the UIKeyboardWillShowNotification, that is only triggered when the keyboard will actually be displayed. You need to do a three step process:

  1. Determine actual size of keyboard from the notifications userInfo dictionary. The size will differ from landscape/portrait, and different devices.
  2. Update the contentInset using the determined size. You can do it animated, the notification will even tell you the duration for the keyboard animation.
  3. Scroll the textfield into view, very easy to forget this!

You find more information and sample code from here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...