Adding a title to the left side of the navigation bar

后端 未结 3 1149
迷失自我
迷失自我 2021-02-04 05:07

Is it possible to add a title to the left side of the navigation bar? I know how I can add a title in the center but when I try to add one to the left side I see nothing. This i

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 05:33

    You could try creating a custom view, and then create a UIBarButtonItem with that custom view in it.

    Custom view:

    var button = UIButton(frame: CGRectMake(0, 0, 44, 44))
    var label = UILabel(frame: CGRectMake(0, 0, 44, 14)) // adjust as you see fit
    
    label.text = "Label test"
    label.textAlignment = NSTextAlignment.Left
    
    button.addSubview(label)
    
    // Add it to your left bar button
    
    self.navigationItem.leftBarButtonItems = [barButtonNegativeSpacer, UIBarButtonItem(customView: button)
    

提交回复
热议问题