Prefer large titles for single View Controller - swift

How can I set a large title for a single View Controller embedded in a Navigation Controller?
Normally I am only able to set large titles for a whole Navigation Controller including all View Controllers but i only want one to display a large title.
self.navigationController?.navigationBar.prefersLargeTitles = true

While both of the other answers do the trick, a cleaner way to achieve this is by doing the following in the UIViewController you want:
navigationItem.largeTitleDisplayMode = .always
or for the reverse effect:
navigationItem.largeTitleDisplayMode = .never
This takes away the need to keep track of a state, which in my opinion is a vast improvement
See largeTitleDisplayMode - Apple Developer Documentation for more information on the subject
Only downside is that it's iOS 11.0+

You can set prefersLargeTitles = true in viewWillAppear when ViewController's going to appear and prefersLargeTitles = false in viewWillDisappear when ViewController's going to disappear
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.navigationBar.prefersLargeTitles = false
}

You can implement the logic for this viewController only. You can try something like this:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = false
}

Related

UISearchBar height gets overridden by _UITemporaryLayoutHeight - how do apps make search bars the same height as iOS 11?

I constantly see, on snapchat, instagram, and pretty much every major app - UISearchBars which have a height of 44px. However, I have tried every online 'solution' and have not been able to figure this out.
Here is another thread which no one seemed to find a solution to the 'sub-issue' of it - how to make a UISearchBar's height 44px in iOS 12 and up:
UISearchBar increases navigation bar height in iOS 11
Note - I have joined this community on a new account and have been building iOS applications for many years. I am asking if anyone found a solution to this or do you have to build your own custom search bar (UIView with a textfield in it, etc.)?
private var searchController = UISearchController(searchResultsController: nil)
private func setupSearchController() {
navigationItem.searchController = searchController
searchController.delegate = self
searchController.searchResultsUpdater = self
searchController.definesPresentationContext = true
searchController.searchBar.autocorrectionType = .yes
searchController.searchBar.searchBarStyle = .minimal
searchController.obscuresBackgroundDuringPresentation = false
}
override func viewDidLoad() {
super.viewDidLoad()
setupSearchController()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
view.setNeedsLayout()
navigationItem.largeTitleDisplayMode = .never
navigationItem.hidesSearchBarWhenScrolling = true
navigationController?.setNavigationBarHidden(false, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationItem.hidesSearchBarWhenScrolling = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.largeTitleDisplayMode = .never
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
navigationItem.hidesSearchBarWhenScrolling = false
}

Swift How can I keep only the first page with a large title?

The large title of navigation item comes out when the app just describes execution. However, when I move to another page and come back, that mode is turned off. Create a viewDidLoad for each page as follows: What's the problem?
class CollectionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
}
next page
class AssetCollectionViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.largeTitleDisplayMode = .never
}
Possibly adding code didn't work.
override func viewDidDisappear(_ animated: Bool) {
self.navigationItem.largeTitleDisplayMode = .always
}
Change titleDisplay mode in viewWillppear function as it will always execute
class CollectionViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
self.navigationItem.largeTitleDisplayMode = .always
}
How about doing all the implementation in your CollectionViewController like so:
override func viewWillDisappear(_ animated: Bool) {
navigationController?.navigationBar.prefersLargeTitles = false
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationController?.navigationBar.prefersLargeTitles = true
}
When you're setting navigationItem.largeTitleDisplayMode in viewDidDisappear(_:), you're doing it for the current controller's navigationItem.
I suggest you to create UINavigationController subclass with:
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.prefersLargeTitles = true
}
Then set navigationItem.largeTitleDisplayMode to .never in controllers, where you don't want large title.

Hiding Bottom Bar When Pushed - Animation freezing on Iphone X

I've encountered an issue that I cannot figure out for the life of me. The issue is only happening on the iPhone X. I've added a little video since its difficult to explain exactly whats happening.
I've also added a screenshot of my storyboard so you can see the flow.
Pretty much were experiencing a freeze when the tab bar is being hidden. It only happens when we visit the category VC (which is presented modally using a segue, it is also embedded in a navigation controller.)
** I'm still new to iOS development so if I'm doing anything terribly wrong feel free to share :)
Video:
https://youtu.be/HC14zFxh-HM
Code that sends to reader:
#IBAction func sendToReader(_ sender: Any) {
let myVC = storyboard?.instantiateViewController(withIdentifier: "ReaderRootVC") as! ReaderRootVC
myVC.book = self.book
myVC.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(myVC, animated: true)
}
Code that closes Category VC:
#IBAction func navigationCancelBtnPressed(_ sender: Any) {
self.navigationController?.dismiss(animated: false, completion: nil)
}
Storyboard:
In your ReaderRootVC,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// Hide the Tab Bar
self.tabBarController?.tabBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Show the Tab Bar
self.tabBarController?.tabBar.isHidden = false
}

Black view under navigation bar

In my app i have two view controllers embedded in navigation controller (lets say viewControllerA and viewControllerB). In the rootviewcontroller I don't want to show navigation bar, so in viewWillAppear and viewWillDisappear i have added thes lines:
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: false)
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: false)
}
Now from the viewcontrollerB when back button is typed under navigation bar (when it starts to disapear) black view appears. How to remove that black view?
P.S. I have set navigation bar isTranslucent to false but it does not solve the problem. In my project i'm not using storyboards.
Below answer based on the question owners test project.
From your test project you disabled the navigationController transition(view to view) animation when you hide and unhide navigation bar that causes the black view to appears.
override func viewWillAppear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(true, animated: true) // set to true
}
override func viewWillDisappear(_ animated: Bool) {
self.navigationController?.setNavigationBarHidden(false, animated: true) //set to true
}

Reset hidesBarsOnSwipe in swift

Where and how do I have to reset hidesBarsOnSwipe? I set the option in a View Controller which I push and want to reset it for the View Controller which did the push. What I tried until now is setting hidesBarsOnSwipe to false in the viewDidDisappear and in the viewDidLoad of the pushing ViewController.
The Navigationbar is still disappearing.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if (!visiblePOIs.isEmpty) {
let beaconInfo = visiblePOIs[indexPath.item]
var controller = storyboard!.instantiateViewControllerWithIdentifier("DetailController")! as! DetailController
controller.setup(beaconInfo)
self.parentViewController!.navigationController?.pushViewController(controller, animated: true)
}
}
DetailController
override func viewDidLoad() {
super.viewDidLoad()
webView.delegate = self
navigationController?.hidesBarsOnSwipe = true
self.automaticallyAdjustsScrollViewInsets = false
}
next view controller write in viewDidload
First Vc
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.hidesBarsOnSwipe = true
}
Second VC
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.hidesBarsOnSwipe = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
I hope its works
I had your exact problem. Here's how I solved it. (You can adapt this based on your needs.)
class MyViewController: UITableViewController {
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
//Correct the nav bar state unwinding from segues
self.navigationController?.hidesBarsOnSwipe = true
}
override func willMoveToParentViewController(parent: UIViewController?) {
super.willMoveToParentViewController(parent)
//Toggle the auto-hiding nav bar when this view gets added/removed from the nav controller
self.navigationController?.hidesBarsOnSwipe = !self.navigationController!.hidesBarsOnSwipe
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
super.prepareForSegue(segue, sender: sender)
//Reset the nav bar to visible when segueing to another view
self.navigationController?.navigationBarHidden = false
self.navigationController?.hidesBarsOnSwipe = false
}
}
This approach allows you to limit the functionality of the auto-hiding feature to the desired view controller without adding code to all associated view controllers.