gpt4 book ai didi

ios - 大导航栏标题上方的字幕

转载 作者:行者123 更新时间:2023-11-30 11:24:16 32 4
gpt4 key购买 nike

我想知道当使用preferredsLargeTitles功能时如何在导航栏标题上方获得字幕(如日期)。就像音乐应用一样。我也想要旁边的大按钮(例如音乐应用程序中的个人资料按钮)。我仍然希望大标题显示在顶部,并在滚动屏幕时变小(例如,音乐应用程序上的浏览页面)。

Music App Subtitle and Large Button Icon

我到目前为止使用的代码是

override func viewDidLoad() {
super.viewDidLoad()

self.navigationController?.navigationBar.prefersLargeTitles = true
collectionView.initialize()
self.title = "Home Screen"
}

最佳答案

您可以设置自定义UIView

设定功能

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

}


设置导航标题视图

self.navigationItem.titleView = setTitle(title: "Title", subtitle: "Subtitle")

关于ios - 大导航栏标题上方的字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50932566/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com