在 UITextview 中定位 UIButton
Position UIButton in UITextview
我想在我的密码文本字段中放置一个 "forgot?" 按钮。如果文本字段中没有任何内容,用户应该能够单击它,并且应该弹出另一个 ViewController。我唯一能做的就是你在下面的图片中看到的。我的问题是该按钮不可点击,并且它与占位符文本不在同一级别。关于如何解决这两个问题有什么想法吗?
let button = UIButton(type: .custom)
button.setTitle("vergessen?", for: .normal)
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
passwordTextField.rightView = button
passwordTextField.rightViewMode = .unlessEditing
您可以使用 Storyboard
。为密码 TextField
和忘记 Button
.
创建帮助视图
-设置助手视图与电子邮件相同的宽度和高度TextField
。
-在助手视图中添加一个 TextField
和一个 Button
,然后您可以决定密码 TextField
宽度和忘记 Button
宽度。
- 为 TextField
和 Button
设置约束
我设置了绿色来理解辅助视图的作用。
更新
我刚刚使用了您的代码并且它有效 fine.Check 您的文本字段再次受到限制。这是我用的。
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("vergessen?", for: .normal)
button.setTitleColor(#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1), for: .normal)
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
textfFeld.rightView = button
textfFeld.rightViewMode = .unlessEditing
}
@objc func refresh(_ sender: Any) {
print("Hello")
}
我唯一认为我改变的是按钮类型从 .custom
到 .system
。
我建议创建一个 xib
文件及其内部有按钮的文本项的相关视图。因此您将来可以在其他地方(本项目或其他项目)重用它
通过定义恒定高度 (100),您将在不同的 iOS 设备中体验丑陋和错位 UI。
这是你应该做的:
- 为您的自定义定义一个 xib 文件
UITextView
- 为其创建约束,使其宽度和高度由其 parent.Also 定义您忘记的
UIButton
相对于您的 UITextView
.
- 定义其(xib)相关
UIView
class
- 在您的
Storyboard
中使用它
在你从我的 子类化的文件中添加另一个函数
// Modify the values as required
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
let offset = -20
let width = 100
let height = width
let x = Int(bounds.width) - width - offset
let y = offset
let rightViewBounds = CGRect(x: x, y: y, width: width, height: height)
return rightViewBounds
}
现在您可以删除后续行
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
输出
以及关于按钮点击事件。删除您的代码,因为您提到它未连接
IBAction func refresh(_ sender: Any) { }
并在创建按钮的同一文件中添加以下代码。
@objc func refresh() {
// your vc code here
print("in refresh")
}
以上代码与您拥有的 addTarget 代码挂钩。
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
希望对您有所帮助。
我想在我的密码文本字段中放置一个 "forgot?" 按钮。如果文本字段中没有任何内容,用户应该能够单击它,并且应该弹出另一个 ViewController。我唯一能做的就是你在下面的图片中看到的。我的问题是该按钮不可点击,并且它与占位符文本不在同一级别。关于如何解决这两个问题有什么想法吗?
let button = UIButton(type: .custom)
button.setTitle("vergessen?", for: .normal)
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
passwordTextField.rightView = button
passwordTextField.rightViewMode = .unlessEditing
您可以使用 Storyboard
。为密码 TextField
和忘记 Button
.
-设置助手视图与电子邮件相同的宽度和高度TextField
。
-在助手视图中添加一个 TextField
和一个 Button
,然后您可以决定密码 TextField
宽度和忘记 Button
宽度。
- 为 TextField
和 Button
我设置了绿色来理解辅助视图的作用。
更新
我刚刚使用了您的代码并且它有效 fine.Check 您的文本字段再次受到限制。这是我用的。
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(type: .system)
button.setTitle("vergessen?", for: .normal)
button.setTitleColor(#colorLiteral(red: 0.3647058904, green: 0.06666667014, blue: 0.9686274529, alpha: 1), for: .normal)
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
textfFeld.rightView = button
textfFeld.rightViewMode = .unlessEditing
}
@objc func refresh(_ sender: Any) {
print("Hello")
}
我唯一认为我改变的是按钮类型从 .custom
到 .system
。
我建议创建一个 xib
文件及其内部有按钮的文本项的相关视图。因此您将来可以在其他地方(本项目或其他项目)重用它
通过定义恒定高度 (100),您将在不同的 iOS 设备中体验丑陋和错位 UI。 这是你应该做的:
- 为您的自定义定义一个 xib 文件
UITextView
- 为其创建约束,使其宽度和高度由其 parent.Also 定义您忘记的
UIButton
相对于您的UITextView
. - 定义其(xib)相关
UIView
class - 在您的
Storyboard
中使用它
在你从我的
// Modify the values as required
override func rightViewRect(forBounds bounds: CGRect) -> CGRect {
let offset = -20
let width = 100
let height = width
let x = Int(bounds.width) - width - offset
let y = offset
let rightViewBounds = CGRect(x: x, y: y, width: width, height: height)
return rightViewBounds
}
现在您可以删除后续行
button.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(100), height: CGFloat(100))
输出
以及关于按钮点击事件。删除您的代码,因为您提到它未连接
IBAction func refresh(_ sender: Any) { }
并在创建按钮的同一文件中添加以下代码。
@objc func refresh() {
// your vc code here
print("in refresh")
}
以上代码与您拥有的 addTarget 代码挂钩。
button.addTarget(self, action: #selector(self.refresh), for: .touchUpInside)
希望对您有所帮助。