【发布时间】:2018-12-17 01:06:59
【问题描述】:
我正在尝试创建一个自定义 UITextView 类来添加底部边框。使用以下代码,它不显示底部边框。我通过在情节提要中添加视图并根据需要对其进行操作来替代解决方案,但这不是最佳选择,因为我必须在许多地方使用 UITextView 因此我想要自定义 UITextView 类。我有用于自定义 UITextField 的类似代码,它可以工作。
我还需要通过代码更改此边框的颜色。
有什么帮助吗?
import Foundation
import UIKit
class CustomTextView: UITextView {
var bottomBorder = UIView()
init(frame: CGRect) {
super.init(frame: frame, textContainer: nil)
self.initialize()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func awakeFromNib() {
super.awakeFromNib()
self.initialize()
// Setup Bottom-Border
self.translatesAutoresizingMaskIntoConstraints = false
bottomBorder = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
bottomBorder.backgroundColor = .red
bottomBorder.translatesAutoresizingMaskIntoConstraints = false
addSubview(bottomBorder)
bottomBorder.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
bottomBorder.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
bottomBorder.rightAnchor.constraint(equalTo: rightAnchor).isActive = true
bottomBorder.heightAnchor.constraint(equalToConstant: 1).isActive = true // Set Border-Strength
}
func initialize() {
}
}
【问题讨论】:
-
我多次使用底部边框。所以大多数时候我只是简单地取一个
UIImage view蚂蚁,只需将黑色订单图像设置为UIImageView,并将高度设置为2px。这不是我所知道的正确方法。但这也是最简单的方法。
标签: ios swift uitextview