[英]Subclassing UIViewController With NavigationBar Title
I'm trying to subclass a UIViewController
with a label ( UILabel
) set to the title of the navigation bar. 我正在尝试使用标签(
UILabel
)设置为导航栏标题的UIViewController
子类。 Instead of setting a name to self.title, I want to use an attributed string to set the title. 我想使用属性字符串设置标题,而不是将名称设置为self.title。
class BasicViewController: UIViewController {
var titleString = ""
func setup() {
//self.title = titleString
let navBar = navigationController!.navigationBar
navBar.barTintColor = UIColor.redColor()
let atext = NSMutableAttributedString(string: titleString)
atext.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, atext.length))
atext.addAttribute(NSStrokeColorAttributeName, value: UIColor.yellowColor(), range: NSMakeRange(0, atext.length))
atext.addAttribute(NSStrokeWidthAttributeName, value: NSNumber.init(float: -1.0), range: NSMakeRange(0, atext.length))
let titleLabel:UILabel = UILabel.init(frame: CGRectMake(50, 3, 220, 44))
titleLabel.attributedText = atext
titleLabel.textAlignment = NSTextAlignment.Center
titleLabel.font = UIFont(name: "Helvetica", size: 24.0)
}
}
class HomeViewController: BasicViewController {
override func viewDidLoad() {
super.viewDidLoad()
titleString = "My App"
setup()
}
}
If I run this code, I get an empty title. 如果运行此代码,则标题为空。 What am I doing wrong?
我究竟做错了什么? Thanks.
谢谢。
我看不到您在哪里设置self.navigationItem.titleView = titleLabel
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.