0

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

Fabio
  • 5,432
  • 4
  • 22
  • 24
yude
  • 5
  • 3

1 Answers1

0

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
Bulat Yakupov
  • 440
  • 4
  • 13
  • Thank you very much for your reply! But what if I want my entire viewcontroller to be the same color, but my title to be styled differently? – yude Feb 13 '23 at 02:22
  • No problem. Just set view.backgroundColor = – Bulat Yakupov Feb 13 '23 at 07:01
  • Sorry, I think I didn't express my meaning clearly. Actually for example I have 5 ViewControllers in my project. I need their navigationBar and statusBar to have the same color, but the title styles of these 5 viewControllers are different, some may be bold, some may be thin, and some text size is 20 or 30, so how do I set it Woolen cloth? Here I want to thank you again for your reply =)) – yude Feb 13 '23 at 07:15
  • Oh. Ok, let me modify my answer a bit – Bulat Yakupov Feb 13 '23 at 07:26
  • Please take a look – Bulat Yakupov Feb 16 '23 at 22:42