ios - 尝试移动我的自定义UITextField清除按钮,但无法调整大小或正确移动

标签 ios objective-c uitextfield uiedgeinsets

因此,我为通用UITextField有一个自定义的clearButton。我正在尝试将clearButton移至uitextfield的背景,右侧具有一些设计内容。我需要将清除按钮推到约150像素以上。

我尝试了所有这些组合,但是最终将clearbutton推离屏幕,或者使clear按钮真正变宽,因此uitextfield中的文本停止变短。

第一次尝试

UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 50, 0, 0);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];

第二次尝试
UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeCustom];

CGRect rect;
UIEdgeInsets contentInsets;
CGRect result;
[clearBtn setImage:[UIImage imageNamed:@"design.png"] forState:UIControlStateNormal];
rect = CGRectMake(0, 0, 45, 45);
contentInsets = UIEdgeInsetsMake(0, 250, 0, 150);
result = UIEdgeInsetsInsetRect(rect, contentInsets);
[clearBtn setFrame:result];

最佳答案

一个更好的选择是子类UITextField并重写clearButtonRectForBounds:方法。

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect rect = [super clearButtonRectForBounds:bounds];
    rect.origin.x -= 150;

    return rect;
}

这会将清除按钮从其正常位置向左移动150点。

关于ios - 尝试移动我的自定义UITextField清除按钮,但无法调整大小或正确移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129024/

相关文章:

ios - 使用 Xcode 4 部署到 iOS 3.1.3 设备

ios - NSURLCredentialPersistenceForSession : What is meant by session here?

ios - UITextField 和 UILabel 的奇怪比较

ios - 当我将 UIView 向下转换为 SKView 时,出现异常

iphone - 没有缓存的 UIWebview

objective-c - 是否可以将 NSAttributedString 应用于 CPTTextLayer 的文本

ios - UIKit Dynamics collision - 保持 Barriers 元素静止

iphone - Swift:下一个按钮的链文本字段

ios - 如何在用户编辑 UITEXTFIELD 时自动插入字符串

ios - 我无法暂停在 tablecell 上播放视频