Custom Clear Button

后端 未结 3 928
旧时难觅i
旧时难觅i 2020-12-15 12:48

I want to create custom clear button on UITextField, that is to use rightView and put image there, the problem is attaching the original clear button event to that custom r

相关标签:
3条回答
  • 2020-12-15 13:09

    Here is my solution in Swift 3. In addition to the already existing answer, I also made sure that both left and right views of the textfield (i.e. the search magnifier image view and the custom clear button) have a padding to their left/right by overriding leftViewRect() and rightViewRect(). Otherwise, they will stick right on the edges of the textfield.

    class CustomTextField: UITextField
    {
        fileprivate let searchImageLength: CGFloat = 22
        fileprivate let cancelButtonLength: CGFloat = 15
        fileprivate let padding: CGFloat = 8
    
    
        override init( frame: CGRect )
        {
            super.init( frame: frame )
            self.customLayout()
        }
    
        required init?( coder aDecoder: NSCoder )
        {
            super.init( coder: aDecoder )
            self.customLayout()
        }
    
        override func leftViewRect( forBounds bounds: CGRect ) -> CGRect
        {
            let x = self.padding
            let y = ( bounds.size.height - self.searchImageLength ) / 2
            let rightBounds = CGRect( x: x, y: y, width: self.searchImageLength, height: self.searchImageLength )
            return rightBounds
        }
    
        override func rightViewRect( forBounds bounds: CGRect ) -> CGRect
        {
            let x = bounds.size.width - self.cancelButtonLength - self.padding
            let y = ( bounds.size.height - self.cancelButtonLength ) / 2
            let rightBounds = CGRect( x: x, y: y, width: self.cancelButtonLength, height: self.cancelButtonLength )
            return rightBounds
        }
    
        fileprivate func customLayout()
        {
            // Add search icon on left side
            let searchImageView = UIImageView()
            searchImageView.contentMode = .scaleAspectFit
            let searchIcon = UIImage( named: "search_magnifier" )
            searchImageView.image = searchIcon
            self.leftView = searchImageView
            self.leftViewMode = .always
    
            // Set custom clear button on right side
            let clearButton = UIButton()
            clearButton.setImage( UIImage( named: "search_cancel" ), for: .normal )
            clearButton.contentMode = .scaleAspectFit
            clearButton.addTarget( self, action: #selector( self.clearClicked ), for: .touchUpInside )
            self.rightView = clearButton
            self.clearButtonMode = .never
            self.rightViewMode = .whileEditing
        }
    
        @objc fileprivate func clearClicked( sender: UIButton )
        {
            self.text = ""
        }
    }
    
    0 讨论(0)
  • 2020-12-15 13:18

    You can add a custom button as right view of the UITextField like this

    class CustomTextField : UITextField
    {
        override init(frame: CGRect) {
            super.init(frame: frame)
    
            let clearButton = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 15, height: 15))
            clearButton.setImage(UIImage(named: "clear.png")!, forState: UIControlState.Normal)
    
            self.rightView = clearButton
            clearButton.addTarget(self, action: "clearClicked:", forControlEvents: .touchUpInside)
    
            self.clearButtonMode = .never
            self.rightViewMode = .always
        }
    
        func clearClicked(sender: UIButton)
        {
            self.text = ""
        }
    
        required init(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
        }
    }
    
    0 讨论(0)
  • 2020-12-15 13:18

    Implementing a custom text field as suggested in the other answers is not a good idea. You should try to use extensions rather than inheritance if at all possible, because with inheritance you are much more likely to need to make major changes to your codebase in response to changes, whereas using extensions you are much more flexible to change.

    I strongly suggest that instead of implementing a custom text field, you extend the UITextField class like this:

    extension UITextField {
        func applyCustomClearButton() {
            clearButtonMode = .Never
            rightViewMode   = .WhileEditing
    
            let clearButton = UIButton(frame: CGRectMake(0, 0, 16, 16))
            clearButton.setImage(UIImage(name: "iCFieldClear")!, forState: .Normal)
            clearButton.addTarget(self, action: "clearClicked:", forControlEvents: .TouchUpInside)
    
            rightView = clearButton
        }
    
        func clearClicked(sender:UIButton) {
            text = ""
        }
    }
    

    Then to use it you just do this:

    yourTextField.applyCustomClearButton()
    
    0 讨论(0)
提交回复
热议问题