当UITextField被禁用时,如何在leftView / rightView中启用点按?

时间:2016-02-29 13:53:42

标签: ios objective-c uitextfield uitapgesturerecognizer

我有一个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中启用点击?

2 个答案:

答案 0 :(得分:2)

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

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

然后,当我点击textFieldShouldBeginEditing时,我已实施isEditEnabled以返回UITextField的值,已实施textFieldDidBeginEditing以将textField的backgroundColor设置为白色并实现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来实现我的目标。

答案 1 :(得分:1)

我会建议:

enter image description here

您可以嵌入查看TextField和UIButton(或UIImage,如果需要)并将颜色和角半径设置为视图