【问题标题】:UITextField clearButton keeps returning wrong imageUITextField clearButton 不断返回错误的图像
【发布时间】:2014-09-14 02:54:44
【问题描述】:

我正在尝试访问 UITextField 的 UIImage。但它不断返回放大镜而不是 x 标记。我究竟做错了什么?

UIButton *clearButton = [searchField valueForKey:@"_clearButton"];
UIImage *clearImage = clearButton.imageView.image;//[clearButton imageForState:UIControlState];

我知道 x 标记在那里,因为它总是在我开始在文本字段中输入时出现。

【问题讨论】:

    标签: ios uibutton uiimage xcode5 uitextfield


    【解决方案1】:
    self.tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 64)];
    self.tf.clearButtonMode = UITextFieldViewModeAlways; //maybe you need do this    
    
    UIButton *clearButton = [self.tf valueForKey:@"_clearButton"];
    UIImage *clearImage = clearButton.imageView.image;
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:clearImage];
    imageView.frame = CGRectMake(100, 100, 100, 100);
    [self.view addSubview:imageView];
    

    【讨论】:

    • 事实证明我的代码是正确的。程序的其他地方有错字。但你的也可以。
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-18
    • 2016-11-15
    • 2023-03-11
    • 2012-03-20
    相关资源
    最近更新 更多