Move up keyboard when editing UITextField on iOS9

后端 未结 7 1380
太阳男子
太阳男子 2021-02-02 00:03

For my keyboards to move up to uncover UITextField in my iOS app, I used to implement this answer: https://stackoverflow.com/a/6908258/3855618 on iOS7 and 8 and it

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-02 00:18

    Zero lines of Code

    Devoid of hacks, kludges, workaround and listeners.

    The present question has been asked over and over since the dawn of iOS time. No answer on StackOverflow survived more than 2 iOS iterations. Rightly so, because the UIKit keeps changing from underneath your feet. There exists a design as opposed to implementation solution to this ancient problem. Use a UITableViewController.

    Use a UITableViewController

    When a UITableView is managed by a UITableViewController, the scrolling is managed automatically for you. Never tinker with UIKeyboardWillShowNotification, ever again. Merely create static or dynamic UITableViewCells to layout your interface, add UITextView or UITextField as needed ; merely becoming first responder will scroll the the proper location.

    @availability(iOS, introduced=2.0)

    Notes

    1. Works on all iOS since 2.0.
    2. Quote: «Waste no time optimizing a poor algorithm ; pick a better one»
    3. See https://stackoverflow.com/a/32390936/218152.

提交回复
热议问题