带有自定义rightView的UITextField中的宽度问题

Ami*_*ark 5 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 = 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;
Run Code Online (Sandbox Code Playgroud)

显示文本字段时,不会按预期处理触摸:

  • 如果我触摸按钮,它会被触发.
  • 如果我触摸靠近按钮左边缘的区域(但在其边界之外),仍然会触发该按钮.
  • 如果我想编辑文本字段中的文本而不是触发按钮,我必须进一步向左触摸.

如果我将设备旋转到横向模式,情况会变得更糟.在这种情况下,按钮左侧仍然触发按钮的区域较宽,并且该区域左侧有一个间隙,它不执行任何操作 - 按钮未被触发且文本未被编辑.在这种情况下,我必须向左移动以编辑文本.

仅当字段中的当前文本很短时才会发生这种情况.如果文本填充文本字段,则按预期处理触摸(触摸按钮左侧任何位置以纵向和横向编辑文本).

我试图覆盖textRectForBounds,editingRectForBounds并且rightViewRectForBounds.我验证它们被调用并返回正确的值,但它们不会改变任何东西(只要返回值是正确的).

有任何想法吗?

Ben*_*ard 5

我有一个类似的问题与rightView似乎阻止文本字段的一部分接受输入.我的rightView不是一个按钮所以我只是将它的userInteractionEnabled设置为NO,文本字段开始在整个区域再次接收触摸.

由于你有一个按钮作为rightView,这可能没有多大帮助.但我们的例子之间的另一个相似之处是两个rightViews的宽度都很小.也许rightView不喜欢宽度<x.


Ale*_*ciu 0

您是否为 TableViewCell 设置了任何自动调整大小属性?尝试添加:

[cell setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
Run Code Online (Sandbox Code Playgroud)

文本字段和按钮设置UIViewAutoresizingFlexibleTopMargin如下选项:

[textField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];

[button setAutoresizingMask: UIViewAutoresizingFlexibleTopMargin];
Run Code Online (Sandbox Code Playgroud)

  • 为 UITableViewCell 设置 autoResizingMask 不是必需的,它继承自 UITableView (我确实尝试过,没有改变)。UITextField 已经有一个自动调整大小掩码(请参阅代码),并且该按钮不应调整大小,因为它是基于图像的按钮。 (2认同)