本文介绍了如何在UITextField leftView图像上获得左边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITextField上设置 UIImageView 作为 leftView 喜欢这样:

I am setting up a UIImageView as a leftView on a UITextField like so:

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;

给我以下信息:

正如您所见,即使我尝试设置,图像的左侧大小也会一直向上移动到按钮的左边缘插图。如何移动这个信封,使其四面都有填充?

As you can see the left size of the image goes right up to the left edge of the button even though I tried to set an inset. How can I move this envelope in so that it's got padding on all sides?

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

Update: I tried the proposed answer of changing the UIImageView frame like so, but the envelope is still lined up on the left side at the border of the UITextField:

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


推荐答案

你可以试试这个:

    UIImageView *envelopeView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 0, 30, 30)];
    envelopeView.image = [UIImage imageNamed:@"comment-128.png"];
    envelopeView.contentMode = UIViewContentModeScaleAspectFit;
  UIView *test=  [[UIView alloc]initWithFrame:CGRectMake(20, 0, 30, 30)];
    [test addSubview:envelopeView];
    [self.textField.leftView setFrame:envelopeView.frame];
    self.textField.leftView =test;
        self.textField.leftViewMode = UITextFieldViewModeAlways;

这篇关于如何在UITextField leftView图像上获得左边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:11