我使用以下代码以编程方式创建了一个UITextField:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
我遇到的问题是我的UITextField最终在边缘上有这些奇怪的黑色像素。这在设备和模拟器中都会发生。您可以在下面的屏幕截图中看到:
当我使用IB创建具有相同规格和背景的相同UITextField时,我没有问题。不幸的是,我需要以编程方式创建此UITextField。
有人看过吗?该怎么办?
最佳答案
似乎这是在屏幕上绘制文本字段的方式。我稍微玩了一下您的代码,如果我将文本字段的高度设置为低于20左右,您就会开始看到明显的“阴影”,显然没有正确绘制。
我的建议是为文本字段使用20或更高的高度,或者使用其他样式(例如“边框”或“线条”)并将背景设置为白色。
这是演示的屏幕截图:
这是我用来绘制这些代码的代码:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
希望这可以帮助。
Mk
关于iphone - UITextField边缘的黑色像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5249615/