ios - 如何在 iOS 7 的 UITextField 中添加带缩进的图像 UIImage?

标签 ios objective-c iphone uiimage uitextfield

我有一个 UITextField,我想在其中的左侧添加一个图像。我使用在 UITextFieldleftView 属性中设置的 UIImageView 来执行此操作。图像在 ImageView 中调整到合适的大小。问题是它坚持边界。如何设置图像的左缩进。我想一种解决方案是在图像的左侧设置一些空白区域,但我想使用缩进。另外我想在图像后缩进一些,这样文本就不会太靠近它。

我使用以下代码:

[textField setLeftViewMode:UITextFieldViewModeAlways];

UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 37, 20)];
imageView1.image = [UIImage imageNamed:@"custom_image"];
textField.leftView = imageView1;

最佳答案

您可以将 UIImageview 框架设置为比原始 UIImage 多,并将 UIImageview contentMode 属性设置为居中。 请引用以下答案。

UIImageView *imgSearch=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 32, 28)]; // Set frame as per space required around icon
[imgSearch setImage:[UIImage imageNamed:@"search.png"]];

[imgSearch setContentMode:UIViewContentModeCenter];// Set content mode centre

self.tfSearch.leftView=imgSearch;
self.tfSearch.leftViewMode=UITextFieldViewModeAlways;

关于ios - 如何在 iOS 7 的 UITextField 中添加带缩进的图像 UIImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992741/

相关文章:

ios - avplayer 不在 IOS 中播放音频

ios - 如何使用 mac-catalyst 应用程序的 CloudKit 数据库订阅调试 "Error acquiring assertion"?

ios - 具有 MVC 模式的遗留 Objective C 代码是否与 RxSwift 共存?

iphone - 从多个变量类型中创建一个对象

iphone - 如何在当前时间添加时间

ios - 获取设备图像比例(例如@1x、@2x 和@3x)

ios - 是否可以更改 UITableView 中的字体类型和大小?

iphone - 音频流API:Wifi与什么?

objective-c - UIScrollView 仅在底部禁用垂直弹跳

ios - Xcode 7 特定的静态框架警告