How to re-size UITextView when keyboard shown with iOS 7

后端 未结 8 1416
耶瑟儿~
耶瑟儿~ 2020-12-05 03:34

I have a view controller which contains a full-screen UITextView. When the keyboard is shown I would like to resize the text view so that it is not hidden under

8条回答
  •  情歌与酒
    2020-12-05 04:12

    With Auto Layout, it's much easier (provided you understand Auto Layout) to handle:

    Instead of trying to identify and resize the affected views, you simply create a parent frame for all your view's contents. Then, if the kbd appears, you resize the frame, and if you've set up the constraints properly, the view will re-arrange all its child views nicely. No need to fiddle with lots of hard-to-read code for this.

    In fact, in a similar question I found a link to this excellent tutorial about this technique.

    Also, the other examples here that do use textViewDidBeginEditing instead of the UIKeyboardWillShowNotification have one big issue:

    If the user has an external bluetooth keyboard attached then the control would still get pushed up even though no on-screen keyboard appears. That's not good.

    So, to summarize:

    1. Use Auto Layout
    2. Use the UIKeyboardWillShowNotification notification, not the TextEditField's events for deciding when to resize your views.

    Alternatively, check out LeoNatan's reply. That might even be a cleaner and simpler solution (I've not tried myself yet).

提交回复
热议问题