ios - UITextfield rightview 的位置问题
黄舟
黄舟 2017-04-17 14:56:35
[iOS讨论组]

给textfield添加了 rightview 效果如下

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(2)
PHP中文网

你再做个底层的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];

怪我咯

两个办法:

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

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

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

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

    imageView.contentMode = UIViewContentModeCenter;
    

    就行了。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号