位置“清除按钮"在 UITextfield 中 [英] Position "Clear Button" in UITextfield

查看:30
本文介绍了位置“清除按钮"在 UITextfield 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法定位清除按钮?我想将其向下移动一点,以便与文本输入处于同一级别.有什么想法吗?

Is there a way to position the clear button? I would like to move it down by just a little bit so it's on the same level with the text input. Any ideas?

我的文本字段已经是另一个处理效果的类的子类.包含clearButtonRect"功能不起作用.

My Textfields are already Subclasses of another Class that handles the effects. Including the "clearButtonRect" function doesn't work.

@IBDesignable open class HoshiTextField: TextFieldEffects {

//effect functions..

    override open func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        return CGRect(x: 0, y: 0.2, width: 0.1, height: 0.3)
        }
    }`

推荐答案

子类化你的文本框并覆盖 clearButtonRect 函数

Subclass your textfield and override the clearButtonRect function

class CustomTextField: UITextField {

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

编辑 1 - 子类 3rd 方文本字段

当您使用第 3 方库调用 TextFieldEffects 时,您需要按照以下步骤操作.

As you are using 3rd party library calls TextFieldEffects you will need to follow the below steps.

如果您正在使用故事板并在那里有一个文本字段(将类设置为 CustomTextField)

If you you are using storyboard and have a textfield on there (set the class to CustomTextField)

如果您可以以编程方式执行此操作,则需要在此处进行更改.

if you can doing it programmatically you will need to change it there.

然后创建一个名为 CustomTextField.swift 的新 swift 文件并添加以下代码

Then create a new swift file call it CustomTextField.swift and add the below code

import UIKit
import TextFieldEffects // Import the 3rd party library

// Now you are subclassing the 3rd party textfield
// Change it to the textfield you are using and if you are using multiple create subclass for individual ones.
class CustomTextField: HoshiTextField {

    override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
        // change the return line as per your requirement. 
        return CGRect(x: 300, y: 25, width: 40, height: 20) 
    }
}

输出

希望这会有所帮助.

这篇关于位置“清除按钮"在 UITextfield 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