Search code examples
iosobjective-cuikituitextfield

Position of rightView UITextField


Is there a way to adjust the position of a rightView on UITextField? I tried setting the frame on the view (set as rightView) but it didn't change anything.

I'd like to avoid making two views, one as the rightView and one as the rightView's subview where I change the subview's position, if possible.


Solution

  • The right overlay view is placed in the rectangle returned by the rightViewRectForBounds: method of the receiver.

    So I suggest you subclass UITextField and override this method, something like this:

    @interface CustomTextField: UITextField
    
    @end
    
    @implementation CustomTextField
    
    // override rightViewRectForBounds method:
    - (CGRect)rightViewRectForBounds:(CGRect)bounds{
        CGRect rightBounds = CGRectMake(bounds.origin.x + 10, 0, 30, 44);
        return rightBounds ;
    }