ios - 用于 UITextfield setLeftView 的 xcode UIView ScaleAspectFit

标签 ios objective-c xcode uiimage uitextfield

我想在我的文本字段中有一个图标,并且我正在使用 setLeftView 属性。 我现在的代码是

    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 26, tb_teamA_playerA.bounds.size.height)];
    leftView.contentMode = UIViewContentModeScaleAspectFit;
    [leftView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed : @"person_26" ]]];
    [tb_teamA_playerA setLeftView :leftView];
    [tb_teamA_playerA setLeftViewMode: UITextFieldViewModeAlways];

这就是结果。

enter image description here

最左边的图像是一个单独的 UIImageView,具有相同的图像集和比例设置为 ScaleAspectFit。 所以我希望这两张图片的大小和比例相同,但事实并非如此?!

最佳答案

您正在使用 UIViewContentModeScaleAspectFit,因此图像将适合 UITextField 的大小。 我认为您应该使用 UIViewContentModeScaleAspectFill,如果您对尺寸有疑问并且两个图像的尺寸仍然不同,您应该调整图像的大小。

以下内容来自Doc

UIViewContentModeScaleToFill

Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.

UIViewContentModeScaleAspectFit

Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.

UIViewContentModeScaleAspectFill

Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.

关于ios - 用于 UITextfield setLeftView 的 xcode UIView ScaleAspectFit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26957358/

相关文章:

c++ - NSMutableDictionary 和 std::map 的访问运算符之间的区别

iOS SwiftUI - 从先前的结构调用函数

ios - 无法在 iOS Swift 中导入 UserNotifications

ios - Xcode 10 - iPhone 已拒绝启动请求

ios - 在初始化期间存储到 NSString 的值永远不会被读取

ios - Google Plus API GPPSignIn 错误代码 "Mismatched authentication"

ios - 如何在 ios swift 中以编程方式将 Storyboard View Controller 加载到标签栏 Controller 中?

ios - Pod init 错误工具 'xcodebuild' 需要 Xcode

ios - xcode - MPNowPlayingInfoCenter 信息不显示在 iOS 8 上

iphone - Applescript Xcode 用于在 iPhone 设备上清理、构建和安装