Swift: How to hide/move navigationTitle for small in-line navigation - swift

I have a tableView that has a large navigation title of "My title". By default, when I scroll up, the large title turns into a small title. How do I go about hiding the small title during the small navigation bar but still show the large title when i scroll down? Is there a delegate i can use to notify me when navigation is transitioning for large title to small? Because my end result is to have my title be on the left bar bar button side when i scroll up.
Is there an easier to way move the title location to the left where left bar button would be?
My approach is create the left bar button with the same title as my navigation. I would hide it when my large navigation title is present then show it when large navigation title isnt. Meanwhile, I would hide the small navigation title when the small navigation bar is present. I hope this makes sense haha. Basically just trying to find a way to have my title on where the left bar button item should be.
Solution:
self.navigationItem.title = "hello"
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.titlePositionAdjustment = UIOffset(horizontal: -150, vertical: 0)
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
navBarAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = true

self.navigationItem.title = "My Title"
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.titlePositionAdjustment = UIOffset(horizontal: -150, vertical: 0)
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
navBarAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.black]
navigationController?.navigationBar.standardAppearance = navBarAppearance
navigationController?.navigationBar.scrollEdgeAppearance = navBarAppearance
navigationController?.navigationBar.prefersLargeTitles = true

Related

How to achieve the auto-shrink SwiftUI Navigation Title with UIKit?

I am trying to achieve the auto-shrink large nav title like the Navigation Title in SwiftUI, but no matter what my nav title always stay shrinked and never appear in large title mode. How do i fix this? (code below)
func setup() {
table.translatesAutoresizingMaskIntoConstraints = false
table.dataSource = self
table.delegate = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "mycell")
table.separatorColor = .white
table.backgroundColor = .black
let appearance = UINavigationBarAppearance()
appearance.titleTextAttributes = [.foregroundColor: UIColor.black]
appearance.largeTitleTextAttributes = [.foregroundColor: UIColor.black]
appearance.configureWithOpaqueBackground()
navigationItem.title = "Todo List"
navigationItem.scrollEdgeAppearance = appearance
navigationItem.standardAppearance = appearance
view.addSubview(table)
}
As well as providing a configuration for large titles, you need to instruct your navigationController to use large titles
navigationController.navigationBar.prefersLargeTitles = true
The default configuration for a view controller should be to automatically use large titles if that is the preferred choice. However you can set it explicitly per view controller:
navigationItem.largeTitleDisplayMode = .always // or .automatic or .never
If the nav controller has prefersLargeTitle = false then large titles will never be displayed whatever the largeTitleDisplayMode setting.

How to set navbar and tabbar not transparent and their borders visible

I can not find any info in internet about this question. I have tableview in UIViewcontroller and it is embedded in navigation controller. I want that my navbar was always like in scrolling state (borders are visible) and same thing with tabbar. Here is photo what I have(1-st) and what I want (2-nd).
In AppDelegate file in didFinishLaunchingWithOptions method added:
//MARK: - NavBar appearance
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.backgroundColor = UIColor(named: "AccentColor")
UINavigationBar.appearance().standardAppearance = navBarAppearance
UINavigationBar.appearance().scrollEdgeAppearance = navBarAppearance
//MARK: - TabBar appearance
let tabBarAppearance = UITabBarAppearance()
tabBarAppearance.backgroundColor = UIColor(named: "AccentColor")
UITabBar.appearance().standardAppearance = tabBarAppearance
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance

Xcode 13 - Navigation bar and status bar text color changing in swift OS 15

