如何在禁用 UITextField 时启用 leftView/rightView 中的点击?

How to enable tap in leftView/rightView when UITextField disabled?

我有一个 UITextField,我在 rightView 中添加了一张图片。我已经为此 rightView 添加了一个手势,我希望在禁用 UITextField 时让它工作,因为此视图仅在不编辑时显示。

这是我想要实现的图像示例:

下面是用于将外观应用到 UITextField 并将图像应用到 rightView 的代码:

- (void)applyAppearanceToTextField:(UITextField *)textField withRoundingCorner:(UIRectCorner)corner withIcon:(NSString *)icon {
    CAShapeLayer *shape = [[CAShapeLayer alloc] initWithLayer:textField.layer.mask];
    UIBezierPath *shadow = [UIBezierPath bezierPathWithRoundedRect:textField.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(5, 5)];
    shape.path = shadow.CGPath;
    textField.layer.mask = shape;

    // Apply space to the left as a padding
    [textField setLeftView:[[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, textField.bounds.size.height)]];
    [textField setLeftViewMode:UITextFieldViewModeAlways];

    // Add the icon image to the right view
    CGFloat height = textField.bounds.size.height;
    UIImage *image = [UIImage imageWithIcon:icon backgroundColor:[UIColor clearColor] iconColor:[UIColor grayColor] andSize:CGSizeMake(height / 2, height / 2)];
    image = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:UIImageOrientationUpMirrored];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [imageView setBounds:CGRectMake(5, 0, image.size.width, image.size.width)];
    [imageView setUserInteractionEnabled:YES];
    [textField setRightView:imageView];
    [textField setRightViewMode:UITextFieldViewModeUnlessEditing];
    [textField.rightView setExclusiveTouch:YES];

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleIconTap:)];
    [textField.rightView addGestureRecognizer:tapGesture];
}

- (void)handleIconTap:(UITapGestureRecognizer *)gesture {
    NSLog(@"handleIconTap");
}

viewDidLoad 中,我检查该字段是否已填写,因此该字段已禁用。当 UITextField 被禁用时,如何在 leftView/rightView 中启用水龙头?

我建议这样做:

您可以在 View 中嵌入您的 TextField 和 UIButton(或 UIImage,如果需要的话)并为您的 View 设置颜色和圆角半径

我使用默认的 UITextField 组件解决了这个问题。我创建了一个布尔值 属性 来处理值,如果字段应该被启用,它被设置在 viewDidLoad:

isEditEnabled = (self.myTextField.text != nil && self.myTextField.text.lenght > 0);

然后,我将 textFieldShouldBeginEditing 实现为 return 单击此 UITextFieldisEditEnabled 的值,实现 textFieldDidBeginEditing 以设置背景颜色将 textField 设置为白色,并实施 textFieldDidEndEditing 以将 backgroundColor 设置为深灰色,如果 myTextField 已填充,则设置 isEditEnabled = NO。代码如下:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField == self.myTextField) {
        return isEditEnabled;
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    textField.backgroundColor = [UIColor whiteColor];
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField.text != nil && textField.text.length > 0) {
        if (textField == self.myTextField) {
            textField.backgroundColor = [UIColor colorWithWhite:0.80f alpha:1.0f];
            isEditEnabled = NO;
        }
    } else {
        textField.backgroundColor = [UIColor whiteColor];
        if (textField == self.url) {
            isEditEnabled = YES;
        }
    }
    activeField = nil;
}

通过这种方式,我的目标是始终启用 leftView 和 rightView 进行点击。