Mayank Jain Mayank Jain - 2 months ago 47
Swift Question

Swift 3- How to override a method in subclass

In Swift 3, I want to override caretRectForPosition method of

UITextField
in its subclass say
CustomTextField


Here is my code which is working fine prior to Swift 2.3

import UIKit

class CustomTextField: UITextField {
override func caretRectForPosition(position: UITextPosition) -> CGRect {
return CGRect.zero
}
}


but in Swift 3 its showing a compile time error Method does not override any method from its superclass.

Can any one help me out on this.

I am using this code to hide the cursor of text field.

Thanks in advance.

Answer

In Swift 3 caretRectForPosition(position:) is changed to caretRect(for:).

override func caretRect(for position: UITextPosition) -> CGRect {
    return CGRect.zero
}

Check Apple Documentation for more detail on UITextInput.

Comments