Recently I updated my Xcode to 13 and after that, I am facing some issues with the navigation bar and Status bar. I am using the tab bar in my view controller. After updating the Xcode, according to the version, I added some code related to the navigation bar.
if #available(iOS 15.0, *) {
tableView.sectionHeaderTopPadding = 0
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = UIColor(red: 58/255,green: 24/255, blue: 93/255, alpha: 1.0)
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
}
Everything is working when I first opened the app. When I click on the other tab and then this tab. The status bar text color is changing.
I tried different ways to set the status bar text color. But nothing worked for me.
Use this function:
extension YourViewController {
override var preferredStatusBarStyle: UIStatusBarStyle {
.lightContent
}
}
If you just want to set to white colour you status bar in the entire project you can easily do it by adding the following keys into your project.plist file:
<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarStyle</key>
<string>UIStatusBarStyleLightContent</string>
<key>UIUserInterfaceStyle</key>
<string>Light</string>
So, what does these keys do... You are just defining that you status bar colour will not be controlled by the any view controller and that the default colour will be "light theme" that is pretty much the white colour that you want in this case...
this would be the best approach if you do not intent to update the status bar colour all over the place.
this was tested and is working as expected on Xcode 13 and iOS [13.x,14.x,15.x]
If you want to update to customise or update the colours for the navigation bar you would should be able to do with an extension like this one bellow, just few examples but you guys should get what I mean:
extension UINavigationBar {
func clearBackground() {
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithTransparentBackground()
navBarAppearance.shadowColor = UIColor.clear
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
func blackColor() {
// Create an Appearance and customise as you wish.
// in this case just a black background with titles in white.
let navBarAppearance = UINavigationBarAppearance()
navBarAppearance.configureWithOpaqueBackground()
navBarAppearance.shadowColor = UIColor.black
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
self.standardAppearance = navBarAppearance
self.scrollEdgeAppearance = navBarAppearance
}
}
with this code you should be able to easy update on any view controller by just calling something like:
override func viewDidLoad() {
super.viewDidLoad()
// set the navigation bar color as transparent
self.navigationController?.navigationBar.clearBackground()
}

Navigation bar and tab bar turned black on iOS15 Xcode 13

Today I just updated my Xcode to version 13 and found that all the navigation bars and the tab bars of my project turned black, I didn't change any settings, my project was working fine on Xcode 12, and I have toggled it to light mode, I couldn't find a way to recover the appearances to the old ones.
Just simply check the "Translucent" in your attributes inspector.
Apply the following code to update the appearance of the navigation bar. This can be done in AppDelegate if you wish for it to be the appearance throughout the app.
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: .black]
appearance.backgroundColor = .white
// Default portrait view
UINavigationBar.appearance().standardAppearance = appearance
// There are other appearances you could apply it to as well...
// UINavigationBar.appearance().scrollEdgeAppearance = appearance
// UINavigationBar.appearance().compactAppearance = appearance
Make like this:
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .red
appearance.titleTextAttributes = [.font:
UIFont.boldSystemFont(ofSize: 20.0),
.foregroundColor: UIColor.white]
// Customizing our navigation bar
navigationController?.navigationBar.tintColor = .white
navigationController?.navigationBar.standardAppearance = appearance
navigationController?.navigationBar.scrollEdgeAppearance = appearance
I wrote a new article talking about it.
https://medium.com/#eduardosanti/uinavigationbar-is-black-on-ios-15-44e7852ea6f7

UIActivityViewController: access the navigation items to change color of presented view

I am changing a project colors and this old orange color not changed on UIActivityViewController button. I want to change button color orange to white.
This look is when user press the share button and select "Add to Notes" button. How can I access these buttons and change colors?
First problem solved. This code is working on buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
When I click to "New Note" this look is came. How can I change the title color on this view?
I want to change this title color black to white.
This is my sharing code and I was trying every bar tint color code.
func shareLink(_ link: String) {
let activityViewController = UIActivityViewController(activityItems: [link], applicationActivities: nil)
activityViewController.navigationController?.navigationBar.barTintColor = .white
activityViewController.navigationController?.navigationBar.tintColor = .white
activityViewController.tabBarController?.navigationItem.backBarButtonItem?.tintColor = .white
activityViewController.tabBarController?.navigationController?.navigationBar.tintColor = .white
self.present(activityViewController, animated: true)
}
Also I tried adding some code on AppDelegate in didFinishLaunchingWithOptions function
UINavigationBar.appearance().tintColor = .white
UIApplication.shared.keyWindow?.tintColor = .white
Ok. These buttons are not accessible. So I solved the problem as follows:
It's for all UINavigationBar buttons:
UIButton.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .white
It's for all UINavigationBar titles:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]