【问题标题】:UITextField "clearButtonRectForBounds" method not callingUITextField“clearButtonRectForBounds”方法未调用
【发布时间】:2014-06-13 07:54:19
【问题描述】:

我正在使用以下代码创建一个具有清除按钮模式的文本字段。

  self.TextField =[[UITextField alloc]initWithFrame:CGRectMake(30, 0, 200, 30)];
    self.TextField.tintColor =[UIColor lightGrayColor];
    self.TextField.returnKeyType =UIReturnKeyGo;
    self.TextField.clearButtonMode =UITextFieldViewModeWhileEditing;
    self.TextField.delegate=self;
    [self.view addSubview:self.TextField];

- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.origin.x - 10, bounds.origin.y, bounds.size.width, bounds.size.height+30);
}

我想为清除按钮设置自定义框架,为此我试图调用“clearButtonRectForBounds”。但是“clearButtonRectForBounds”方法没有调用。如何设置清除按钮的框架?

【问题讨论】:

    标签: ios iphone uitextfield


    【解决方案1】:

    您需要子类UITextField 并且需要覆盖-(CGRect)clearButtonRectForBounds:(CGRect)bounds 方法,如@Luda 在此答案中所述,https://stackoverflow.com/a/14870633/1603234

    【讨论】:

    • 我有一个 UIViewController 子类。在那个类中我有一个 TextField
    • 好吧,但是你必须创建一个 UITextField 的子类,然后在你的 UIViewController 中使用。
    猜你喜欢
    • 2011-10-29
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2021-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多