我有一个带有 rightButtonItem 的导航栏和另一个带有 right&leftButtonItem 的导航栏。
我练习尝试使用带有子标题的导航栏,但是当我将标题和子标题字符串设置得很长时。
它会重叠,并且字符串被截断。
我该如何解决这个问题?
Issue picture_1
Issue picture_2
func setTitle(title:String, subtitle:String) -> UIView {
let titleLabel = UILabel(frame: CGRect(x:0, y:-5, width:0, height:0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.red
titleLabel.font = UIFont.boldSystemFont(ofSize: 14)
titleLabel.text = title
titleLabel.sizeToFit()
let subtitleLabel = UILabel(frame: CGRect(x:0, y:18, width:0, height:0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor.blue
subtitleLabel.font = UIFont.systemFont(ofSize: 12)
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
let titleView = UIView(frame: CGRect(x:0, y:0, width:max(titleLabel.frame.size.width, subtitleLabel.frame.size.width), height:30))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
let widthDiff = subtitleLabel.frame.size.width - titleLabel.frame.size.width
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
titleLabel.frame = frame.integral
}
return titleView
}//source from here:https://gist.github.com/nazywamsiepawel/0166e8a71d74e96c7898
最佳答案
这部分代码需要进行一点修改:
if widthDiff > 0 {
var frame = titleLabel.frame
frame.origin.x = widthDiff / 2
titleLabel.frame = frame.integral
} else {
var frame = subtitleLabel.frame
frame.origin.x = abs(widthDiff) / 2
// this should be *subtitleLabel*
//titleLabel.frame = frame.integral
subtitleLabel.frame = frame.integral
}
关于ios - NavigationTitle 与 subTitle 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786784/