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

前端 未结 7 824
盖世英雄少女心
盖世英雄少女心 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:35
    internal var btnDropDown: UIButton {
        let size: CGFloat = 25.0
        let button = UIButton(type: .custom)
        button.setImage(#imageLiteral(resourceName: "DropDownImage"), for: .normal)
        button.imageEdgeInsets = UIEdgeInsetsMake(0, -(size/2.0), 0, 0)
        button.frame = CGRect(x: self.frame.size.width - size, y: 0.0, width: size, height: size)
        return button
    }
    

    And I used it like this:

    override func awakeFromNib() {
        super.awakeFromNib()   
        textField.rightView = self.btnDropDown
        textField.rightViewMode = .always
    }
    
    0 讨论(0)
提交回复
热议问题