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

标签 ios objective-c uitextfield uitapgesturerecognizer

我有一个 UITextField,我在 rightView 中添加了一个图像。我已经向这个 rightView 添加了一个手势,我想让它在 UITextField 被禁用时工作,因为这个 View 只在不编辑时显示。

这是我想要实现的图像示例: 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 中启用点击?

最佳答案

我建议这样做:

enter image description here

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

关于ios - 禁用 UITextField 时如何在 leftView/rightView 中启用点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35701355/

相关文章:

ios - 需要在启用 ARC 的情况下使用 Leaks 进行分析吗?

iphone - 如何从一个 View 复制一个字符串并在下一个 View 中显示它?(iphone)

ios - MKAnnotationView 自定义按钮图片

ios - 将输入到 UITextField 中的内容转换为计数器编号,例如 00 :01

ios - 如何在 UITextField 中为每 3 位数字添加逗号?

ios - 向 UINavigationItem 添加按钮根本不起作用

ios - 在 iOS 中将 self.navigationItem.backBarButtonItem 对齐到设备的左侧

ios - 将字符串日期转换为 NSDate

iphone - Mobile Safari - iPhone 上的文章、文本和图像模糊

IOS:文本字段和双值