gpt4 book ai didi

ios - 如何在 UITextField leftView 图像上获得左填充?

转载 作者:可可西里 更新时间:2023-11-01 04:17:21 26 4
gpt4 key购买 nike

我正在将 UIImageView 设置为 UITextField 上的 leftView,如下所示:

UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.height*.1, self.height*.1)];
envelopeView.image = [UIImage imageNamed:@"envelope.png"];
envelopeView.contentMode = UIViewContentModeScaleAspectFit;
envelopeView.bounds = CGRectInset(envelopeView.frame, 15, 10);
self.emailAddress.leftView = envelopeView;
self.emailAddress.leftViewMode = UITextFieldViewModeAlways;

这让我得到以下信息:

UITextField image

如您所见,即使我尝试设置插图,图像的左侧尺寸也会一直延伸到按钮的左边缘。我怎样才能把这个信封移进去,让它四面都有填充物?


更新:我尝试了像这样更改 UIImageView 框架的建议答案,但信封仍然排在 UITextField 边界的左侧:

CGFloat padding = 20;
UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(3*padding, padding, self.height*.1-padding, self.height*.1-padding)];

最佳答案

对于 Swift 3 用户

这是对我有用的:

extension UITextField {

/// set icon of 20x20 with left padding of 8px
func setLeftIcon(_ icon: UIImage) {

let padding = 8
let size = 20

let outerView = UIView(frame: CGRect(x: 0, y: 0, width: size+padding, height: size) )
let iconView = UIImageView(frame: CGRect(x: padding, y: 0, width: size, height: size))
iconView.image = icon
outerView.addSubview(iconView)

leftView = outerView
leftViewMode = .always
}
}

测试:

txOrigin.setLeftIcon(icon_location)

结果:

enter image description here

关于ios - 如何在 UITextField leftView 图像上获得左填充?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32525006/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com