【问题标题】:Swift: scrolling not working斯威夫特:滚动不起作用
【发布时间】:2016-12-31 05:28:12
【问题描述】:

我有一个与主视图高度不同的 uiscrollview。我在这个滚动视图之外有一个 uitextfield 和一个按钮。我在其中输入一个值,然后单击按钮,它会在滚动视图中创建另一个文本字段,然后如果我再次按下,它将在先前创建的文本字段下方创建另一个文本字段。现在,我正在尝试将视图滚动到新创建的文本字段,该文本字段应该在键盘视图上方可见。我尝试了以下方法。它不起作用:

func keyboardWillShow() {
var userInfo = notification.userInfo!
var keyboardFrame:CGRect = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let textField: UITextField = self.scrollView!.viewWithTag(rowsCount) as! UITextField
let newRect: CGRect = CGRectMake(textField.frame.origin.x, textField.frame.origin.y, textField.frame.width, textField.frame.height)
let newFrame = self.view.convertRect(newRect, fromView: nil)
keyboardFrame = self.view.convertRect(keyboardFrame, fromView: nil)
//          contentInset.bottom = newRect.origin.y + newRect.height
            var viewRect: CGRect = self.view.frame
            viewRect.size.height = viewRect.height - keyboardFrame.height - 40.0

            if (!CGRectContainsPoint(viewRect, (newFrame.origin))) {
                self.scrollView.scrollRectToVisible((newFrame), animated: true)
            }
}

P.S.:rowsCount 每次创建新文本字段时都会增加。

【问题讨论】:

  • 没有得到确切的要求!!当您按下按钮时,它将创建新的文本字段,那么您是否希望该文本字段作为活动文本字段(为其提供键盘)?
  • 我不想要活动文本字段,因为活动文本字段将始终是我输入值的那个(在 uiscrollview 之外的那个)。我想要的是滚动视图滚动到新创建的文本字段。

标签: ios swift uiscrollview uitextfield cgrect


【解决方案1】:

你可以这样做,

 scrollView.scrollRectToVisible(CGRect(x: x, y: y, width: 1, height: 1), animated: true)

 // or

 scrollView.setContentOffset(CGPoint(x: x, y: y), animated: true)

单击按钮时将 x 和 y 传递给此。

【讨论】:

  • 不工作。问题是我正在尝试检查该文本字段是否在该视图中(uiscrollview - 键盘视图),但我的代码正在检查该文本字段是否在完整的 uiscrollview 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 2017-03-24
  • 2016-02-16
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多