So my problem is that I cannot set the bar tint color on an iPhone 6 plus. I can set the bar tint color for all other devices but for the iPhone 6 plus the bar tint won't change. Here is the code for the view controller. Additionally, this VC is being pushed onto the stack by a navigation controller. Any help is majorly appreciated all.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.barTintColor = .redColor()
navigationController?.navigationBar.translucent = false
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
navigationController?.navigationBar.tintColor = UIColor.whiteColor()
}
This is the only thing that I am doing in the view controller and it doesn't work at all.
Ok so I solved the issue. For some reason, iPhone 6 plus calls
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
navigationController?.navigationBar.barTintColor = .primaryGrayColor()
navigationController?.navigationBar.translucent = false
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blackColor()]
}
every time a new view controller is pushed onto a navigation stack. This does not happen in the iPhone 5, iPhone 5s, iPhone 6 or the iPhone 7.
Related
I'm applying the new appearance API for NavigationBar and I'm struggling with the status bar content (text). On Info.plist I forced to use the light mode because most of the app is white, but in one of my viewControllers, the navigation bar is dark blue (almost black) so the content within this bar needs to be white.
I used to use navigationBar.barStyle = .black on versions up to iOS 12.4, but for iOS 13 and above it doesn't work anymore. Follow the code I've tried and failed.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let navController = self.navigationController {
let navBarAppearance = navController.navigationBar.standardAppearance.copy()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowImage = UIImage()
navBarAppearance.backgroundColor = LayoutBootstrap.colors.primary.solid // Dark blue
navController.navigationBar.standardAppearance = navBarAppearance
navController.navigationBar.barStyle = .black
}
}
Status bar content is black
PS: On didFinishLaunchingWithOptions I already called a default configuration appearance for NavigationBar and Items
Do you guys have any suggestion?
Thanks in advance.
You need to set the status bar style as well
UIApplication.shared.statusBarStyle = .lightContent
I have some problems with my tabbed view when I set isTranslucent to false in combination with a NavigationView.
Does anyone know how to fix this? The problem is shown in the attached image.
I need translucent set to false otherwise I can't get the dark color.
You can set backgroundColor. Don't set isTranslucent to false or it will create these artefacts you mentioned.
UITabBar.appearance().backgroundColor = .black
UINavigationBar.appearance().backgroundColor = .black
It becomes much darker. It isn't completely opaque though.
Edit: Just watched Modernizing Your UI for iOS 13 This is the way to do it :
The TabView and NavigationView are actually UIHostedController for the legacy UITabBarController and UINavigationController:
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [.foregroundColor: UIColor.white]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor .white]
Then set the appearance on the various type of appearance.
tabBar.standardAppearance = appearance
2nd Edit:
extension UINavigationController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
navigationBar.standardAppearance = appearance
navigationBar.compactAppearance = appearance
navigationBar.scrollEdgeAppearance = appearance
}
}
extension UITabBarController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let appearance = UITabBarAppearance()
appearance.configureWithOpaqueBackground()
tabBar.standardAppearance = appearance
}
}
There should be a cleaner way to get to both tabBar and navBar.
Reference: https://developer.apple.com/videos/play/wwdc2019/224/
I was using UIKit with SwiftUI. My Tab bar was creating in storyboard but the view for which I was getting extra bottom space was a swiftui view as you mentioned. I tried all above solution but Nothing worked for me.
I am using Xcode 12.4 for development. My solution is to mark Translucent to true in storyboard and Bottom extra gray bar was gone.
Just customize it in an extension like this:
extension UITabBarController {
override open func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarAppearance()
appearance.backgroundColor = .black
tabBar.standardAppearance = appearance
}
}
Pay attention that the overridden function must be viewDidLoad(). At least it doesn't work for me when it is a viewDidAppear(:) function.
It's easier than all that, just delete the next line:
UITabBar.appearance().isTranslucent = false
I`m preparing app for iOS 13, and get bug with search controller in navigation bar. How to solve navigation bar glitch?
let search = UISearchController(searchResultsController: nil)
search.dimsBackgroundDuringPresentation = false
search.searchResultsUpdater = self
search.hidesNavigationBarDuringPresentation = false
self.definesPresentationContext = true
search.searchBar.isTranslucent = false
self.navigationItem.searchController = search
self.navigationItem.hidesSearchBarWhenScrolling = true
Press Cancel and navigation bar items becomes untouchable.
Pushing view controller leads to navigation bar item overlap.
I have created test project on git https://github.com/eKroman/TESTsearchBar
Bug appears on iOS 13 beta (tested on iPad) using from Xcode 11 from beta 7 (maybe older beta) to Xcode 11 GM seed 2.
Does not appear on simulators.
I encountered the same problem, if I cancel the searchBar and change the navigationItem.title then I have a double title 👍. It's like a ghost layer of the navigation bar stays here in the navigation controller.
This is how I fixed it:
searchController.hidesNavigationBarDuringPresentation = true
Probably best to use it until Apple fix this issue.
I also noticed that the back button switch to default color (blue), as if the navigationBar TintColor was reset.
Config:
- Xcode 11.0 (11A420a)
- iOS 13.1 (17A5844a)
For the back button reset to default color (blue) in #CoachThys's answer, I manage to work around it by the code below.
if #available(iOS 13.0, *) {
let appearance = UINavigationBarAppearance()
/* .. set other things on appearances */
appearance.buttonAppearance.normal.titleTextAttributes = [.foregroundColor: color]
standardAppearance = appearance
compactAppearance = appearance
scrollEdgeAppearance = appearance
}
However, I cannot find a way to work around the back indicator image which is still reset to blue color briefly.
Add custom backbutton with a image would fixed the new bug. It works well for me.
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
negativeSpacer.width = -8
self.navigationItem.leftBarButtonItems = [negativeSpacer, leftBarButtonItem]
I have a view controller in my navigation stack that needs to have a transparent navigation bar, while still showing the back button.
I'm able to achieve that with one line of code inside viewWillAppear:
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
However, when I try to go back to the previous view, I'm setting the background image back to nil or .none but I'm losing the translucent effect that was previously on there when I do that.
I've tried setting all the following options in viewWillDisappear and none seem to bring the translucency back. It just appears white no matter what I do. The shadow on the bottom is also gone too:
self.navigationController?.navigationBar.isTranslucent = true
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.backgroundColor = .none
self.navigationController?.navigationBar.setBackgroundImage(.none, for: .default)
Initial Navigation Bar:
Transparent Navigation Bar:
After Transitioning Back:
In viewWillAppear make the navigation bar transparent
override func viewWillAppear(_ animated: Bool) { self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true
}
And backg to translucent in viewWillDisappear
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
self.navigationController?.navigationBar.shadowImage = nil
self.navigationController?.navigationBar.isTranslucent = false
}
After spending time poking around in the UINavigationBar internals, I did discover a simple method that seems to work, and does not require any configuration of the standard UINavigationBar attributes we've previously fiddled with to achieve transparency. The following is tested working on iOS 12.2.x:
class TallNavigationBar: UINavigationBar {
private lazy var maskingView: UIView = {
let view = UIView(frame: bounds)
view.backgroundColor = .clear
return view
}()
var isTransparent = false {
didSet {
guard isTransparent != oldValue, let bkgView = subviews.first else { return }
bkgView.mask = isTransparent ? maskingView : nil
}
}
}
Obviously, whenever fiddling (even slightly) with undocumented internals: use at your own risk!
This worked for my app which needs to revert to an opaque navigation bar after popping from a transparent navigation bar.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
navigationController?.navigationBar.shadowImage = nil
navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.backgroundColor = nil
}
I notice a problem in my UIDocumentPicker's navigation bar at iOS 11 only, the done, cancel, or edit buttons are invisible, and when the user touch it it appears i.e. The color at normal state is white, even when changing the UINavigationBar.appearnce().tintColor, The color only changed on touch.
For unknown reason I figured out that if you make a subclass of UIDocumentPicker using Objective-C and set the [UINavigationBar appearance].tintColor = [UIColor black]; in viewWillAppear func, and reset it to your defaults in the viewWillDisappear, it works well.
But if you do the same steps using swift it wont.
I'm not a big fan of setting the global appearance between viewWillAppear and viewWillDisappear. The appearance API should be used at application start only. You can just reset the appearance for UIDocumentPickerViewController only without subclassing by putting this code in application:didFinishLaunchingWithOptions: and the bar buttons will return the their original blue:
if #available(iOS 11.0, *) {
UINavigationBar.appearance(whenContainedInInstancesOf: [UIDocumentBrowserViewController.self]).tintColor = nil
}
Use CustomDocumentPickerViewController with black appearance for UINavigationBar and UIBarButtonItem
import UIKit
class CustomDocumentPickerViewController: UIDocumentPickerViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UINavigationBar.appearance().tintColor = UIColor.black
UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal)
}
override func viewWillDisappear(_ animated: Bool) {
UINavigationBar.appearance().tintColor = UIColor.white // your color
UIBarButtonItem.appearance().setTitleTextAttributes(nil, for: .normal)
super.viewWillDisappear(animated)
}
}