需要向文本框添加一个向下的箭头。决定使用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"];
结果:
此方法非常适合向左侧添加填充。它几乎就像 rightPaddingView 在第一个文本框中用完一样....../boggle
最佳答案
你不能将相同的 UIImageView 添加到两个文本字段,一个 UIView 一次只能在一个 View 中,所以你应该创建你的 imageview 的各种实例,只要你想要文本字段,然后像这样分配它。
关于iphone - UITextField 中的图像仅显示在第一个文本框中,而不显示在第二个文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388832/