【问题标题】:Disappear keyboard in uitextview在uitextview中消失键盘
【发布时间】:2016-02-17 04:45:37
【问题描述】:

我在 swift 中使用 uitextview,而键盘消失我使用的是 uitextview 委托方法

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    if(text == "\n") {
        textView.resignFirstResponder()
        return false
    }
    return true
}

但我不想使用此委托方法返回键盘 bcz 我想在下一行使用返回键。 有没有其他方法可以让键盘消失? 出于某种原因,我不想使用触摸事件。

【问题讨论】:

  • 您在键盘外添加手势以将其关闭。
  • 我用过,但由于某些原因我不想使用手势。因为当我使用手势时,它会禁用某些功能。
  • 请去掉“return false”并尝试。

标签: ios swift uikit


【解决方案1】:

除非您使用自定义键盘,否则您不能在键盘上同时拥有返回键和关闭键。可以通过设置inputViewproperty来添加自定义键盘

更好的标准做法是在键盘顶部设置一个按钮,让用户可以选择隐藏它。这可以通过为UITextView 分配inputAccessoryView 来完成。更多here

【讨论】:

    【解决方案2】:

    尝试使用工具栏

    override func viewDidAppear(animated: Bool) {
    
        ...
    
    
        var toolBar = UIToolbar()
        toolBar.barStyle = UIBarStyle.Default
        toolBar.translucent = true
        toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
    
        var nextButton = UIBarButtonItem(title: "Next", style: UIBarButtonItemStyle.Done, target: self, action: "nextTextfield")
        var previousButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "previousTextfield")
        var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
        toolBar.setItems([previousButton, spaceButton, nextButton], animated: false)
        toolBar.userInteractionEnabled = true
        toolBar.sizeToFit()
    
        textField.delegate = self
        textField.inputAccessoryView = toolBar
    }
    
    func nextTextfield() {
        nextTextField.resignFirstResponder()
    }
    
    func previousTextfield() {
       //if exist previous        
       //previousTextField.resignFirstResponder()
    }
    

    【讨论】:

      【解决方案3】:

      试试这个

      self.view!.endEditing(true)
      

      【讨论】:

        【解决方案4】:

        您可以在键盘上添加完成工具栏。在完成操作后,您可以退出键盘

        【讨论】:

        • 我也在考虑添加工具栏。但是不能在键盘中添加返回键和下一行吗?
        • 一次您只能在键盘上使用一个键,否则您必须使用定制键盘(第三方键盘)。 . .在我看来,使用 done tool bar 。 .这是完美的。
        【解决方案5】:

        使用适用于 Swift 和 Objective C 的 IQKeboardManager 类。

        下面编写的快速代码将对您有所帮助。

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
        {
            // Override point for customization after application launch.
        
            IQKeyboardManager.sharedManager().enable = true
        }
        

        下面给出的链接会对您有所帮助。

        https://github.com/hackiftekhar/IQKeyboardManager

        【讨论】:

          猜你喜欢
          • 2014-12-19
          • 1970-01-01
          • 2012-01-05
          • 2023-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多