简体   繁体   English

键盘出现时快速移动视图-Bug

[英]swift move view when keyboard appears - Bug

I am new developing with swift and don't know how to resolve my problem. 我是迅速发展的新人,不知道如何解决我的问题。 I have two views. 我有两种看法。 One for signUp and one for signIn. 一个用于注册,另一个用于登录。 I added the following functions to my viewControllers and everything works fine. 我将以下函数添加到我的viewControllers中,一切正常。 When I click on each textField (for password, username) the view moves up when keyboard appears. 当我单击每个textField(用于输入密码,用户名)时,出现键盘时视图就会向上移动。 But when I sign in a user and after that call "Part 2" of my code and the User is signed in, the view changes from my signInView to my signUpView. 但是,当我登录一个用户并在调用代码的“第2部分”并且该用户登录后,该视图从我的signInView变为我的signUpView。 The problem: after I did the following steps and tab now on my textFields the keyboard don't appear anymore. 问题:在我执行以下步骤并在我的textFields上进行分页之后,键盘不再显示。

Part 1: Code in signInViewController: 第1部分:signInViewController中的代码:

var kbHeight: CGFloat!

override func viewDidLoad() {
    super.viewDidLoad()

    textFieldUserName.delegate = self
    txtFieldUserPassword.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    txtFieldUserPassword.resignFirstResponder()

    return true
}

//viewDidAppear or viewWillAppear?
override func viewDidAppear(animated:Bool) {
    super.viewWillAppear(animated)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}

override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        if let keyboardSize =  (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
            kbHeight = keyboardSize.height-50
            self.animateTextField(true)
        }
    }
}

func keyboardWillHide(notification: NSNotification) {
    self.animateTextField(false)
}

func animateTextField(up: Bool) {
    let movement = (up ? -kbHeight : kbHeight)

    UIView.animateWithDuration(0.3, animations: {
        self.view.frame = CGRectOffset(self.view.frame, 0, movement)        //Fatal Error: nil sometimes? - Why?
    })
}

Part 2: Code in signInViewController 第2部分:signInViewController中的代码

do {
        try signup.signUpUser()
        self.showAlert(self)
} catch ...


func showAlert(viewController : UIViewController) -> Void {

    let alertController = UIAlertController(title: "Registrieren erfolgreich", message: "Sie können sich jetzt Anmelden", preferredStyle: .Alert)

    alertController.addAction(UIAlertAction(title: "Anmelden", style: .Default, handler: { (alertAction) -> Void in self.dismissViewControllerAnimated(true, completion: nil) }))

    viewController.presentViewController(alertController, animated: true, completion: nil)

I use this handy solution called TPKeyboardAvoiding in one of my apps: https://github.com/michaeltyson/TPKeyboardAvoiding 我在我的一个应用程序中使用了这个名为TPKeyboardAvoiding的便捷解决方案: https : //github.com/michaeltyson/TPKeyboardAvoiding

With this library you don't need handle this kind of scroll behavior on your app, imagine that you can have other behavior in other VC, and you need to calculate it every time. 使用此库,您无需在应用程序上处理这种滚动行为,可以想象您可以在其他VC中具有其他行为,并且每次都需要对其进行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM