aCertos aCertos - 4 years ago 193
Swift Question

TextViews not in correct position in ScrollView

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download