【问题标题】:Show uitextview with keypad and toolbar when editing textfield编辑文本字段时显示带有键盘和工具栏的 uitextview
【发布时间】:2018-12-23 07:48:35
【问题描述】:

实现您在 gif 中看到的内容的正确方法是什么?这是一个带有文本字段的表格视图。当您点击一个文本字段时,一个文本视图会显示在它的键盘上。我有一个带有文本字段(valueTextField)的自定义表格视图单元的表格视图。在 cellforrow 中,我将文本字段的 inputview 设置为 UITextView。当我按下文本字段时,文本视图应该在顶部显示工具栏(带有“完成”按钮)但没有工具栏显示。当我点击 textview 时,仍然没有工具栏。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as! EditProfileTableViewCell
    cell.isUserInteractionEnabled = true
    cell.valueTextField.isUserInteractionEnabled = true
    cell.valueTextField.delegate = self
    toolBar = UIToolbar()
    toolBar.sizeToFit()

    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(editorDone))
    toolBar.setItems([doneButton], animated: false)

    editorTextView = UITextView(frame: CGRect(x:0, y:0, width:view.bounds.width, height:view.bounds.height))
    cell.valueTextField.inputAccessoryView = toolBar
        cell.valueTextField.inputView = editorTextView

    return cell
}

【问题讨论】:

    标签: swift uitextfield uitextview inputaccessoryview inputview


    【解决方案1】:

    您展示的示例只是在点击单元格时使用UINavigationController 导航到单独的UIViewController。在这个视图控制器中,UITextField 将被这样处理。

    // View controller subclass conforming to UITextFieldDelegate protocol
    class MyDetailViewController: UIViewController, UITextFieldDelegate {
    
        // Set an IB Outlet to the text field in storyboard
        @IBOutlet myTextField: UITextField!
    
        override viewDidLoad() {
            myTextField.delegate = self
    
            // ToolBar
            let toolBar = UIToolbar()
            // Optional styling
            toolBar.barStyle = .default
            toolBar.isTranslucent = true
            toolBar.tintColor = .black
            toolBar.layer.borderWidth = 0
    
            toolBar.sizeToFit()
    
            // Buttons
            let buttonOne = BarButtonItem(title: "One", style: .plain, target: self, action: #selector(yourSelectorOne(_:)))
            let buttonTwo = BarButtonItem(title: "One", style: .plain, target: self, action: #selector(yourSelectorTwo(_:)))
    
            let spacer = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    
           toolBar.setItems([buttonOne, spacer, buttonTwo], animated: false)
           toolBar.isUserInteractionEnabled = true
           textField.inputAccessoryView = toolBar
        }
    }
    

    这将创建两个按钮,一个在工具栏的左侧,一个在工具栏的右侧。键盘类型可以在Interface Builder中设置。

    要通过直接点击单元格中的文本字段来实现此目的,无需导航到新的视图控制器,您可以将自定义 UITableViewCell 子类设置为文本字段委托。

    请注意,您需要提供方法存根以符合UITextFieldDelegate 协议。这些对于本示例来说不是必需的,因此我省略了它们。 XCode 将提供为您填充它们。

    【讨论】:

    • 知道了。这是我将要使用的方法,但我认为我可以避免导航到另一个视图控制器来呈现视图。谢谢。
    【解决方案2】:

    您的代码中几乎没有您想要更改的内容。

    1) 这个视图的框架会导致这种奇怪的行为:

    UITextView(frame: CGRect(x:0, y:0, width:view.bounds.width, height:view.bounds.height))
    

    2) 你之所以犯这个错误,是因为你搞混了

    .inputAccessoryView
    .inputView
    

    3) 您发布的 gif 是基于导航栏按钮的。基本上,它通过点击单元格来呈现一个详细的视图控制器,其中包含在导航栏中设置的两个项目

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      • 1970-01-01
      相关资源
      最近更新 更多