繁体   English   中英

使用NavigationBar标题将UIViewController子类化

[英]Subclassing UIViewController With NavigationBar Title

我正在尝试使用标签( UILabel )设置为导航栏标题的UIViewController子类。 我想使用属性字符串设置标题,而不是将名称设置为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()
    }
}

如果运行此代码,则标题为空。 我究竟做错了什么? 谢谢。

我看不到您在哪里设置self.navigationItem.titleView = titleLabel

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM