risposta di @Puneet Sharma è stato grande, ma che darebbe la necessità di creare una classe che sarebbe sottoclasse UITextField
, quello che ho fatto invece è stato creare un UIView che avrebbe funzionato come un padding.
mio codice funziona senza la necessità di creare una sottoclasse
Ecco il mio codice, anche se è scritto in Swift 3
// this is the view I want to see on the rightView
let checkImageView = UIImageView(image: UIImage(named: "check24.png"))
checkImageView.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
checkImageView.curveEdges(12)
checkImageView.contentMode = .scaleAspectFit
// declare how much padding I want to have
let padding: CGFloat = 6
// create the view that would act as the padding
let rightView = UIView(frame: CGRect(
x: 0, y: 0, // keep this as 0, 0
width: checkImageView.frame.width + padding, // add the padding
height: checkImageView.frame.height))
rightView.addSubview(checkImageView)
// set the rightView UIView as the textField's rightView
self.textField.rightViewMode = .whileEditing
self.textField.rightView = rightView
Che cosa è successo qui è, che la rightView
che è un UIView
che ha uno sfondo colorato trasparente che poi ha dato l'illusione che ci sia un padding mentre non lo è.
fonte
2017-07-04 09:46:22
Hai provato '[textField.rightView setFrame: someFrame];'? O forse 'setBounds:'. – Sti
È necessario prendere 2 visualizzazioni per quello. –