1

So I have a custom clearButton for my generic UITextField. I am trying to move the clearButton as the background of my uitextfield has some design stuff on the right side. I need the clear button to be pushed over about 150 pixels.

I tried all combinations of this but it ends up pushing the clearbutton off screen or makes the clear button really wide so the text in the uitextfield stops way to short.

1st try

UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 50, 0, 0);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];

2nd try

UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 250, 0, 150);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];
jdog
  • 10,351
  • 29
  • 90
  • 165

1 Answers1

3

A better option bight be to subclass UITextField and override the clearButtonRectForBounds: method.

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect rect = [super clearButtonRectForBounds:bounds];
    rect.origin.x -= 150;

    return rect;
}

That will move the clear button to the left 150 points from its normal position.

rmaddy
  • 314,917
  • 42
  • 532
  • 579