【问题标题】:How to place textfield position using tap gesture如何使用点击手势放置文本字段位置
【发布时间】:2016-04-08 23:34:15
【问题描述】:
super.viewDidLoad()

    let tapGesture = UITapGestureRecognizer(target: self, action: Selector("hideKeyboard"))
    tapGesture.cancelsTouchesInView = true

    scrlView.addGestureRecognizer(tapGesture)

    //self.navigationController!.navigationBarHidden = true

     scrlView.contentSize = CGSizeMake(UIScreen .mainScreen().bounds.size.width, (regButton.frame.size.height + regButton.frame.origin.y)+20)

    // Do any additional setup after loading the view.
}

func hideKeyboard() {
    scrlView.endEditing(true)
    scrlView .setContentOffset(CGPointMake(0, 0), animated: true)
}


@IBAction func tapped(sender: AnyObject) {

    scrlView.endEditing(true)
    scrlView .setContentOffset(CGPointMake(0, 0), animated: true)
}

我的 hideKeyboard 功能没问题。但是现在我想在点击它们时改变键盘上方文本字段的位置。

【问题讨论】:

  • 当文本字段开始编辑然后它应该向上滚动时,你到底需要什么意思??
  • 完全正确.. @Shubhambairagi
  • 那么你需要做的是设置委托textFieldShouldReturn。此处为 scrollview 设置 contant 偏移量。并在 tapGausture 上再次重置。见下面我的回答
  • 答案在哪里?

标签: ios swift uiscrollview uitextfield


【解决方案1】:

试试这个:

func textFieldShouldBeginEditing(textField: UITextField) -> Bool{
    scrl.setContentOffset(CGPointMake(0.0, textField.center.y-120), animated: true)
}

您的func hideKeyboard() 应该保持不变 希望对你有帮助

【讨论】:

  • 您可以根据自己的要求设置ContentOffset
  • func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() scrlView .setContentOffset(CGPointMake(0, 0), animated: true) return true } 我用过这个方法。跨度>
  • textFieldShouldBeginEditing 你也需要设置
  • 不要使用ContentOffset 等于CGPointMake(0, 0) 这是滚动视图的初始ContentOffset。您需要设置CGPointMake(0, Y-Param) Y-Param 可能是一些值
【解决方案2】:

您可以为您的项目使用 IQkeyboardmanger 第三方库,这是链接,https://github.com/hackiftekhar/IQKeyboardManager

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多