gpt4 book ai didi

ios - UITextfield 中的位置 "Clear Button"

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:12 25 4
gpt4 key购买 nike

有没有办法定位清除按钮?我想将它向下移动一点,使其与文本输入处于同一水平。有什么想法吗?

我的文本字段已经是另一个处理效果的类的子类。包含“clearButtonRect”函数不起作用。

@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 函数

class CustomTextField: UITextField {

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

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

当您使用第 3 方库调用 TextFieldEffects 时,您需要执行以下步骤。

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

Setting CustomTextField Class

如果您可以通过编程方式进行,您将需要在那里进行更改。

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

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)
}
}

输出

Output Image

希望这会有所帮助。

关于ios - UITextfield 中的位置 "Clear Button",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57787658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com