2016-02-29 36 views
1

我有一个UITextField我在rightView添加图像。我已经为此rightView添加了一个手势,并且当UITextField被禁用时,我想让它工作,因为只有在未编辑时才显示此视图。当UITextField被禁用时如何在leftView/rightView中启用tap?

这里是什么,我想达到的图像样本: 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被禁用时,如何在左侧视图/右侧视图中启用水龙头?

+0

嗨请把一些代码或图像以便更好地理解... – ilesh

+0

@ilesh编辑了更多细节的问题。 – mxmlc

回答

1

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

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

然后,我实现了textFieldShouldBeginEditing单击此UITextField何时返回的isEditEnabled价值,实现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; 
} 

这样我有左侧视图和右侧视图总是启用轻拍,这是我的目标。

0

我建议这样的:

enter image description here

(如果你需要这样或UIImage的),你可以在一个视图中嵌入您的文本字段和一个UIButton并设置颜色和圆角半径您查看

相关问题