【问题标题】:Moving uiTextField With ScrollView - Scroll stop working使用 ScrollView 移动 uiTextField - 滚动停止工作
【发布时间】:2016-03-06 16:19:59
【问题描述】:

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

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 停止滚动后,我的应用程序有很多字段需要滚动屏幕才能看到。

我可能做错了什么?

【问题讨论】:

  • 你能在keyboardWillBeShownkeyboardWillBeHidden函数里面检查backgroundScrollViewcontentsize..如果它小于显示区域大小或0
  • 我如何检查这个?我对此很陌生。

标签: ios iphone uiscrollview uitextfield inputview


【解决方案1】:

keyboardWillBeShown() 方法中设置内容大小:-

backgroundScrollView.contentSize = CGSizeMake(0, 600)

0 "width" 表示 -> 不要在 x 方向滚动

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

【讨论】:

  • 但是我有 UITextFields 被键盘隐藏了,我需要它与 inputView 对齐。
  • 所以基本上你想在打开键盘时向上移动文本字段吗?
  • github.com/michaeltyson/TPKeyboardAvoiding 这个库会解决你所有的问题。如果不让我知道,那么。
  • 我什么都不懂..哈哈,是客观C?
  • 哈哈是的...无论如何在键盘中设置 contentsize 将解决您的滚动问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
相关资源
最近更新 更多