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

前端 未结 7 836
盖世英雄少女心
盖世英雄少女心 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:26

    For Swift4

    func setupUI() {
       let imgSearch = UIImageView();
       let imgSearch = UIImage(named: "search");
    
       // set frame on image before adding it to the uitextfield
       imgSearch.image = imagePassword;
       imgSearch.frame = CGRect(x: txtSearchField.frame.size.width - 40 , y: 5, width: 22, height: 22)
       txtSearchField.rightView = imgSearch
       txtSearchField.rightViewMode = .always
    }
    

    Call function like this

     // MARK: View lifecycle
        override func viewDidLoad() {
            super.viewDidLoad()
            self.setupUI()
        }
    

提交回复
热议问题