gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:56:31 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com