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似乎可以解决这个问题。谢谢你解决过这个问题吗?我现在也有同样的问题。