How to add padding to left right view in UITextField

Issue #536

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
extension UITextField {
func setLeftView(_ view: UIView, padding: CGFloat) {
view.translatesAutoresizingMaskIntoConstraints = true

let outerView = UIView()
outerView.translatesAutoresizingMaskIntoConstraints = false
outerView.addSubview(view)

outerView.frame = CGRect(
origin: .zero,
size: CGSize(
width: view.frame.size.width + padding,
height: view.frame.size.height + padding
)
)

view.center = CGPoint(
x: outerView.bounds.size.width / 2,
y: outerView.bounds.size.height / 2
)

leftView = outerView
}
}

Comments