I have a custom font in a UITextField, and I\'ve noticed that when it\'s accessed (when the keyboard appears), the text shifts down by a very small amount -- maybe
Unfortunately none of the answers worked for me.
@blackjacx answer worked but only sometimes :(
I started out debugging and here is what I've discovered:
1 - The real problem seems to be with a private subview of UITextField of type UIFieldEditorContentView
Below you can see that the y
of it subview is not the same of the UITextField
itself:
After realizing it I came out with the following workaround:
override func layoutSubviews() {
super.layoutSubviews()
fixMisplacedEditorContentView()
}
func fixMisplacedEditorContentView() {
if #available(iOS 10, *) {
for view in subviews {
if view.bounds.origin.y < 0 {
view.bounds.origin = CGPoint(x: view.bounds.origin.x, y: 0)
}
}
}
}
You will need to subclass UITextField
and override layoutSubviews
to add the ability to manually set to 0
the y
of any subview that is set to a negative value. As this problem doesn't occur with iOS 9 our below I added a check to do the workaround only when it is on iOS 10.
The result you can see below:
2 - This workaround doesn't work if the user choose to select a subrange of the text (selectAll works fine)
Since the selection of the text is not a must have for my app I rather disable it. In order to do that you can use the following code (Swift 3):
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if #available(iOS 10, *) {
if action == #selector(UIResponderStandardEditActions.select(_:)) {
return false
}
}
return super.canPerformAction(action, withSender: sender)
}