25.swift-UIView(纯代码自定义)
2021-07-26 本文已影响0人
ChaosHeart
1.实现
///发布界面的自定义titleView
class ComposeTitleView: UIView {
// 标题
private lazy var titleLabel = UILabel()
/// 昵称
private lazy var screenNameLabel = UILabel()
///自定义构造函数
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
//MARK:- 设置界面
extension ComposeTitleView {
func setupUI() {
//1.添加
addSubview(titleLabel)
addSubview(screenNameLabel)
//2.frame
// titleLabel.snp.makeConstraints { (make) in
// make.centerX.equalTo(self)
// make.top.equalTo(self)
// }
// screenNameLabel.snp.makeConstraints { (make) in
// make.centerX.equalTo(titleLabel.snp.centerX)
// make.top.equalTo(titleLabel.snp.bottom).offset(3)
// }
titleLabel.frame = CGRect.init(x: 0, y: 0, width: 200, height: 20)
screenNameLabel.frame = CGRect.init(x: 0, y: 22, width: 200, height: 20)
//3.属性
titleLabel.font = UIFont.systemFont(ofSize: 15)
titleLabel.textAlignment = .center
screenNameLabel.font = UIFont.systemFont(ofSize: 13)
screenNameLabel.textColor = UIColor.lightGray
screenNameLabel.textAlignment = .center
//4.文字
titleLabel.text = "发微博"
screenNameLabel.text = UserInfoViewModel.shareInstance.info?.screen_name
}
}
2.调用
///懒加载titleView
private lazy var titleV = ComposeTitleView()