【问题标题】:UIStackview with UIKeyboard in swift2.0swift2.0中带有UIKeyboard的UIStackview
【发布时间】:2016-03-01 06:32:30
【问题描述】:

我正在使用带有 swift2.0 的 iOS9。我的视图控制器处理两个文本字段和一些标签。这两个文本字段包含在堆栈视图中,标签也包含在另一个堆栈视图中。我已经设置了纵向和横向选项的通用应用程序,它的工作很好。但是当我点击文本字段时,当时键盘是向上的,键盘覆盖了文本字段,所以我看不到文本字段数据。如何解决这个问题以及如何在键盘向上和向下时管理所有约束?

override func viewWillAppear(animated: Bool) {

    super.viewWillAppear(animated)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object:nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector:("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)

}
override func viewWillDisappear(animated: Bool) {

    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification:NSNotification){

    print("keyboardWillShow")
    var info = notification.userInfo!
    let keyboardFrame:CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    print(keyboardFrame)
    UIView.animateWithDuration(0.1, animations: { () -> Void in

    })
}

func keyboardWillHide(notification:NSNotification){

      print("keyboardWillHide")

}

【问题讨论】:

    标签: iphone swift2 ios9 uikeyboard uistackview


    【解决方案1】:
    func keyboardWillShow(sender: NSNotification){
    
        var info = sender.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    
    
        UIView.animateWithDuration(0.1, animations: { () -> Void in
    
            self.view.center.y = self.view.center.y - keyboardFrame.size.height
        })
    
    
    }
    func keyboardWillHide(sender: NSNotification){
    
        var info = sender.userInfo!
        let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            self.view.center.y = self.view.center.y + keyboardFrame.size.height
        })
    
    }
    

    【讨论】:

    • 点击两个文本字段时它的移动视图向上。它不能正常工作。
    • 你想要什么? @jasper 它将您的视图移动到上键盘,如果您只想将文本字段移动而不是整个视图,您可以设置您的文本字段中心而不是像 textfield.center.y 那样的视图,还可以通过标签检测您的文本字段。
    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    相关资源
    最近更新 更多