导航栏中是否可以只有大标题?
Is it possible to have only large titles in the navigation bar?
我希望我的应用程序有大标题,但是,当用户滚动时,我不希望 regular/normal 导航栏出现。我只希望大栏与 tableview 一起向上滚动。如果不在导航栏中制作自定义视图,这可能吗?我已经使用自定义视图成功地完成了它,但是它不是那么流畅并且在里面放一个 UISearchController 很痛苦。
func createNavController(vc: UIViewController, title: String, image: UIImage, tag: Int) -> UINavigationController {
let navController = UINavigationController(rootViewController: vc)
navController.navigationBar.prefersLargeTitles = true
navController.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.tintColor = .headerColor
navController.tabBarItem = UITabBarItem(title: title, image: image.withRenderingMode(.alwaysTemplate), tag: tag)
navController.navigationBar.topItem?.title = title
navController.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.headerColor, .font: UIFont.customHeaderFont(size: 25)]
navController.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.headerColor, .font: UIFont.customHeaderFont(size: 40)]
// let titleLabel = UILabel()
// titleLabel.text = title
// titleLabel.textColor = .headerColor
// titleLabel.font = UIFont.customHeaderFont(size: navController.navigationBar.frame.height - 5)
// navController.navigationBar.addSubview(titleLabel)
// titleLabel.anchor(top: nil, left: navController.navigationBar.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
// titleLabel.centerYAnchor.constraint(equalTo: navController.navigationBar.centerYAnchor).isActive = true
return navController
}
您可以探索创建您的自定义导航控制器并在您的视图控制器中调用;更容易,自定义本机导航控制器很难,但您可以在本机元素中搜索子视图。
使用我的分机 iOS 13 Swift 5 测试
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}}
使用方法:
configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)
设置 ViewController-based 状态栏......在 info.plist 中设置为 NO 如果你想要浅色内容
如果您不想使用 largeTitles,请将其设置为 false
用于透明更改 navBarAppearance.configureWithOpaqueBackground() in:
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true
在调用中将背景颜色设置为.clear
更新:如果你想在第一个控制器中使用导航控制器和大标题,请不要忘记像这样在场景委托中设置启动控制器:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC
希望对您有所帮助:)
我希望我的应用程序有大标题,但是,当用户滚动时,我不希望 regular/normal 导航栏出现。我只希望大栏与 tableview 一起向上滚动。如果不在导航栏中制作自定义视图,这可能吗?我已经使用自定义视图成功地完成了它,但是它不是那么流畅并且在里面放一个 UISearchController 很痛苦。
func createNavController(vc: UIViewController, title: String, image: UIImage, tag: Int) -> UINavigationController {
let navController = UINavigationController(rootViewController: vc)
navController.navigationBar.prefersLargeTitles = true
navController.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navController.navigationBar.shadowImage = UIImage()
navController.navigationBar.tintColor = .headerColor
navController.tabBarItem = UITabBarItem(title: title, image: image.withRenderingMode(.alwaysTemplate), tag: tag)
navController.navigationBar.topItem?.title = title
navController.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.headerColor, .font: UIFont.customHeaderFont(size: 25)]
navController.navigationBar.largeTitleTextAttributes = [.foregroundColor: UIColor.headerColor, .font: UIFont.customHeaderFont(size: 40)]
// let titleLabel = UILabel()
// titleLabel.text = title
// titleLabel.textColor = .headerColor
// titleLabel.font = UIFont.customHeaderFont(size: navController.navigationBar.frame.height - 5)
// navController.navigationBar.addSubview(titleLabel)
// titleLabel.anchor(top: nil, left: navController.navigationBar.leftAnchor, bottom: nil, right: nil, paddingTop: 0, paddingLeft: 20, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
// titleLabel.centerYAnchor.constraint(equalTo: navController.navigationBar.centerYAnchor).isActive = true
return navController
}
您可以探索创建您的自定义导航控制器并在您的视图控制器中调用;更容易,自定义本机导航控制器很难,但您可以在本机元素中搜索子视图。
使用我的分机 iOS 13 Swift 5 测试
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}}
使用方法:
configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)
设置 ViewController-based 状态栏......在 info.plist 中设置为 NO 如果你想要浅色内容
如果您不想使用 largeTitles,请将其设置为 false
用于透明更改 navBarAppearance.configureWithOpaqueBackground() in:
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true
在调用中将背景颜色设置为.clear
更新:如果你想在第一个控制器中使用导航控制器和大标题,请不要忘记像这样在场景委托中设置启动控制器:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC
希望对您有所帮助:)