Iphone 具有自定义rightView的UITextField中的宽度问题
我有一个Iphone 具有自定义rightView的UITextField中的宽度问题,iphone,xcode,uitextfield,Iphone,Xcode,Uitextfield,我有一个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 = UIViewAutoresi
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
。我验证了它们是否被调用并返回正确的值,但它们不会更改任何内容(只要返回值正确)
有什么想法吗 是否为TableViewCell设置了自动调整大小属性?尝试添加:
[cell setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
文本字段和按钮设置UIViewAutoResisingFlexibleTopMargin
选项如下:
[textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
[button setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin];
我在rightView中遇到了类似的问题,它似乎阻止了textfield的一部分接受输入。我的rightView不是一个按钮,所以我只是简单地将它的userInteractionEnabled设置为NO,然后文本字段开始在其整个区域再次接收触摸
由于您有一个按钮作为右视图,这可能没有多大帮助。但我们的例子之间的另一个相似之处是,两个右视图的宽度都很小。也许rightView不喜欢宽度小于x 没有必要为UITableViewCell设置autoResizingMask,它继承自UITableView(我确实尝试过,没有更改)。UITextField已经有一个自动调整大小的掩码(请参见代码),并且按钮不应该调整大小,因为它是基于图像的按钮。宾果!成功了。我一直在试图弄清楚我是如何阻止输入的(我使用的是leftView和leftViewMode)。在我的填充视图上设置userInteractionEnabled似乎可以解决这个问题。谢谢你解决过这个问题吗?我现在也有同样的问题。