我试图在导航栏中设置标题标签以允许多行.我有自定义导航控制器代码,我将多行代码放入.我知道代码已经存在,但我的多行部分无效.
let titleLabel = UILabel() titleLabel.frame = CGRectMake(0, 0, self.navigationBar.frame.width, self.navigationBar.frame.height * 2) titleLabel.numberOfLines = 0 titleLabel.lineBreakMode = .ByWordWrapping navigationItem.titleView = titleLabel
但是文本最后仍然没有结束.我也尝试将它放入单独的视图控制器本身,self.navigationController?.
在前面添加navigationItem
相同的结果.
我的代码中是否有一些东西会使标题标签不能使用多行?
以下是如何创建多行navigationBar标题的代码示例
let label: UILabel = UILabel(frame: CGRectMake(0, 0, 400, 50)) label.backgroundColor = UIColor.clearColor() label.numberOfLines = 2 label.font = UIFont.boldSystemFontOfSize(16.0) label.textAlignment = .Center label.textColor = UIColor.whiteColor() label.text = "This is a\nmultiline string for the navBar" self.navigationItem.titleView = label
Swift 3.0:
let label = UILabel() label.backgroundColor = .clear label.numberOfLines = 2 label.font = UIFont.boldSystemFont(ofSize: 16.0) label.textAlignment = .center label.textColor = .white label.text = "This is a\nmultiline string for the navBar" self.navigationItem.titleView = label