Search code examples
objective-ctextfielduser-interaction

textfield userinteractionenabled disable also input obj c


I have a textfield with left and right view, I need to disable the touch on the text but not on the right view which allows me to delete the character entered with the custom keyboard. for this I have to disable input only of textfiel .. is it possible?

example


Solution

  • Sure it is. Some ideas:

    1. Add a new UIView subview to your textview on top covering the part that's meant to "ignore" user touches. Set its background to clearColor so that the text view is actually visible (during debugging a solid color like red may be actually helpful before you're happy with the final size & positioning). A small drawback of this approach is frames are rectangles, so area above and below the button would still accept user input (that may or may not make a difference in your case).
    2. The other way round. Put the textfield view in a container view (also background color set to clear color), make the container frame smaller then the textview. Set the origin.x of the textfield frame to negative number. Set container view clip to bounds to NO.Eventually you want the container view to match your back button and position textview accordingly. You need also need passthrough touch events to textfield:

    in container view from 2)

    -(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
            return NO;
    }
    

    Now only your area of the text view within container view will accept touches.