当UITextField被禁用时,如何启用左视图/右视图上的轻敲功能?

3

我有一个UITextField,在其中我在rightView中添加了一张图片。我已经为这个rightView添加了手势,但我希望在UITextField被禁用时也能起作用,因为只有在非编辑状态下才显示此视图。

以下是我想要实现的示例图片: Webservice address

  • leftView只是一个空白区域;
  • rightView只有图片。

以下是用于应用外观到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 上的点击操作?


嗨,请放置一些代码或图像以获得更好的理解... - Ilesh P
@ilesh编辑了问题并提供了更多细节。 - mxmlc
2个回答

2

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

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

接下来,我已经实现了textFieldShouldBeginEditing方法,以便在单击此UITextField时返回isEditEnabled的值;实现textFieldDidBeginEditing方法将textField背景颜色设置为白色;实现textFieldDidEndEditing方法将背景颜色设置为深灰色,并且如果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进行轻击,这是我的目标。

2
我建议这样做:

enter image description here

您可以在视图中嵌入TextField和UIButton(或UIImage,如果需要),并将颜色和圆角设置为您的视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接