galenom galenom - 2 months ago 18
Swift Question

Swift - Disable text shifting when clear button appears

I have a UITextField in my project with the clear button enabled during editing.

Any centered text inside the box will shift to the left to make room for the clear button on focus. Is it possible for me to disable this shifting. I feel that it is distracting.

Please see the screenshots below:
enter image description here
enter image description here

Answer

To fix the issue I subclassed UITextField, and added the following to the subclass.

override func editingRectForBounds(bounds: CGRect) -> CGRect { let padding = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5); return UIEdgeInsetsInsetRect(bounds, padding) }

Answer was found here: Create space at the beginning of a UITextField