【问题标题】:Adding the "Clear" Button to an iPhone UITextField将“清除”按钮添加到 iPhone UITextField
【发布时间】:2010-09-24 02:53:26
【问题描述】:

如何在 UITextField 右侧添加用于清除文本的小“X”按钮?我在 iPhone OS 2.2 SDK 的 Interface Builder 中找不到用于添加此子控件的属性。

注意:在 Xcode 4.x 及更高版本(iPhone 3.0 SDK 及更高版本)中,您可以在 Interface Builder 中执行此操作。

【问题讨论】:

    标签: ios cocoa-touch uitextfield uikit


    【解决方案1】:

    此按钮是由UITextField 类提供的内置叠加层,但在 iOS 2.2 SDK 中,无法通过 Interface Builder 设置它。您必须以编程方式启用它。

    在某处添加这行代码(例如viewDidLoad):

    Objective-C

    myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    

    Swift 5.0

    myUITextField.clearButtonMode = .whileEditing
    

    【讨论】:

      【解决方案2】:

      您也可以直接在 Attributes Inspector 下的 Interface Builder 中进行设置。

      取自 XCode 5.1

      【讨论】:

      • 请注意,该问题专门询问 2.2 SDK,并注意此选项在更高版本的 Interface Builder 中可用。
      【解决方案3】:

      斯威夫特 4+:

      textField.clearButtonMode = UITextField.ViewMode.whileEditing
      

      甚至更短:

      textField.clearButtonMode = .whileEditing
      

      【讨论】:

      • 修复枚举类型。不能以大写字母开头。
      【解决方案4】:

      您可以添加自定义清除按钮并使用此按钮控制大小和所有内容:

      UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
      [clearButton setImage:img forState:UIControlStateNormal];
      [clearButton setFrame:frame];
      [clearButton addTarget:self action:@selector(clearTextField:) forControlEvents:UIControlEventTouchUpInside];
      
      textField.rightViewMode = UITextFieldViewModeAlways; //can be changed to UITextFieldViewModeNever,    UITextFieldViewModeWhileEditing,   UITextFieldViewModeUnlessEditing
      [textField setRightView:clearButton];
      

      【讨论】:

        【解决方案5】:

        目标 C:

        self.txtUserNameTextfield.myUITextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        

        斯威夫特:

        txtUserNameTextfield.clearButtonMode = UITextField.ViewMode.WhileEditing;
        

        【讨论】:

          【解决方案6】:

          Swift 4(改编自 Kristopher Johnson 的回答)

          textfield.clearButtonMode = .always
          
          textfield.clearButtonMode = .whileEditing
          
          textfield.clearButtonMode = .unlessEditing
          
          textfield.clearButtonMode = .never
          

          【讨论】:

            【解决方案7】:

            这行不通,像我一样做:

            迅速:

            customTextField.clearButtonMode = UITextField.ViewMode.Always
            
            customTextField.clearsOnBeginEditing = true;
            
            func textFieldShouldClear(textField: UITextField) -> Bool {
                return true
            }
            

            【讨论】:

              【解决方案8】:

              在 Xcode 8 (8A218a) 上:

              斯威夫特:

              textField.clearButtonMode = UITextField.ViewMode.whileEditing;
              

              “W”从大写变为非大写“w”。

              【讨论】:

                【解决方案9】:
                  func clear_btn(box_is : UITextField){
                    box_is.clearButtonMode = .always
                    if let clearButton = box_is.value(forKey: "_clearButton") as? UIButton {
                        let templateImage =  clearButton.imageView?.image?.withRenderingMode(.alwaysTemplate)
                
                        clearButton.setImage(templateImage, for: .normal)
                        clearButton.setImage(templateImage, for: .highlighted)
                
                        clearButton.tintColor = .white
                
                     }
                }
                

                【讨论】:

                  【解决方案10】:

                  在 Xcode 版本 8.1 (8B62) 上,它可以直接在 Attributes Inspector 中完成。选择 textField,然后从位于 Attributes Inspector 中的 Clear Button 下拉框中选择适当的选项。

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-01-25
                    • 2020-03-27
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多