我有一个自定义UIView,它具有2个UILabel
(相同的宽度)和一个UIImageView
(已知的44pt宽度)。宽度大小仅作为示例提供,可以更改,但是UILabels
的宽度应相同,UIImage的宽度为44点。我想将此视图添加到UINavigationBar
'titleView
,但ImageView
应该在导航栏的center中。
(60 width) UILabel---UIImageView (44 width) ---UILabel (60 width)
我想要是UILabel最多具有两行且adJustFontSizeToFitWidth
为true。我给标题视图指定了特定的宽度和高度,但是标签只有两行,但是即使它们不适合视图,它们的字体大小也不会改变。
我如何添加titleView:
navigationItem.titleView = myTitleView
let widthOfItem: CGFloat = 30.0
let pading: CGFloat = 40
let aWidth: CGFloat = (self.navigationController?.navigationBar.frame.width)! - CGFloat(1) * widthOfItem * 2.0 - pading
myTitleView { (make) in
make.width.equalTo(aWidth)
make.height.equalTo(44)
}
MyCustomView:
override func layoutSubviews() {
super.layoutSubviews()
let preferredWidth = (bounds.width / 2) - 56
firstLabel.preferredMaxLayoutWidth = preferredWidth
secondLabel.preferredMaxLayoutWidth = preferredWidth
}
private func setupViews() {
addSubview(firstLabel)
addSubview(myImageView)
addSubview(secondLabel)
firstLabel.font = .myFont(.bold, size: 36)
firstLabel.adjustsFontSizeToFitWidth = true
firstLabel.minimumScaleFactor = 0.5
firstLabel.textColor = .textPrimary
firstLabel.numberOfLines = 2
firstLabel.lineBreakMode = .byWordWrapping
firstLabel.textAlignment = .right
myImageView.contentMode = .scaleAspectFit
myImageView.clipsToBounds = true
myImageView.layer.minificationFilter = .trilinear
myImageView.layer.cornerRadius = currencyImageSize.height / 2
secondLabel.font = . myFont(.bold, size: 36)
secondLabel.translatesAutoresizingMaskIntoConstraints = false
secondLabel.textColor = .textPrimary
secondLabel.numberOfLines = 2
secondLabel.adjustsFontSizeToFitWidth = true
secondLabel.baselineAdjustment = .none
secondLabel.minimumScaleFactor = 0.5
secondLabel.lineBreakMode = .byWordWrapping
secondLabel.textAlignment = .left
firstLabel.snp.makeConstraints { (make) in
make.leading.equalToSuperview()
make.top.bottom.equalToSuperview()
make.trailing.equalTo(myImageView.snp.leading).offset(-12)
}
myImageView.snp.makeConstraints { (make) in
make.height.equalToSuperview()
make.width.equalTo(myImageView.snp.height)
make.centerX.equalToSuperview()
}
secondLabel.snp.makeConstraints { (make) in
make.top.bottom.equalToSuperview()
make.leading.equalTo(myImageView.snp.trailing).offset(12)
make.trailing.equalToSuperview()
}
}
也许此代码会起作用?
class CustomNavClass: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
setup_view()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup_view()
}
private func setup_view() {
backgroundColor = .lightGray
let image = UIImageView(image: .init())
addSubview(image)
image.translatesAutoresizingMaskIntoConstraints = false
image.backgroundColor = .green
NSLayoutConstraint.activate([
image.centerXAnchor.constraint(equalTo: centerXAnchor),
image.widthAnchor.constraint(equalToConstant: 44),
image.heightAnchor.constraint(equalToConstant: 44),
image.topAnchor.constraint(equalTo: topAnchor, constant: 3),
image.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -3)
])
let leftLabel = UILabel()
leftLabel.text = "Label label left long label will go here, naturally"
leftLabel.numberOfLines = 2
leftLabel.adjustsFontSizeToFitWidth = true
leftLabel.backgroundColor = .red
addSubview(leftLabel)
leftLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
leftLabel.leadingAnchor.constraint(equalTo: leadingAnchor),
leftLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
leftLabel.trailingAnchor.constraint(equalTo: image.leadingAnchor, constant: -10)
])
let rightLabel = UILabel()
rightLabel.text = "Label label right long label will go here, naturally"
rightLabel.numberOfLines = 2
rightLabel.adjustsFontSizeToFitWidth = true
rightLabel.backgroundColor = .blue
addSubview(rightLabel)
rightLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
rightLabel.trailingAnchor.constraint(equalTo: trailingAnchor),
rightLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
rightLabel.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 10)
])
}
}
代码不使用UIStackView
作为其功能的限制(具体地说,在UIView
.fill
属性上只能扩展一个distribution
。
这将创建布局约束:
center
(顶部和底部约束为3,可选,您可以使用centerY约束等)。UILabel
的前缘与UIImageView
的前缘与后缘挂钩。UILabel
与尾沿和前沿与UIImageView
的尾沿挂钩。