使用 SwiftUI 设置导航栏标题字体 [英] Set the Navigation Bar Title Font with SwiftUI

查看:102
本文介绍了使用 SwiftUI 设置导航栏标题字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 SwiftUI 问题,而不是 UIKit 问题.:)

我正在尝试使用 SwiftUI 为导航栏标题设置不同的字体.我的怀疑是这还不支持.这是我尝试过的:

I'm trying to set a different font for the navigation bar title using SwiftUI. My suspicion is that this isn't supported yet. Here's what I've tried:

var body: some View {
  NavigationView {
    .navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
  }
}

无论我如何使用 .font 设置,它都不会改变文本.我还尝试设置自定义字体并删除 displayMode 属性.

No matter what I do with the .font settings, it doesn't change the text. I've also tried setting a custom font and removing the displayMode property.

有没有人能够让它发挥作用?

Has anyone been able to get this to work?

推荐答案

在 SwiftUI 中,此时我们无法直接更改 navigationBarTitle 字体,但您可以像这样更改导航栏外观,

In SwiftUI, at this point we can not change the navigationBarTitle font directly, but you can change navigationBar appearance like this,

struct YourView: View {
    init() {
        //Use this if NavigationBarTitle is with Large Font
        UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]

        //Use this if NavigationBarTitle is with displayMode = .inline
        //UINavigationBar.appearance().titleTextAttributes = [.font : UIFont(name: "Georgia-Bold", size: 20)!]
    }
    var body: some View {
        NavigationView {
            Text("Hello World!")
              .navigationBarTitle(Text("Dashboard").font(.subheadline), displayMode: .large)
            //.navigationBarTitle (Text("Dashboard"), displayMode: .inline)
        }
    }
}

希望对你有帮助.谢谢!!

I hope this will help you. Thanks!!

这篇关于使用 SwiftUI 设置导航栏标题字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