0

I have two UITextView boxes that are in a UIView which is in a UIScrollView. My problem is that when I run the app in simulator, the UITextView boxes are sitting lower than I would like, but after clicking within a UITextView box, then dismissing the keyboard and my textViewDidEndEditing function fires off, they set back to the position that I originally wanted them to be in. In my textViewDidEndEditing function I did set the UIScrollView content offset to go back to (0,0) so my initial solution thought is I need to set the content offset to (0,0) immediately which would then put both of my UITextView boxes where I want them.

Now first I tried doing that in viewDidLoad, but there was no change, so I then tried setting it in viewDidAppear, which worked, but my issue with that is once the view appears, you actually see the textview boxes pop into place, and I do not like that. I also checked my constraints and they are all good, as i thought they might be causing the problem as well. I am not quite sure what I can do to make it work without actually seeing the UITextView pop into place when the view appears.

This is what the UITextView boxes look like when the view controller first comes up without the viewDidAppear method setting content offset.

enter image description here

And this is what I want the UITextView boxes to look like.

enter image description here

Any help would be greatly appreciated. Thank you

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
ACerts
  • 308
  • 1
  • 6
  • 22
  • 1
    If u create it programatically, try create the box in `viewDidLoad` then set their place in `viewDidLayoutSubview`, it should do the trick – Tj3n Jan 04 '16 at 05:12
  • Thank you for the reply. So just to make sure i understand, delete both textview boxes from storyboard and create them in viewDidLoad then set them in viewDidLayoutSubview? – ACerts Jan 04 '16 at 05:25
  • 1
    in `viewDidLoad` set `self.automaticallyAdjustsScrollViewInsets = false` – Hamza Ansari Jan 04 '16 at 05:26
  • Sorry, i meant whatever u set in code that related to size and constraint, u should put it in `viewDidLayoutSubview`, it will be same as u set it in `viewDidAppear` but wont be notice by eye, if u already create those boxes with storyboard, then keep it :) in this case is your `scrollview content offset` i guess? – Tj3n Jan 04 '16 at 05:30
  • @Hamza Ansari, that worked perfectly. Thank you very much. Is there a reason why scrollview insets are set automatically by default? – ACerts Jan 04 '16 at 05:32

1 Answers1

0

In your viewDidLoad set viewController autoScollViewInsets to false:

override func viewDidLoad() {
    super.viewDidLoad()

    self.automaticallyAdjustsScrollViewInsets = false
}

you want more detail about this check this answer: Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout in iOS7

Community
  • 1
  • 1
Hamza Ansari
  • 3,009
  • 1
  • 23
  • 25