Mayank Jain
Mayank Jain

Reputation: 5754

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.

Upvotes: 0

Views: 2149

Answers (1)

Nirav D
Nirav D

Reputation: 72410

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.

Upvotes: 6

Related Questions