xcode UITextField rightView 中的图像未出现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5411837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Image in UITextField rightView not appearing
提问by Lance
I have this code to add a 26 x 30 .png file into the right view of a UITextField.
我有这个代码将一个 26 x 30 .png 文件添加到 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.
有任何想法吗?可能是图像对于文本字段来说太大了吗?我的文本字段是我从 IB 拖入的标准大小,所以我不认为是这样,但这是一个想法。
the text field is 245 x 31
文本字段为 245 x 31
回答by mmoris
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 时指定框架而不是使用类方法 buttonWithType。我猜没有它,文本字段无法确定按钮的大小。
[[UIButton alloc] initWithFrame:CGRectMake(0, 0, width, height)]