ios - 文本字段中的 Border 和 Padding 样式不适用于 swift5
<p><p>我目前正在开发 <code>iOS</code> 元素。我的问题是 <code>textfield</code> 中的任何内容都没有改变我。很难找到问题的答案。应该怎么改?</p>
<pre class="lang-swift prettyprint-override"><code>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
}
}
</code></pre>
<p><strong>主线 Storyboard</strong></p>
<p> <a href="/image/rJuMP.png" rel="noreferrer noopener nofollow"><img src="/image/rJuMP.png" alt="MainStoryboard"/></a> </p>
<p><strong>屏幕不变</strong></p>
<p> <a href="/image/eBjop.png" rel="noreferrer noopener nofollow"><img src="/image/eBjop.png" alt="screen field"/></a> </p>
<p><code>CornerRadius</code> 在 TextView 中效果很好。</p>
<p>提前致谢</p>
<hr/>
<h2>编辑开始</h2>
<p>我听了评论并使用了<code>leftview</code>。并且间隙问题得到了解决。但我的 <code>Border</code> 样式仍然没有改变。</p>
<pre class="lang-swift prettyprint-override"><code> 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
</code></pre>
<p> <a href="/image/tiB4c.png" rel="noreferrer noopener nofollow"><img src="/image/tiB4c.png" alt="change"/></a> </p>
<blockquote>
<p><code>Border</code> style doesn't change.</p>
</blockquote>
<hr/>
<h2>编辑第二次开始</h2>
<pre class="lang-swift prettyprint-override"><code>@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
}
}
</code></pre>
<blockquote>
<p>But this has not changed anything changed.</p>
</blockquote></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您的问题似乎是您的设置,当您创建 socket 时,您应该选择 PaddingTextField 而不是 UITextField,因为 UITextField 是一个父类(super class),并且对它的子类所具有的附加功能一无所知。</p>
<p>删除 IBOutlet</p>
<pre><code>@IBOutlet weak var passTextfield: UITextField!
</code></pre>
<p>并重新创建它应该如下所示:</p>
<pre><code>@IBOutlet weak var passTextfield: PaddingTextField!
</code></pre>
<p>希望这会有所帮助。</p>
<p>关于边框:</p>
<pre><code>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
}
}
</code></pre>
<p>按预期运行:
<a href="/image/Az3xo.png" rel="noreferrer noopener nofollow"><img src="/image/Az3xo.png" alt="enter image description here"/></a> </p></p>
<p style="font-size: 20px;">关于ios - 文本字段中的 Border 和 Padding 样式不适用于 swift5,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/57865685/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/57865685/
</a>
</p>
页:
[1]