swift - 使用资产中的颜色集更改导航栏标题索引颜色
问题描述
我必须将导航栏标题的最后 5 个字母更改为资产中的颜色。
struct MainView: View {
var body: some View {
NavigationView {
Text("Hello, World!")
.navigationBarTitle("HelloWorld")
}
}
init() {
// UIColor.red should be Color("orange1") and only for the last 5 letters (World)
UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red]
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.red]
}
}
解决方案
我这样做的一种方法是将根视图添加到顶部对齐的 ZStack 中,并在根视图之后添加自定义中心视图。然后,我只是将文本放在 HStack 中,并获得您想要的标题的前缀和后缀,并将颜色更改为您想要的任何颜色。有不同的方法可以解决这个问题,但这就是我为这个例子选择的方法。最终结果看起来像这样。
var navBarTitle: String = "HelloWorld"
var body: some View {
ZStack(alignment: .top){
//Root view with empty Title
NavigationView {
Text("Hello, World")
.navigationBarTitle("",displayMode: .inline)
}
VStack{
HStack{
Text(navBarTitle.prefix(5))
.font(.largeTitle)
.foregroundColor(.red)
Text(navBarTitle.suffix(5))
.font(.largeTitle)
.foregroundColor(.blue)
}
}
}
}
不确定这是否正是您要寻找的,但希望对您有所帮助!
推荐阅读
- android - 尝试在空对象引用上调用虚拟方法“boolean android.media.AudioManager.isBluetoothA2dpOn()”
- jquery - 如何在点击时隐藏手风琴
- pact - 在提供者服务中,我们可以有两个或多个具有相同状态或提供者状态的交互吗?
- reactjs - 如何使用 react 和 typescript 解决程序化导航问题
- r - 分层数据聚合和操作
- android - 获取 java.lang.SecurityException:DownloadManager:用户 10173 和当前进程都没有 android.permission.ACCESS_ALL_DOWNLOADS
- list - 如何在 q# 中选择列表的唯一元素?
- nasm - 终端“-bash:名称:找不到命令”中显示错误
- alignment - After Effects - 对齐多个对象/图层
- excel-formula - ui网格在导出excel单元格上显示超链接