0

My UIViewController contains a UITableView (is always on top, the same width with the superview), and a UITextView (is always under the UITableView 10.0f), as below:

enter image description here

Now I want to use autolayout to raise up the UITextView when the keyboard appears. I add the following constraints for UITableView (sticks to the top, and is always on top of UITextView 10.0f):

  • UITableView : Width (320), Height (706), Left, Right, Top, Bottom (10.0f)
  • UITextView : Width (280), Height (44), Left, Right

enter image description here

I assign IBOutlet the UITableView's height constraint, and modify its value in code. For example, when the keyboard appears, the height of UITableView will be decreased -216.0f (the height of the keyboard), and as a result, the UITextView will be raised up on top of the keyboard (because the UITextView is always under the UITableView 10.0f).

So, that's what I expected. However, for the only first step, when running application, the UITableView takes up the whole screen, and I do not see my UITextView. Is there any problem with my constraints?

Soumya Ranjan
  • 4,817
  • 2
  • 26
  • 51
chipbk10
  • 5,783
  • 12
  • 49
  • 85

1 Answers1

0

the keyboards default behaviour is to cover the screen, autolayout cant account for it since its not part of your layout originally, i wrote a category that will provide this functionality for you here, just #import it into your viewController and it should just work

Community
  • 1
  • 1
Fonix
  • 11,447
  • 3
  • 45
  • 74
  • although if you want your tableview to squash when the keyboard shows, this solution will not work for you, this just moves the whole screen up – Fonix May 20 '14 at 09:48
  • Don't bother the keyboard behavior. The problem is with my above constraints, I cannot see my UITextView when the UIViewController at first appears. – chipbk10 May 20 '14 at 10:55
  • oh i see, your viewcontroller looks quite long for some reason, you have your tableViews height set to 706, but the longest and iPhone screen can be is 568 (480 for 3.5" devices). i think you need to resize your viewController in the storyboard – Fonix May 20 '14 at 11:43
  • you could change the height constraint to be <= 706 as well, and put its compression resistance low and priority up, so that it was squash automatically, but im not sure if it would anyway with the vc being the wrong height initially, but it may work – Fonix May 20 '14 at 11:54
  • My device is iPad Air. So its height is 768 (landscape) – chipbk10 May 20 '14 at 12:06
  • Actually, I put the UIViewController into a UINavigationController, so maybe the NavigationBar takes up a space that makes my UITableView in wrong position? – chipbk10 May 20 '14 at 12:11