使用ScrollView移动uiTextField - 滚动停止工作

时间:2015-12-02 12:14:45

标签: ios iphone uiscrollview uitextfield inputview

我正在使用某些功能在UITextFields之后立即移动InputView,请参阅下面的代码:

func DismissKeyboard(){
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.registerForKeyboardNotifications()
}

override func viewDidDisappear(animated: Bool) {
    super.viewWillDisappear(animated)
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func registerForKeyboardNotifications() {
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeShown:",
        name: UIKeyboardWillShowNotification,
        object: nil)
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)
}

func keyboardWillBeShown(sender: NSNotification) {
    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameBeginUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size
    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height + 8, 0.0)
    backgroundScrollView.contentInset = contentInsets
    backgroundScrollView.scrollIndicatorInsets = contentInsets
}

// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) {
    let insets: UIEdgeInsets = UIEdgeInsetsMake(self.backgroundScrollView.contentInset.top, 0, 0, 0)

    //let insets: UIEdgeInsets = UIEdgeInsetsZero
    backgroundScrollView.contentInset = insets
    backgroundScrollView.scrollIndicatorInsets = insets

}
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

正在发生的事情是,在点击第一个UITextFieldbackgroundScrollView停止滚动和我的应用后,有很多字段需要滚动屏幕才能看到。

我可能做错了什么?

1 个答案:

答案 0 :(得分:0)

keyboardWillBeShown()方法setcontentSize: -

backgroundScrollView.contentSize = CGSizeMake(0, 600)

0“宽度”表示 - >不要在x方向滚动

600“高度”表示 - >滚动内容可以滚动到600高度