我有一个UITextField
,在其中我在rightView
中添加了一张图片。我已经为这个rightView
添加了手势,但我希望在UITextField
被禁用时也能起作用,因为只有在非编辑状态下才显示此视图。
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 上的点击操作?