简体   繁体   English

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

[英]Is it possible to have only large titles in the navigation bar?

I want my application to have large titles, however, when the user scrolls, I don't want the regular/normal navigation bar to appear.我希望我的应用程序具有大标题,但是,当用户滚动时,我不希望出现常规/普通导航栏。 I just want the large bar to scroll up along with the tableview.我只希望大栏与 tableview 一起向上滚动。 Is this possible without making custom views in the navigation bar?如果不在导航栏中创建自定义视图,这可能吗? I have done it successfully with custom views, but it isn't as fluid and putting a UISearchController inside is a pain.我已经使用自定义视图成功地完成了它,但它不是那么流畅,并且将 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
}

Use my extension iOS 13 Swift 5 tested使用我的扩展 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
    }
}}

How to use:如何使用:

configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)

Set ViewController-based status bar...... to NO in info.plist if you want light Content如果你想要轻量内容,在 info.plist 中将基于 ViewController 的状态栏......设置为 NO

If you don't want largeTitles set it to false如果您不希望 largeTitles 将其设置为 false

for tranlsucent change navBarAppearance.configureWithOpaqueBackground() in:对于半透明更改 navBarAppearance.configureWithOpaqueBackground() 在:

navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true

in the call set background color to .clear在通话中将背景颜色设置为 .clear

UPDATE : If you want to start with navigation controller and large Titles at first controller, don't forget to set launch controller in Scene Delegate like this:更新:如果你想在第一个控制器上使用导航控制器和大标题,不要忘记在场景委托中设置启动控制器,如下所示:

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC

hope this help :)希望这有帮助:)

You can probe create your custom navigation controller and called in your view controllers;您可以探测创建您的自定义导航控制器并在您的视图控制器中调用; is more easy, customize the native navigation controller is hard, but you can do it scouring subviews in the natives elements.更容易,自定义本机导航控制器很难,但您可以在本机元素中搜索子视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM