UINavigationBar TitleView with subtitle

后端 未结 9 895
一生所求
一生所求 2021-01-01 21:09

I want a titleView inside my UINavigationBar which has two lines of text instead of only one

My current implementiation works well when I have a \"Back\"-Button and

9条回答
  •  别那么骄傲
    2021-01-01 21:27

    You can also use an attributed string to achieve the same result:

    let subtitleAttribute = [NSForegroundColorAttributeName: UIColor.grayColor() , NSFontAttributeName: UIFont.systemFontOfSize(12.0)]
    let titleString = NSMutableAttributedString(string: "title" + "\n", attributes: [NSFontAttributeName: UIFont.boldSystemFontOfSize(17.0)])
    let subtitleString = NSAttributedString(string: "subtitle", attributes: subtitleAttribute)
    titleString.appendAttributedString(subtitleString)
    
    let label = UILabel(frame: CGRectMake(0, 0, titleString.size().width, 44))
    label.numberOfLines = 0
    label.textAlignment = NSTextAlignment.Center
    label.attributedText = titleString
    self.navigationItem.titleView = label
    

    Swift 3

    let subtitleAttribute = [NSForegroundColorAttributeName: UIColor.gray , NSFontAttributeName: UIFont.systemFont(ofSize: 12.0)]
    let titleString = NSMutableAttributedString(string: "title" + "\n", attributes: [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 17.0)])
    let subtitleString = NSAttributedString(string: "subtitle", attributes: subtitleAttribute)
    titleString.append(subtitleString)
    
    let label = UILabel(frame: CGRect(x: 0, y: 0, width: titleString.size().width, height: 44))
    label.numberOfLines = 0
    label.textAlignment = NSTextAlignment.center
    label.attributedText = titleString
    self.navigationItem.titleView = label
    

提交回复
热议问题