如何修改UINavigationController中Prompt的字体、字号及颜色?
Hey there! I get it—trying to customize the UINavigationBar's prompt text (your 'Consulenze' label) and having your previous attempts fall flat is super frustrating. Let’s walk through some reliable methods that should work, plus troubleshoot why your earlier tries might’ve failed.
Starting with iOS 13, Apple introduced UINavigationBarAppearance which gives precise control over navigation bar styling—including the prompt. This is the recommended approach for modern iOS versions:
// Create and configure a custom appearance object let navBarAppearance = UINavigationBarAppearance() // Set your desired prompt text attributes navBarAppearance.promptTextAttributes = [ .font: UIFont(name: "YourCustomFont", size: 14)!, // Replace with your font name/size .foregroundColor: UIColor.systemBlue // Swap in your preferred color ] // Apply the appearance to your navigation controller navigationController?.navigationBar.standardAppearance = navBarAppearance // Also apply to scroll edge appearance if you want consistency when scrolling navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
Pro tip: Double-check that your custom font is properly added to your project (include it in Info.plist under Fonts provided by application)—a misspelled font name will silently fall back to the system font.
If the appearance method still doesn’t do the trick (or you need more layout flexibility), you can replace the default prompt with a fully custom UIView:
// Create your custom prompt label let customPromptLabel = UILabel() customPromptLabel.text = "Consulenze" customPromptLabel.font = UIFont(name: "YourCustomFont", size: 14) customPromptLabel.textColor = UIColor.systemBlue customPromptLabel.textAlignment = .center customPromptLabel.numberOfLines = 0 // Allow wrapping if needed // Assign to promptView (iOS 16+) if #available(iOS 16.0, *) { navigationController?.navigationBar.promptView = customPromptLabel } else { // For iOS <16, add the label as a subview to the navigation bar customPromptLabel.translatesAutoresizingMaskIntoConstraints = false navigationController?.navigationBar.addSubview(customPromptLabel) // Position it like the default prompt NSLayoutConstraint.activate([ customPromptLabel.centerXAnchor.constraint(equalTo: navigationController!.navigationBar.centerXAnchor), customPromptLabel.bottomAnchor.constraint(equalTo: navigationController!.navigationBar.titleBarGuide.topAnchor, constant: -4), customPromptLabel.leadingAnchor.constraint(greaterThanOrEqualTo: navigationController!.navigationBar.leadingAnchor, constant: 16), customPromptLabel.trailingAnchor.constraint(lessThanOrEqualTo: navigationController!.navigationBar.trailingAnchor, constant: -16) ]) }
This method lets you tweak every detail of the prompt—from padding to text alignment—without relying on system appearance settings.
- Wrong Attribute Key: If you used
titleTextAttributesinstead ofpromptTextAttributes, that only affects the navigation bar title, not the prompt. - iOS Version Misalignment: Older methods (like directly modifying navigation bar subviews) break on iOS 13+ due to the new navigation bar structure.
- Custom Navigation Bar Subclass: If you’re using a custom
UINavigationBarsubclass, it might be overriding or hiding the prompt area. Check if there’s code that modifies subviews or suppresses the prompt. - Late Appearance Setup: If you applied appearance settings after the navigation controller was already initialized, the changes might not take effect. Try setting them early (like in
AppDelegate/SceneDelegateorviewWillAppearof your view controller).
内容的提问来源于stack exchange,提问作者Elia Crocetta

