首页 > UITextfield rightview 的位置问题

UITextfield rightview 的位置问题

给textfield添加了 rightview 效果如下

问题是我怎么设置,把?图片的位置靠左边移动一点,不要紧贴住左边?


两个办法:

  1. 写一个类继承UITextField,重写这个方法:

    - (CGRect) rightViewRectForBounds:(CGRect)bounds {
    
        CGRect textRect = [super rightViewRectForBounds:bounds];
        textRect.origin.x -= 10;
        return textRect;
    }
    

    然后需要的地方,用你自己写的这个类替换UITextField

  2. 这个方法简单一些:就把那个图片的UIImageView的宽度,设成比原来大20像素,然后加一句:

    imageView.contentMode = UIViewContentModeCenter;
    

    就行了。


你再做个底层的rightView。把ImageView加到这个rightView上就行了。随意改变位置了。

    UIView *rightVeiw = [[UIView alloc] initWithFrame:CM(0, 0, 100, 30)];
    rightVeiw.backgroundColor = RedColor;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CM(10, 5, 30, 20)];
    imageView.backgroundColor = OrangeColor;
    [rightVeiw addSubview:imageView];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [imageView addGestureRecognizer:tap];
    //你把tapAction:实现以下,点击,只会触发这个方法。

    UITextField *textedd = [[UITextField alloc] initWithFrame:CM(15, 350, Screen_Width - 30, 40)];
    textedd.font = [UIFont fontWithName:@"Party LET" size:20];
    textedd.placeholder = @"dfsdfdf";
    textedd.textColor = BlackColor;
    textedd.backgroundColor = WhiteColor;
    textedd.rightView = rightVeiw;
    textedd.rightViewMode = UITextFieldViewModeAlways;
    [self.view addSubview:textedd];

【热门文章】
【热门文章】