Add a button on right view of UItextfield in such way that, text should not overlap the button

前端 未结 7 923
盖世英雄少女心
盖世英雄少女心 2021-01-31 09:07

I can add a button to a textfield on the right hand side of the UITextField using the right view however, the text overlaps on the button. Below is the code for right view butto

7条回答
  •  悲哀的现实
    2021-01-31 09:11

    use the rightView property of the UITextField. Basically, there are two properties (this one and leftView accordingly) that allow you to place custom views/controls inside the UITextField. You can control whether those views are shown or not by means of rightViewMode/leftViewMode properties:

    textField.rightView = btnColor
    textField.rightViewMode = .unlessEditing
    

    for e.g

    let button = UIButton(type: .custom)
    button.setImage(UIImage(named: "send.png"), for: .normal)
    button.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0)
    button.frame = CGRect(x: CGFloat(txt.frame.size.width - 25), y: CGFloat(5), width: CGFloat(25), height: CGFloat(25))
    button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
    textField.rightView = button
    textField.rightViewMode = .always
    

    and call the action as

    @IBAction func refresh(_ sender: Any) {
    }
    

提交回复
热议问题