我目前正在开发 iOS
元素。我的问题是 textfield
中的任何内容都没有改变我。很难找到问题的答案。应该怎么改?
import Foundation
import UIKit
class passwrodCheckAlertController : UIViewController {
@IBOutlet weak var passModalAlert: UITextView!
@IBOutlet weak var passTextfield: UITextField!
@IBOutlet weak var cancelbutton: UIButton!
@IBOutlet weak var okButton: UIButton!
// let color = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.5)
let color = UIColor.red
let insets = UIEdgeInsets.init(top: 31.7, left: 0.0, bottom: 0.0, right: 0.0)
override func viewDidLoad() {
super.viewDidLoad()
self.passModalAlert.layer.cornerRadius = 8
self.passTextfield.layer.cornerRadius = 3
self.passTextfield.layer.borderColor = color.cgColor
self.passModalAlert.textContainerInset = insets
changeViewFont()
}
}
class PaddingTextField : UITextField {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: UIEdgeInsets(top: 0, left: 100, bottom: 0, right: 15))
return bounds
}
}
主线 Storyboard
屏幕不变
CornerRadius
在 TextView 中效果很好。
提前致谢
我听了评论并使用了leftview
。并且间隙问题得到了解决。但我的 Border
样式仍然没有改变。
let paddingView = UIView(frame: CGRect.init(x: 0, y: 0, width: 10, height: self.passTextfield.frame.height))
self.passTextfield.leftView = paddingView
self.passTextfield.leftViewMode = .always
self.passTextfield.layer.cornerRadius = 3
self.passTextfield.layer.borderColor = color.cgColor
self.passModalAlert.textContainerInset = insets
Border
style doesn't change.
@IBOutlet weak var passTextfield: PaddingTextField!
...
class PaddingTextField : UITextField {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
//set your border style here
self.layer.cornerRadius = 3
self.layer.borderColor = UIColor.red.cgColor
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 15))
return bounds
}
}
But this has not changed anything changed.
您的问题似乎是您的设置,当您创建 socket 时,您应该选择 PaddingTextField 而不是 UITextField,因为 UITextField 是一个父类(super class),并且对它的子类所具有的附加功能一无所知。
删除 IBOutlet
@IBOutlet weak var passTextfield: UITextField!
并重新创建它应该如下所示:
@IBOutlet weak var passTextfield: PaddingTextField!
希望这会有所帮助。
关于边框:
class PaddingTextField : UITextField {
override func awakeFromNib() {
//set your border style here
self.layer.cornerRadius = 3
// Add borderWidth as otherwise you are having a 0 point wide border
self.layer.borderWidth = 5
self.layer.borderColor = UIColor.red.cgColor
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: UIEdgeInsets(top: 0, left: 100, bottom: 0, right: 15))
return bounds
}
}
关于ios - 文本字段中的 Border 和 Padding 样式不适用于 swift5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57865685/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |