ios - 使用 SwiftUI 为 UINavigationBar 自定义背景颜色
问题描述
我正在尝试使用 SwiftUI 为 UINavigationBar 使用我自己的背景颜色,但我无法使用以下代码更改它:
let attrs = [
NSAttributedString.Key.foregroundColor: UIColor.blue,
NSAttributedString.Key.font: UIFont(name: "AbrilFatface-Regular", size: 24)!]
UINavigationBar.appearance().titleTextAttributes = attrs
UINavigationBar.appearance().backgroundColor = UIColor(red:253/255, green:213/255, blue:211/255, alpha:1.0)
解决方案
您的代码与 SwiftUI 无关。要更改 UINavigationBar 外观,您必须使用 SwiftUI 的前身 UIKit。下面的代码将全局更改导航栏。
// Instantiate new UINavigationBarAppearance object
let navigationBarAppearance = UINavigationBarAppearance()
// Set background colour
navigationBarAppearance.backgroundColor = .black
// Set text color for title
navigationBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
// Set text color for large title
navigationBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
// Assign appearance
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
在AppDelegate.swift文件中调用它 indside application(_:didFinishLaunchingWithOptions:)方法。此代码适用于iOS 13+。
如果您需要,这里是旧版自定义的苹果文档。
推荐阅读
- php - 我正在尝试从经过身份验证的用户配置文件的用户表中传递用户的 ID
- performance - Vue 3:如何实现修改 DOM 中所有输入字段的功能?
- javascript - Mulitdatepicker - 从数组中设置的日期中删除 .ui-state-highlight
- xml - 来自数组响应的 Oracle 12c PLSQL SOAP EXTRACTVALUE
- awk - 仅在下一列中更改与另一个模式匹配的模式
- utf-8 - Mulesoft SFTP 使用 BOM 写入 UTF-8 编码的 .csv 文件
- node.js - nodemailer SMTP 服务器连接问题(错误:连接 ETIMEDOUT)但它适用于不在我的另一个系统
- ios - 1.有没有办法在ios中收集双卡运营商信息(物理+ esim)?
- php - 将数据从小部件搜索传输到 SEO 工具 我正在使用我的网站
- episerver - 如何在 EPiServer 中自定义 linkItemCollection 的默认视图