iphone - UITextField 中的图像仅显示在第一个文本框中,而不显示在第二个文本框中

标签 iphone objective-c ios

需要向文本框添加一个向下的箭头。决定使用rightViewMode来显示它。它适用于我使用的第一个文本字段,但在第二个文本字段中不显示。它发生在我需要执行此操作的 3 个不同 View 中。

代码:

//added to post
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 30)];

UIView *rightPaddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 20)];
UIImageView *unitArrowImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"prev-arrow.png"]];
unitArrowImage.frame = CGRectMake(0, 0, 20, 20);
[unitArrowImage setTransform:CGAffineTransformMakeRotation(-M_PI / 2)];
[rightPaddingView addSubview:unitArrowImage];

//other code

UITextField *seedRateUnitTextField = [[UITextField alloc] initWithFrame:myRect];
seedRateUnitTextField.font = [UIFont fontWithName:textFieldFont size:textFieldFontSize];
seedRateUnitTextField.placeholder = @"Unit";
seedRateUnitTextField.borderStyle = UITextBorderStyleLine;
seedRateUnitTextField.backgroundColor = [UIColor whiteColor];
seedRateUnitTextField.textColor = [UIColor textFieldTextColor];
seedRateUnitTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
seedRateUnitTextField.layer.borderColor = [[UIColor textfieldBorderColor]CGColor];
seedRateUnitTextField.layer.borderWidth = 1.0f;
seedRateUnitTextField.leftView = paddingView;
seedRateUnitTextField.leftViewMode = UITextFieldViewModeAlways;
seedRateUnitTextField.rightView = rightPaddingView;
seedRateUnitTextField.rightViewMode = UITextFieldViewModeAlways;
seedRateUnitTextField.delegate = self;
seedRateUnitTextField.tag = 0;
[myScrollView addSubview:seedRateUnitTextField];
[self.dataSetDictionary setValue:seedRateUnitTextField forKey:@"seedRateUnitData"];

myRect = CGRectMake(200, ((4 * 40) + 140), 170, 30);

UITextField *plantingDepthTextField = [[UITextField alloc] initWithFrame:myRect];
plantingDepthTextField.font = [UIFont fontWithName:textFieldFont size:textFieldFontSize];
plantingDepthTextField.placeholder = @"Enter Planting Depth";
plantingDepthTextField.borderStyle = UITextBorderStyleLine;
plantingDepthTextField.backgroundColor = [UIColor whiteColor];
plantingDepthTextField.textColor = [UIColor textFieldTextColor];
plantingDepthTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
plantingDepthTextField.layer.borderColor = [[UIColor textfieldBorderColor]CGColor];
plantingDepthTextField.layer.borderWidth = 1.0f;
plantingDepthTextField.leftView = paddingView;
plantingDepthTextField.leftViewMode = UITextFieldViewModeAlways;
plantingDepthTextField.delegate = self;
plantingDepthTextField.tag = 0;
[myScrollView addSubview:plantingDepthTextField];
[self.dataSetDictionary setValue:plantingDepthTextField forKey:@"plantingDepthData"];

myRect = CGRectMake(375, ((4 * 40) + 140), 85, 30);

UITextField *plantingDepthUnitTextField = [[UITextField alloc] initWithFrame:myRect];
plantingDepthUnitTextField.font = [UIFont fontWithName:textFieldFont size:textFieldFontSize];
plantingDepthUnitTextField.placeholder = @"Unit";
plantingDepthUnitTextField.borderStyle = UITextBorderStyleLine;
plantingDepthUnitTextField.backgroundColor = [UIColor whiteColor];
plantingDepthUnitTextField.textColor = [UIColor textFieldTextColor];
plantingDepthUnitTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
plantingDepthUnitTextField.layer.borderColor = [[UIColor textfieldBorderColor]CGColor];
plantingDepthUnitTextField.layer.borderWidth = 1.0f;
plantingDepthUnitTextField.leftView = paddingView;
plantingDepthUnitTextField.leftViewMode = UITextFieldViewModeAlways;
plantingDepthUnitTextField.rightView = rightPaddingView;
plantingDepthUnitTextField.rightViewMode = UITextFieldViewModeAlways;
plantingDepthUnitTextField.delegate = self;
plantingDepthUnitTextField.tag = 0;
[myScrollView addSubview:plantingDepthUnitTextField];
[self.dataSetDictionary setValue:plantingDepthUnitTextField forKey:@"plantingDepthUnitData"];

结果:

result

此方法非常适合向左侧添加填充。它几乎就像 rightPaddingView 在第一个文本框中用完一样....../boggle

最佳答案

你不能将相同的 UIImageView 添加到两个文本字段,一个 UIView 一次只能在一个 View 中,所以你应该创建你的 imageview 的各种实例,只要你想要文本字段,然后像这样分配它。

关于iphone - UITextField 中的图像仅显示在第一个文本框中,而不显示在第二个文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388832/

相关文章:

ios - FB iOS SDK 3.2 : Why do I need an access token for logging a conversion pixel?

ios - 企业发行版“无法下载应用程序”在iPhone 4上但在iPhone5上有效

iphone - Iphone 7 和 7 plus 的 CSS 媒体查询

iphone - 您能否向移动设备提供 HTML 输入字段仅为数字的提示?

ios - dismissViewControllerAnimated : does not work

ios - 在应用程序购买中,以编程方式禁用自动更新

IOS如何彻底删除一个View

iphone - 在 UIWebView 中打开的 HTML 页面中自动填充用户名和密码

objective-c - 比较 Objective-C 与 Swift 数组和 NOT 运算符

ios - 带有 iOS 11.1.2 的 iphone 上的 UIDocumentInteractionController