Changing UIActivityViewController share modal navigation bar to opaque on iOS15 - ios

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. šŸ™‚

Related

Move navigation bar to center when tab bar scrolled

I want to create navigation bar like in contacts application on iPhone, seems like default solution but I can't find any code examples. What I want to do is to move my navigation bar from the left to center and change font size when user scrolls tab bar. Please see screenshots:
Large title and title of navigation bar
class BaseNavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 15.0, *) {
let scrollAppearance = UINavigationBarAppearance()
scrollAppearance.shadowColor = .white
scrollAppearance.backgroundColor = .white
let navigationBarAppearance = UINavigationBarAppearance()
navigationBarAppearance.configureWithDefaultBackground()
navigationBarAppearance.backgroundColor = .white
navigationBarAppearance.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
UINavigationBar.appearance().backIndicatorImage = UIImage(named: "back-arrow")
UINavigationBar.appearance().standardAppearance = navigationBarAppearance
UINavigationBar.appearance().compactAppearance = navigationBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = scrollAppearance
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
} else {
navigationBar.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBar.tintColor = .black
navigationBar.prefersLargeTitles = true
navigationBar.isTranslucent = false
navigationItem.largeTitleDisplayMode = .automatic
navigationBar.barTintColor = .white
}
}
}
this line is responsible for showing this large left title in navigation bar
navigationItem.largeTitleDisplayMode = .automatic
this is the font for largeTitle and small center title
navigationBarAppearance.largeTitleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 26),
NSAttributedString.Key.foregroundColor: UIColor.black
]
navigationBarAppearance.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: 17),
NSAttributedString.Key.foregroundColor: UIColor.black
]
how to use it
let vc = ViewController()
let embeddedNav = BaseNavigationController(rootViewController: vc)
present(embeddedNav,animate:true)
set the title in view did load method of vc
navigationItem.title = "Some title"
Solution:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
configureNavigationBar(largeTitleColor: .black, backgoundColor: .spWhite, tintColor: .white, title: "Your Title", preferredLargeTitle: true)
}
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationItem.largeTitleDisplayMode = .always
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}
}

Problem with NavBar in iOS 13 doesn't show [duplicate]

On ios13, with iphone x, the large title navigation does not cover the status bar however when scrolling and transitioning into the traditional nav bar, it works perfectly. This doesn't affect devices without the notch.
Large titles
Traditional navigation bar
It's all embedded within a navigation controller so i'm lost as to why this is happening. Cheers
The official way to customize the UINavigationBar, pre iOS 13, is this:
// text/button color
UINavigationBar.appearance().tintColor = .white
// background color
UINavigationBar.appearance().barTintColor = .purple
// required to disable blur effect & allow barTintColor to work
UINavigationBar.appearance().isTranslucent = false
iOS 13 has changed how navigation bars work, so you'll need to do things slightly differently to support both old & new:
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = .purple
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().standardAppearance = appearance
UINavigationBar.appearance().compactAppearance = appearance
UINavigationBar.appearance().scrollEdgeAppearance = appearance
} else {
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().barTintColor = .purple
UINavigationBar.appearance().isTranslucent = false
}
Use my extension iOS 13 Swift 5 tested
extension UIViewController {
func configureNavigationBar(largeTitleColor: UIColor, backgoundColor: UIColor, tintColor: UIColor, title: String, preferredLargeTitle: Bool) {
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.titleTextAttributes = [.foregroundColor: largeTitleColor]
navBarAppearance.backgroundColor = backgoundColor
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.compactAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = preferredLargeTitle
navigationController?.navigationBar.isTranslucent = false
navigationController?.navigationBar.tintColor = tintColor
navigationItem.title = title
} else {
// Fallback on earlier versions
navigationController?.navigationBar.barTintColor = backgoundColor
navigationController?.navigationBar.tintColor = tintColor
navigationController?.navigationBar.isTranslucent = false
navigationItem.title = title
}
}}
How to use:
configureNavigationBar(largeTitleColor: .yourColor, backgoundColor: .yourColor, tintColor: .yourColor, title: "yuorTitle", preferredLargeTitle: true)
Set ViewController-based status bar...... to NO in info.plist if you want light Content
If you don't want largeTitles set it to false
for tranlsucent change navBarAppearance.configureWithOpaqueBackground() in:
navBarAppearance.configureWithDefaultBackground()
navigationController?.navigationBar.isTranslucent = true
in the call set background color to .clear
UPDATE:
If you want to start with navigation controller and large Titles at first controller, don't forget to set launch controller in Scene Delegate like this:
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let vC = UINavigationController(rootViewController: YourFirstViewController())
window?.rootViewController = vC
hope this help :)
For full application navigation bar support please add this extension inside your code.
import UIKit
extension UIViewController {
open func showNavigationBar(_ large: Bool,
_ animated: Bool,
titleColor: UIColor,
barTintColor: UIColor,
fontSize: CGFloat) {
navigationController?.navigationBar.barTintColor = barTintColor
navigationController?.navigationBar.backgroundColor = barTintColor
navigationController?.navigationBar.isTranslucent = true
self.navigationController?.setNavigationBarHidden(false, animated: animated)
if large {
self.navigationController?.navigationBar.prefersLargeTitles = true
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = barTintColor
appearance.titleTextAttributes = [.foregroundColor: titleColor]
appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.compactAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
} else {
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: fontSize)!]
}
} else {
self.navigationController?.navigationBar.prefersLargeTitles = false
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: titleColor,
NSAttributedString.Key.font: UIFont(resource: R.font.robotoMedium, size: 20.0)!]
}
}
}
And Then call this method simply
self.showNavigationBar(true, true, titleColor: UIColor.blue, barTintColor: UIColor.red, fontSize: 32.0)

