扫码关注官方订阅号
给textfield添加了 rightview 效果如下
问题是我怎么设置,把?图片的位置靠左边移动一点,不要紧贴住左边?
人生最曼妙的风景,竟是内心的淡定与从容!
你再做个底层的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];
两个办法:
写一个类继承UITextField,重写这个方法:
UITextField
- (CGRect) rightViewRectForBounds:(CGRect)bounds { CGRect textRect = [super rightViewRectForBounds:bounds]; textRect.origin.x -= 10; return textRect; }
然后需要的地方,用你自己写的这个类替换UITextField。
这个方法简单一些:就把那个图片的UIImageView的宽度,设成比原来大20像素,然后加一句:
UIImageView
imageView.contentMode = UIViewContentModeCenter;
就行了。
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
你再做个底层的rightView。把ImageView加到这个rightView上就行了。随意改变位置了。

两个办法:
写一个类继承
UITextField,重写这个方法:然后需要的地方,用你自己写的这个类替换
UITextField。这个方法简单一些:就把那个图片的
UIImageView的宽度,设成比原来大20像素,然后加一句:就行了。