Since I upgraded my iPad operating system, the title of the UITabBar of my app is showing truncated, as shown in the screenshot.
I have tried some methods, but I have not found the correct solution.
Hope someone can help me.
And Here is code:
func setupTabBar() {
if #available(iOS 13, *) {
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()
appearance.backgroundImage = UIImage(color: .white)
appearance.shadowImage = UIImage(color: .clear)
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.stackedLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.inlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.inlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
appearance.compactInlineLayoutAppearance.selected.titleTextAttributes = selectedAttrs
appearance.compactInlineLayoutAppearance.normal.titleTextAttributes = normalAttrs
UITabBar.appearance().standardAppearance = appearance
} else {
tabBar.backgroundImage = UIImage(color: .white)
tabBar.shadowImage = UIImage(color: .clear)
}
if #available(iOS 15, *) {
UITabBar.appearance().scrollEdgeAppearance = UITabBar.appearance().standardAppearance
}
}
For some reason, it seems that setting the titleTextAttributes is what causes the problem to happen with inlineLayoutAppearance, and including the default paragraph style of NSParagraphStyle.default fixes it.
For your code, the following changes should fix it (as of iOS 15.0).
let normalAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.gray, .paragraphStyle: NSParagraphStyle.default]
let selectedAttrs: [NSAttributedString.Key: Any] = [.foregroundColor: ThemeColor.red, .paragraphStyle: NSParagraphStyle.default]
For those that cannot have a default paragraph style, setting the attributes for most States got the OS to size the labels correctly for me.
Swift 5.0:
UITabBarItem.appearance()
.setTitleTextAttributes(
customAttributesWithCustomParagraphStyle,
for: [
.normal,
.highlighted,
.disabled,
.selected,
.focused,
.application,
]
)
Without setting it for at least .normal and .selected, the UITabBarItem title gets truncated when used with custom attributes.
Related
I'm getting a transparent navigation bar in share modal controllers on iOS 15. But navbar appearance were properly set already and is not changed at any part of the app. This issue only happens from uiactivity share modals and not all .
Appearance setup
// AppDelegate.swift
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
appearance.shadowImage = nil
appearance.shadowColor = .clear
appearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
navBarAppearance.barTintColor = .white
navBarAppearance.standardAppearance = appearance
navBarAppearance.scrollEdgeAppearance = appearance
navBarAppearance.compactAppearance = appearance
} else {
navBarAppearance.isTranslucent = false
navBarAppearance.backgroundColor = .white
// Remove bottom line aka shadow
navBarAppearance.shadowImage = UIImage()
navBarAppearance.setBackgroundImage(UIImage(), for: .default)
navBarAppearance.tintColor = .clear
navBarAppearance.layer.shadowOpacity = 0
// Update bar title font
navBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
}
}
What I've tried and it does nothing.
func presentShareActivity(items: [Any]) {
//let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)
let activityVC = UIActivityViewController(activityItems: ["test share copy"], applicationActivities: nil)
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
appearance.shadowImage = nil
appearance.shadowColor = .clear
appearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.BananaGrotesk(weight: .semibold, size: 16.0),
NSAttributedString.Key.foregroundColor: UIColor.Function.black
]
activityVC.navigationItem.scrollEdgeAppearance = appearance
activityVC.navigationItem.standardAppearance = appearance
activityVC.navigationItem.compactAppearance = appearance
activityVC.navigationController?.navigationBar.isTranslucent = false
activityVC.navigationController?.navigationBar.scrollEdgeAppearance = appearance
activityVC.navigationController?.navigationBar.standardAppearance = appearance
activityVC.navigationController?.navigationBar.compactAppearance = appearance
}
present(activityVC, animated: true)
}
Screenshots
Navigation bar is transparent but turns opaque on scroll. I want this to be opaque
Interestingly the photo below uses the same code but has an opaque navbar
Try this:
activityVC.view.backgroundColor = UIColor.systemBackground
It worked for me. š
Iām using Xcode 11.4 and iOS 13.4.
I have set navigation bar title custom font using UINavigatinBar.appearance()
And it works correctly but on iOS 13+ when i try to push to another VC and then comeback to the parent VC, the parent VC title font suddenly has been set to default font and after a second it changes back to the custom font.
Below is a gif of the problem:
nav bar font problem
iOS 13.+ has UINavigationBarAppearance approach to customize NavigationBar-Title & NavigationBar-BarButtonItems
Check this code, might help you
let titleFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 20)!, NSAttributedString.Key.foregroundColor: UIColor.white ]
let barButtonFontAttrs = [ NSAttributedString.Key.font: UIFont(name: "custom-font-name", size: 14)! ]
UINavigationBar.appearance().tintColor = UIColor.white // bar icons
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .red // If you want different nav background color other than white
appearance.titleTextAttributes = titleFontAttrs
appearance.largeTitleTextAttributes = titleFontAttrs // If your app supports largeNavBarTitle
UINavigationBar.appearance().isTranslucent = false
appearance.buttonAppearance.normal.titleTextAttributes = barButtonFontAttrs
appearance.buttonAppearance.highlighted.titleTextAttributes = barButtonFontAttrs
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().barTintColor = .red // bar background
UINavigationBar.appearance().titleTextAttributes = titleFontAttrs
UINavigationBar.appearance().isTranslucent = false
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes(barButtonFontAttrs, for: .highlighted)
}
Here you go, manage it in viewDidAppear:
let lblTitle = UILabel()
let titleAttribute: [NSAttributedString.Key: Any] = [.font: UIFont.boldSystemFont(ofSize: 21),
.foregroundColor: UIColor.black]
let attributeString = NSMutableAttributedString(string: "Navigation Title", attributes: titleAttribute)
lblTitle.attributedText = attributeString
lblTitle.sizeToFit()
navigationItem.titleView = lblTitle
UIBarButtonItem.appearance().setTitleTextAttributes() doesn't work on iOS 13.
The way iOS handles global appearances appears to have changed. You'll need to add a condition check for iOS 13 and above and then add your attributes as shown below.
if #available(iOS 13.0, *) {
let standard = UINavigationBarAppearance()
standard.configureWithTransparentBackground()
// TITLE STYLING
standard.titleTextAttributes = [.foregroundColor: UIColor.white, .font: "FONTNAME"]
// NAV BUTTON STYLING
let button = UIBarButtonItemAppearance(style: .plain)
button.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
standard.buttonAppearance = button
let done = UIBarButtonItemAppearance(style: .done)
done.normal.titleTextAttributes = [.foregroundColor: .white, .font: "FONTNAME"]
standard.doneButtonAppearance = done
UINavigationBar.appearance().standardAppearance = standard
} else {
// Your previous styling here for 12 and below
}
Check out this post for more. I found it really useful in understanding the new updates.
This question already has an answer here:
iOS 12.0 : Is there a way to set MFMailComposeViewController navigation bar title's text to white?
(1 answer)
Closed 1 year ago.
How can I change the title color of UINavigationBar in MFMailComposeViewController in iOS 12?
This is what I am doing:
import MessageUI
extension MFMailComposeViewController {
open override func viewDidLoad() {
super.viewDidLoad()
navigationBar.isTranslucent = false
navigationBar.isOpaque = false
navigationBar.barTintColor = .white
navigationBar.tintColor = .white
navigationBar.titleTextAttributes = [.foregroundColor: UIColor.white]
}
}
In iOS 10 works:
In iOS 11 works:
In iOS 12 is not working:
/// UINavigationBar
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.backgroundColor = .red
navBarAppearance.titleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.systemFontSize
]
navBarAppearance.largeTitleTextAttributes = [
.foregroundColor: UIColor.white,
.font: UIFont.smallSystemFontSize
]
let barButtonAppearance = UIBarButtonItem.appearance()
barButtonAppearance.setTitleTextAttributes([.font: UIFont.systemFontSize], for: .normal)
navBarAppearance.isTranslucent = false
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = navBarAppearance.backgroundColor
appearance.titleTextAttributes = navBarAppearance.titleTextAttributes ?? [:]
appearance.largeTitleTextAttributes = navBarAppearance.largeTitleTextAttributes ?? [:]
navBarAppearance.standardAppearance = appearance
navBarAppearance.scrollEdgeAppearance = appearance
}
I tried all the way to change the title color, however it doesn't work
Before presenting the mailcomopser controller
I changed the background color to white
and buttons color to black
Here is the code below:
UINavigationBar.appearance().setBackgroundImage(UIImage(), for: UIBarPosition.any, barMetrics: UIBarMetrics.default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.white
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().isTranslucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = UIColor.white
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.clear], for: .disabled)
UIBarButtonItem.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
In your AppDelegate.swift file in the didFinishLaunchingWithOptions launchOptions block
TRY THIS:
let navigationBarAppearace = UINavigationBar.appearance()
navigationBarAppearace.barTintColor = .blue //your desired color
navigationBarAppearace.tintColor = .white //your button etc color
navigationBarAppearace.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //your Title Text color
titleView still works fine. Just create view with label in story board like this, with such font, as U need and set it as title view.
if let view = Bundle.main.loadNibNamed("WTitleView", owner: self, options: nil)?.first as? UIView {
navigationItem.titleView = view
}
I'm using the new enlarged navigation bar titles in iOS 11. But I can't seem to be able to change the textColor.
I tried doing:
self.navigationController.navigationBar.titleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
This didn't do anything. Any ideas?
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
I think it's still a bug in Xcode 9 beta 6.
I found different "solutions" for it:
It's possible to change the color of the title if you put this in the AppDelegate:
if #available(iOS 11.0, *) {
UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue]
}
Other way is to set the color in your Controller's viewDidLoad, but the secret to make it work is to set the font also:
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.blue, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 31, weight: UIFont.Weight.bold) ]
}
Hope it helps you.
Regards!
iOS 11
Objective-C
if (#available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = #{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
Swift 5.6.1
In my Swift 5.6.1 and iOS 15.6.1 the following code worked only.
Add the following codes in ViewDidLoad()
let appearance = UINavigationBarAppearance(idiom: .phone)
// Add the color you want in your title
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
// Add the color you want as your navigation bar color. Otherwise it shows White by default
appearance.backgroundColor = .purple
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
Swift 4.2
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
with named color
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor(named: "Teal") ?? UIColor.black]
Swift up through Swift 3.2 (not Swift 4+)
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
let largeTitleTextAttributes: [NSAttributedString.Key: Any] = [.foregroundColor: UIColor.gray20, .font: UIFont.systemFont(ofSize: 24.0, weight: .bold)]
if #available(iOS 15, *) {
let navigationBar = navigationController.navigationBar
let appearance = navigationBar.standardAppearance
appearance.largeTitleTextAttributes = largeTitleTextAttributes
navigationBar.standardAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
} else {
navigationController.navigationBar.largeTitleTextAttributes = largeTitleTextAttributes
}