簡體   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