英文:
How can I customize the color of the navigation bar and status bar and the title of the viewController at the same time?
问题
I'm a beginner in swift and I'm trying to change the background color of the navigation to red at the same time as the background color of the status bar
Meanwhile, I have a viewController. I want to change the title of the navigation above to my desired font and size. I browsed various articles on the Internet and brought the method into my own code, but it didn't work. I want to know what's wrong
Below is my code
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let navigationbarAppearance = UINavigationBarAppearance()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
navigationbarAppearance.configureWithOpaqueBackground()
navigationbarAppearance.backgroundColor = UIColor.red
UINavigationBar.appearance().standardAppearance = navigationbarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = UINavigationBar.appearance().standardAppearance
let window = UIWindow(frame: UIScreen.main.bounds)
let naVC = UINavigationController(rootViewController: ViewController())
naVC.navigationBar.backgroundColor = .red
window.rootViewController = naVC
window.makeKeyAndVisible()
self.window = window
return true
}
}
import UIKit
class WinLoseViewController: UIViewController {
var backBtn : UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGray6
navigationItem.title = "WinLose"
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.black
]
setBackBtn()
}
func setBackBtn() {
backBtn = UIBarButtonItem(title: "<back", style: .plain, target: self, action: #selector(backAction))
backBtn.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18)!], for: .normal)
backBtn.tintColor = .white
navigationItem.leftBarButtonItem = backBtn
}
@objc func backAction() {
navigationController?.popViewController(animated: true)
}
}
Thanks a lot
英文:
I'm a beginner in swift and I'm trying to change the background color of the navigation to red at the same time as the background color of the status bar
Meanwhile, I have a viewController. I want to change the title of the navigation above to my desired font and size. I browsed various articles on the Internet and brought the method into my own code, but it didn't work. I want to know what's wrong
Below is my code
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
let navigationbarAppearance = UINavigationBarAppearance()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
navigationbarAppearance.configureWithOpaqueBackground()
navigationbarAppearance.backgroundColor = UIColor.red
UINavigationBar.appearance().standardAppearance = navigationbarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = UINavigationBar.appearance().standardAppearance
let window = UIWindow(frame: UIScreen.main.bounds)
let naVC = UINavigationController(rootViewController: ViewController())
naVC.navigationBar.backgroundColor = .red
window.rootViewController = naVC
window.makeKeyAndVisible()
self.window = window
return true
}
import UIKit
class WinLoseViewController: UIViewController {
var backBtn : UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemGray6
navigationItem.title = "WinLose"
navigationController?.navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.black
]
setBackBtn()
}
func setBackBtn() {
backBtn = UIBarButtonItem(title: "<back", style: .plain, target: self, action: #selector(backAction))
backBtn.setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 18)!], for: .normal)
backBtn.tintColor = .white
navigationItem.leftBarButtonItem = backBtn
}
@objc func backAction() {
navigationController?.popViewController(animated: true)
}
}
Thanks a lot
答案1
得分: 0
你可以只设置自定义的 navigationItem.titleView
,如果它只用于单个屏幕:
let titleLabel = UILabel()
titleLabel.text = "WinLose"
titleLabel.attributedText = .init(string: "WinLose", attributes: [
.font: UIFont.boldSystemFont(ofSize: 10),
.foregroundColor: UIColor.black
])
navigationItem.titleView = titleLabel
如果你想要整个应用程序中的标题都像这样,那么你需要更改外观属性:
navigationbarAppearance.titleTextAttributes = [
.font: UIFont.boldSystemFont(ofSize: 10),
.foregroundColor: UIColor.black
]
更新
如果你想为应用程序中不同的导航栏设置不同的标题,那么你应该为每个视图控制器设置一个 titleLabel
。你可以将这段代码放在 viewDidLoad
中。不要忘记在 AppDelegate
中移除外观修改:
let titleLabel = UILabel()
titleLabel.text = "WinLose"
titleLabel.attributedText = .init(string: "WinLose", attributes: [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.black
])
navigationItem.titleView = titleLabel
英文:
You can just set your custom navigationItem.titleView
if it's for meant for a single screen
let titleLabel = UILabel()
titleLabel.text = "WinLose"
titleLabel.attributedText = .init(string: "WinLose", attributes: [
.font: UIFont.boldSystemFont(ofSize: 10),
.foregroundColor: UIColor.black
])
navigationItem.titleView = titleLabel
If you want your title to be like that across the app then you need to change the appearance property
navigationbarAppearance.titleTextAttributes = [
.font: UIFont.boldSystemFont(ofSize: 10),
.foregroundColor: UIColor.black
]
UPDATE
If you want to set a different title for different navigation bars across the app then you should set a titleLabel
for each view controller. You can put this code in the viewDidLoad
. Don't forget to remove appearance modification in the AppDelegate
.
let titleLabel = UILabel()
titleLabel.text = "WinLose"
titleLabel.attributedText = .init(string: "WinLose", attributes: [
NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10),
NSAttributedString.Key.foregroundColor: UIColor.black
])
navigationItem.titleView = titleLabel
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论