Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image in UITextField rightView not appearing

I have this code to add a 26 x 30 .png file into the right view of a UITextField.

//add button to address bar
//UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
UIButton *refreshButton = [UIButton buttonWithType:UIButtonTypeCustom];
[refreshButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateNormal];
[refreshButton setImage:[UIImage imageNamed:@"refresh.png"] forState:UIControlStateHighlighted];
refreshButton.imageEdgeInsets = UIEdgeInsetsMake(0, -16, 0, 0);
[refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
addressBar.rightView = refreshButton;
addressBar.rightViewMode = UITextFieldViewModeUnlessEditing;

the button is touchable and calls the method that it should, but the image is not displayed.

any ideas? could it be that the image is too big for the text field? My text field is the standard size that I dragged into from IB, so I don't think that's it, but it's an idea.

the text field is 245 x 31

like image 870
Lance Avatar asked Mar 23 '11 21:03

Lance


1 Answers

Have you tried specifying a frame when initializing your UIButton instead of using the class method buttonWithType. I am guessing the textfield cannot determine the size of your button without it.

[[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)]
like image 170
mmoris Avatar answered Oct 23 '22 12:10

mmoris