【问题标题】:Is it possible to allign clearButton of UITextField是否可以对齐 UITextField 的清除按钮
【发布时间】:2013-02-13 16:26:16
【问题描述】:

UITextField的clearButton可以对齐吗?

我以编程方式将按钮添加到 textField:

_usernameTextField.clearButtonMode = UITextFieldViewModeWhileEditing;

但它的位置比文本字段的中心低一点。

我该如何解决?

【问题讨论】:

  • 垂直对齐怎么样?
  • 怎么样?有这个属性吗?
  • 它什么也没做。按钮留在原处。

标签: iphone ios objective-c uitextfield


【解决方案1】:

您可以扩展 UITextField 并覆盖 clearButtonRectForBounds: 并提供自定义矩形。

【讨论】:

    【解决方案2】:

    您是否尝试过字段本身的 contentVerticalAlignment 属性?

    usernameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    

    原文引用:Why doesn't the clear button aligned with the text in a UITextField?

    【讨论】:

    • 它什么也没做。按钮留在原处
    【解决方案3】:

    最终我继承了UITextField 并覆盖了函数clearButtonRectForBounds:

    .h

    #import <UIKit/UIKit.h>
    
    @interface TVUITextFieldWithClearButton : UITextField
    
    @end
    

    .m

    #import "TVUITextFieldWithClearButton.h"
    
    @implementation TVUITextFieldWithClearButton
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
        }
        return self;
    }
    
    - (void)awakeFromNib
    {
        self.clearButtonMode = UITextFieldViewModeWhileEditing;
    }
    
    
    - (CGRect)clearButtonRectForBounds:(CGRect)bounds
    {
        return CGRectMake(bounds.size.width/2-20 , bounds.origin.y-3, bounds.size.width, bounds.size.height);
    }
    
    @end
    

    【讨论】:

      猜你喜欢
      • 2011-03-27
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多