我想知道当使用preferredsLargeTitles功能时如何在导航栏标题上方获得字幕(如日期)。就像音乐应用一样。我也想要旁边的大按钮(例如音乐应用程序中的个人资料按钮)。我仍然希望大标题动画到顶部,并在滚动屏幕时变小(例如音乐应用程序上的浏览页面)。
到目前为止,我正在使用的代码是
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.prefersLargeTitles = true
collectionView.initialize()
self.title = "Home Screen"
}
答案 0 :(得分:0)
You can set a custom UIView
Set a function
func setTitle(title:String, subtitle:String) -> UIView {
//Get navigation Bar Height and Width
let navigationBarHeight = Int(self.navigationController!.navigationBar.frame.height)
let navigationBarWidth = Int(self.navigationController!.navigationBar.frame.width)
//Y position for Title and Subtitle
var y_Title = 0.0
var y_SubTitle = 0.0
//Set Y position
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
//If screen height equal iPhone 5S and SE
case 1136:
y_Title = 46
y_SubTitle = 20
print("iPhone 5S and SE")
//If screen height equal iPhone 6, 6+, 6S, 6S+, 7, 7+, 8, 8+ and X
case 1334, 1920, 2208, 2436:
y_Title = 48
y_SubTitle = 22
print("iPhone 6, 6+, 6S, 6S+, 7, 7+, 8, 8+ and X")
default:
y_Title = 46
y_SubTitle = 36
print("Default")
}
}
//Set Font size and weight for Title and Subtitle
let titleFont = UIFont.systemFont(ofSize: 33, weight: UIFont.Weight.bold)
let subTitleFont = UIFont.systemFont(ofSize: 20, weight: UIFont.Weight.semibold)
//Title label
let titleLabel = UILabel(frame: CGRect(x: 8.5, y: y_Title, width: 0, height: 0))
titleLabel.backgroundColor = UIColor.clear
titleLabel.textColor = UIColor.white
titleLabel.font = titleFont
titleLabel.text = title
titleLabel.sizeToFit()
//SubTitle label
let subtitleLabel = UILabel(frame: CGRect(x: 8.5, y: y_SubTitle, width: 0, height: 0))
subtitleLabel.backgroundColor = UIColor.clear
subtitleLabel.textColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.8)
subtitleLabel.font = subTitleFont
subtitleLabel.text = subtitle
subtitleLabel.sizeToFit()
//Add Title and Subtitle to View
let titleView = UIView(frame: CGRect(x: 0, y: 0, width: navigationBarWidth, height: navigationBarHeight))
titleView.addSubview(titleLabel)
titleView.addSubview(subtitleLabel)
return titleView
}
Set your Navigation Title View
self.navigationItem.titleView = setTitle(title: "Title", subtitle: "Subtitle")