如何移动 UITextField 中的清除按钮?

发布于 2024-09-03 22:22:39 字数 108 浏览 3 评论 0原文

由于某种原因,当我添加 UITextfield 作为表格单元格内容视图的子视图时,清除按钮与字段中键入的文本不对齐,并且出现在其下方。有什么方法可以移动清除按钮的文本来阻止这种情况发生吗?感谢您的帮助,

For some reason, when I add a UITextfield as a subview of the contentview of a tablecell, the clearbutton does not align with the text typed in the field, and appears a bit underneath it. Is there any way I can move the text of the clearbutton to stop this from happening? Thanks for any help,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

月棠 2024-09-10 22:22:42

Swift 4 版本是

import UIKit

class LoginTextField: UITextField {

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: xPos, y:yPos, width: yourWidth, height: yourHeight)
    }

}

Swift 4 version would be

import UIKit

class LoginTextField: UITextField {

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: xPos, y:yPos, width: yourWidth, height: yourHeight)
    }

}
锦上情书 2024-09-10 22:22:41

我对 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

I had subclassed the UITextField and override the function 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
尐偏执 2024-09-10 22:22:41

我还没见过这个,屏幕截图会很有帮助。但是,快速的答案是,您可以检查 UITextField 的子视图数组,找到包含清除按钮的子视图,并调整其frame.origin。

编辑:看来我对这个答案(写于 2010 年)被否决了。这不是“官方”批准的方法,因为您正在操纵私有对象,但 Apple 无法检测到它。主要风险是视图层次结构可能会在某个时刻发生更改。

I haven't seen this, and a screen shot would be helpful. But, the quick answer is that you can inspect the subviews array of the UITextField, find the subview that contains the clear button, and adjust its frame.origin.

Edit: It seems I've been downvoted for this answer (written in 2010). This is of not an "officially" approved method because you're manipulating private objects, but it's not detectable by Apple. The main risk is that the view hierarchy might be changed at some point.

情未る 2024-09-10 22:22:41

子类 UITextField 并重写此方法:

- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.origin.x - 10, bounds.origin.y, bounds.size.width, bounds.size.height);
}

返回符合您需求的 CGRect。

Subclass UITextField and override this method:

- (CGRect)clearButtonRectForBounds:(CGRect)bounds
{
    return CGRectMake(bounds.origin.x - 10, bounds.origin.y, bounds.size.width, bounds.size.height);
}

return the CGRect that matches your needs.

心房敞 2024-09-10 22:22:40

正如 @Luda 所说,正确的方法是子类化 UITextField 并覆盖 - (CGRect)clearButtonRectForBounds:(CGRect)bounds。然而,传递给该方法的边界是视图本身的边界,而不是按钮的边界。因此,您应该调用 super 来获取操作系统提供的大小(以避免图像失真),然后调整原点以满足您的需要。

例如

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect originalRect = [super clearButtonRectForBounds:bounds];
    return CGRectOffset(originalRect, -10, 0); //shift the button 10 points to the left
}

苹果 文档指出:

讨论您不应直接调用此方法。如果你想
将清除按钮放在不同的位置,您可以覆盖它
方法并返回新的矩形。你的方法应该调用 super
实现并仅修改返回的矩形的原点。
更改清除按钮的大小可能会导致不必要的失真
按钮图像的。

As stated by @Luda the correct way is to subclass UITextField and override - (CGRect)clearButtonRectForBounds:(CGRect)bounds. However the bounds passed in to the method are those of the view itself and not the button. Therefore you should call super to get the OS provided size (to avoid distortion of the image) and then adjust the origin to suit your needs.

e.g.

- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    CGRect originalRect = [super clearButtonRectForBounds:bounds];
    return CGRectOffset(originalRect, -10, 0); //shift the button 10 points to the left
}

Apple docs state:

Discussion You should not call this method directly. If you want to
place the clear button in a different location, you can override this
method and return the new rectangle. Your method should call the super
implementation and modify the returned rectangle’s origin only.
Changing the size of the clear button may cause unnecessary distortion
of the button image.

我为君王 2024-09-10 22:22:40

Van Du Tran 在 Swift 4 中的答案:

class CustomTextField: UITextField {


override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
    let originalRect = super.clearButtonRect(forBounds: bounds)

    return originalRect.offsetBy(dx: -8, dy: 0)
}

}

The answer from Van Du Tran in Swift 4:

class CustomTextField: UITextField {


override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
    let originalRect = super.clearButtonRect(forBounds: bounds)

    return originalRect.offsetBy(dx: -8, dy: 0)
}

}

放手` 2024-09-10 22:22:40

斯威夫特 4、5

子类 UITextField(工作完美,已测试)

class textFieldWithCrossButtonAdjusted: UITextField {

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {

        let originalRect = super.clearButtonRect(forBounds: bounds)

        //move 10 points left

        return originalRect.offsetBy(dx: -10, dy: 0)
    }
}

Swift 4, 5

Subclass UITextField (Working Perfectly, Tested)

class textFieldWithCrossButtonAdjusted: UITextField {

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {

        let originalRect = super.clearButtonRect(forBounds: bounds)

        //move 10 points left

        return originalRect.offsetBy(dx: -10, dy: 0)
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文