1.项目中引入第三方框架MarqueeLabel,地址如下:
https://github.com/search?utf8=✓&q=MarqueeLabel&type=Repositories&ref=searchresults
2.那上代码吧:
import UIKit
import MarqueeLabel_Swift
class MarqueeTitleView: UIView {
init(frame: CGRect, title: String) {
super.init(frame: frame)
let label = MarqueeLabel(frame: frame)
label.text = title
label.textColor = .whiteColor()
label.font = UIFont.boldSystemFontOfSize(17)
label.textAlignment = .Center
label.scrollDuration = 10
label.trailingBuffer = 30
label.fadeLength = 10
label.animationCurve = .EaseInOut
self.addSubview(label)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
注:导入框架后自定义一个view将上面代码粘贴替换.
3.控制器中代码:
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "nav_back", target: self, action: #selector(back))
let width = view.frame.width - 120
let titleView = MarqueeTitleView(frame: CGRect(x: 0, y: 0, width: width, height: 30), title: "这个地方是你要导航栏上title滚动的文字")
navigationItem.titleView = titleView
}
}
4.好了,赶快去试试吧,惊喜等你发现.
网友评论