【发布时间】:2023-03-07 02:02:02
【问题描述】:
我有一个UITextField(放在UITableViewCell 内),它有一个UIButton 作为自定义rightView:
UITextField *textField = [[[UITextField alloc] initWithFrame:CGRectMake(10.0, 1.0, cell.bounds.size.width - 20.0, 31.0)] autorelease];
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake(0.0, 0.0, 27.0, 31.0); // Probably not needed, doesn't change anything
button.frame = CGRectMake(0.0, 0.0, 27.0, 31.0);
[button setImage:[UIImage imageNamed:@"27x31.png"] forState:UIControlStateNormal];
textField.rightView = button;
textField.rightViewMode = UITextFieldViewModeUnlessEditing;
显示文本字段时,触摸未按预期处理:
- 如果我触摸按钮,它就会被触发。
- 如果我触摸靠近按钮左边缘的区域(但在其边界之外),按钮仍会被触发。
- 如果我想编辑文本字段中的文本而不是触发按钮,我必须进一步触摸左侧。
如果我将设备旋转到横向模式,情况会变得更糟。在这种情况下,仍然触发按钮的按钮左侧区域更宽,并且该区域左侧有一个空白,没有任何作用 - 按钮未触发且文本未编辑。在这种情况下,我必须向左移动一段相当长的距离来编辑文本。
仅当字段中的当前文本很短时才会发生这种情况。如果文本填满文本字段,则按预期处理触摸(触摸按钮左侧的任意位置以纵向和横向编辑文本)。
我试图覆盖textRectForBounds、editingRectForBounds 和rightViewRectForBounds。我验证了它们被调用并返回正确的值,但它们没有改变任何东西(只要返回值正确)。
有什么想法吗?
【问题讨论】:
-
你解决过这个问题吗?我现在遇到了同样的问题。
标签: iphone xcode uitextfield