Chr*_*ris 5 uitextfield ios swift
有没有办法定位清除按钮?我想将其向下移动一点,以便与文本输入处于同一级别。有任何想法吗?
我的文本字段已经是另一个处理效果的类的子类。包括“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)
}
}`
Run Code Online (Sandbox Code Playgroud)

您可以创建自己的自定义文本字段
\n\nChirag的答案是正确的,但根据Apple的说法,你的方法应该调用超级实现并仅修改返回的矩形\xe2\x80\x99s原点
\n\nclass CustomTextField : UITextField\n{\n override func clearButtonRect(forBounds bounds: CGRect) -> CGRect{\n let rect = super.clearButtonRect(forBounds: bounds)\n return rect.offsetBy(dx: customX, dy: customY)\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
子类化您的文本字段并覆盖clearButtonRect函数
class CustomTextField: UITextField {
override func clearButtonRect(forBounds bounds: CGRect) -> CGRect {
return CGRect(x: xPos, y:yPos, width: yourWidth, height: yourHeight)
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 1 - 子类 3rd 方文本字段
当您使用 3rd 方库调用 TextFieldEffects 时,您需要按照以下步骤操作。
如果您使用的是情节提要并且在那里有一个文本字段(将类设置为 CustomTextField)
如果您可以以编程方式进行,则需要在那里进行更改。
然后创建一个新的 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)
}
}
Run Code Online (Sandbox Code Playgroud)
输出
希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
1412 次 |
| 最近记录: |