导航栏中可以只有大标题吗?

问题描述 投票:0回答:3

我希望我的应用程序具有大标题,但是,当用户滚动时,我不希望出现常规/普通导航栏。我只想让大栏与桌面视图一起向上滚动。如果不在导航栏中创建自定义视图,这可能吗?我已经使用自定义视图成功完成了此操作,但它并不那么流畅,并且将 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
}
swift uinavigationcontroller uikit uinavigationbar
3个回答
1
投票

使用我的扩展 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)

如果你想要轻量内容,请在info.plist中将基于ViewController的状态栏......设置为NO

如果你不想要 largeTitles 将其设置为 false

对于半透明更改 navBarAppearance.configureWithOpaqueBackground() :

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

希望这有帮助:)


0
投票

您可以探索创建自定义导航控制器并在视图控制器中调用;更容易,自定义本机导航控制器很困难,但是您可以在本机元素中搜索子视图来做到这一点。


0
投票

我的简单解决方案:(只需将小标题字体设置为零,简单的方法)

self.title = "Title text"
    self.navigationController?.navigationBar.prefersLargeTitles = true

    navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 0)]
    
© www.soinside.com 2019 - 2024. All rights reserved.