键盘弹出时如何向上移动文本字段

时间:2018-09-01 19:46:23

标签: ios swift

我正从目标c移到Swift,任何人都可以指定如何停止键盘以隐藏文本字段。问题是当用户单击屏幕底部附近的最后一个文本字段时,我的视图控制器中有一些文本字段,键盘出现并隐藏了该文本字段。有什么容易解决的问题还是我应该使用滚动视图?

1 个答案:

答案 0 :(得分:-1)

使用名为IQKeyboardManager的此库: https://github.com/hackiftekhar/IQKeyboardManager

Install pod file pod 'IQKeyboardManager’ . And in your app delegate 

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


         IQKeyboardManager.shared.enable = true
         IQKeyboardManager.shared.enableAutoToolbar = true
        IQKeyboardManager.shared.toolbarDoneBarButtonItemText = "Done”

      return true
    }
}


You can change the following properties of IQKeyboardManager as well

       IQKeyboardManager.shared.overrideKeyboardAppearance = true
       IQKeyboardManager.shared.keyboardAppearance = .dark
       IQKeyboardManager.shared.toolbarBarTintColor = UIColor.cyan
       IQKeyboardManager.shared.toolbarDoneBarButtonItemImage = UIImage(named: "<#T##String#>")

       IQKeyboardManager.shared.shouldShowToolbarPlaceholder = false
        IQKeyboardManager.shared.placeholderFont = UIFont(name: "Times New Roman", size: 20.0)
        IQKeyboardManager.shared.shouldResignOnTouchOutside = true
        IQKeyboardManager.shared.shouldPlayInputClicks = true //default