Navigation Bar title font problem on ios 13

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

How to set style of the statusbar embedded in a navigation controller on iOS13?

as many iOS devs out there i'm facing some issues with iOS 13 update.
One of these was the different management of the status bar style
On iOS 12 i used to set the navigation bar style like this
self.navigationController?.navigationBar.barStyle = .black
which affects the status bar style, setting it to white (because the navigation bar style is black);
but it doesn't seem to work on iOS 13, i guess it has something to deal with
UINavigationBarAppearance()
class
I configured my navigation bar for each ViewController like this:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.accessibilityTextualContext = .sourceCode
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = .brownCircles
navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
navBarAppearance.shadowColor = nil // remove navigationBar Bottom border
self.navigationController?.navigationBar.standardAppearance = navBarAppearance
self.navigationController?.navigationBar.compactAppearance = navBarAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
} else {
self.navigationController?.navigationBar.barTintColor = .blue
self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black
so far so good, but
self.navigationController?.navigationBar.barStyle = .black
works just on iOS 12, nothing happens on iOS 13 the status bar still looks black instead of white
Did anyone face this issue?
Finally i figured out!
the magic code to set a light status bar text is:
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
of course if you want to change to dark text i have to set it to .light.
Some things to notice:
This code:
if #available(iOS 13.0, *) {
overrideUserInterfaceStyle = .dark
}
although it should set the entire view and subviews to dark, doesn't seem to affect the status bar.
You can also use:
UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
but of course is deprecated so i'd recommend other ways
You still need:
self.navigationController?.navigationBar.barStyle = .black,
but put it AFTER the UINavigationBarAppearance() settings and after the self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark.
Final code will look like this:
if #available(iOS 13.0, *) {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.accessibilityTextualContext = .sourceCode
navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
navBarAppearance.backgroundColor = .brownCircles
navBarAppearance.shadowImage = nil // remove navigationBar Bottom border
navBarAppearance.shadowColor = nil // remove navigationBar Bottom border
self.navigationController?.navigationBar.standardAppearance = navBarAppearance
self.navigationController?.navigationBar.compactAppearance = navBarAppearance
self.navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
self.navigationController?.navigationBar.overrideUserInterfaceStyle = .dark
} else {
self.navigationController?.navigationBar.barTintColor = .blue
self.navigationItem.title = NSLocalizedString(kTitle, comment: kTitle.capitalized)
}
self.navigationController?.navigationBar.barStyle = .black
Hope it helps! ;)

Change title color of navigation bar in MFMailComposeViewController in iOS 12 not working [duplicate]

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
}

Resources