最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Swift之导航栏标题文字太长,导致返回按钮文字消失问题解决
时间:2016-03-14 编辑:简简单单 来源:一聚教程网
项目中常常会使用 UINavigationController 对各个页面进行导航,导航栏左侧的返回按钮默认标题文字是上级页面的 title。
如果子页面的标题文字过长,返回按钮的文字也是就会消失:
通过 navigationItem.titleView 属性可以很方便的将其替换成自定义的 UIView 视图,这里我们使用固定尺寸的 UILabel,效果图如下:
import UIKit
class DetailViewController: UIViewController {
override func viewDidLoad() {
let titleView = UIView(frame: CGRectMake(0, 0, 200, 40))
let labelForTitle = UILabel(frame: CGRectMake(0, 0, 200, 30))
labelForTitle.font = UIFont.systemFontOfSize(17.0, weight: UIFontWeightMedium)
labelForTitle.center = titleView.center
labelForTitle.text = "文章列表(欢迎访问hangge.com)"
titleView.addSubview(labelForTitle)
self.navigationItem.titleView = titleView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
功能改进:如果文字过长,label自动缩小文字大小
上面的方法会把多余的文字截断并在后面添加...。如果想让标题文字能完全显示,可以通过 adjustsFontSizeToFitWidth 属性让文本标签自动缩放文字尺寸以适应标签大小。效果图如下:
import UIKit
class DetailViewController: UIViewController {
override func viewDidLoad() {
let titleView = UIView(frame: CGRectMake(0, 0, 200, 40))
let labelForTitle = UILabel(frame: CGRectMake(0, 0, 200, 30))
labelForTitle.font = UIFont.systemFontOfSize(17.0, weight: UIFontWeightMedium)
labelForTitle.center = titleView.center
labelForTitle.adjustsFontSizeToFitWidth = true
labelForTitle.text = "文章列表(欢迎访问hangge.com)"
titleView.addSubview(labelForTitle)
self.navigationItem.titleView = titleView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
-
上一个: 苹果iPhone不越狱可以关闭动画效果吗
相关文章
- sora软件价格介绍 02-22
- sora官网入口地址一览 02-22
- Sora生成的视频使用教程 02-22
- 《梦幻西游》元宵灯谜线索答案大全2024 02-22
- 《原神》有朋自远方来第一天通关攻略 02-22
- 《苏醒之路》四个结局达成攻略 02-22