你可以同时自定义导航栏和状态栏的颜色,以及视图控制器的标题吗?

huangapple go评论61阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年2月10日 14:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定