ios - 如何使用点击手势放置文本字段位置

标签 ios swift uiscrollview uitextfield

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)
}

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

最佳答案

试试这个:

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

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

关于ios - 如何使用点击手势放置文本字段位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34606676/

相关文章:

ios - 如何确定滚动 UIDatePicker 何时完成?

ios - Swift:实现可拖动的 UIImageView 需要哪些方法?

swift - 如何在集合扩展中返回空子序列

ios - Core Plot Swift BarPlot tickLocation 始终为零

ios - 如何防止像 iPhone 照片应用程序那样自动旋转?

ios - UITextView 使用自动布局动态扩展为 ScrollView 内的文本

iphone - 在 XCode 4.2 中使用动态自定义 UITableViewCell,以及 Storyboards 和 UISeachDisplayController

ios - 为什么按钮开关不起作用?

swift - 使用结构进行转换时的对称性。什么是最佳实践?

ios - 在 hitTest :withEvent: 拦截 UIControlEventTouchUpInside