Reset hidesBarsOnSwipe in swift - 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.

Related

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.

Set UIImageView From Modal To Another Controller

I have a form the user is filling out, and I present a modal with images for them to choose from. I am attempting to set a UIImage in the original controller from the modal, but the problem is it's nil.
What is the best way to set it? The viewDidLoad doesn't seem to trigger on dismissal of a modal, which is how I'm getting rid of it, and neither does the viewWillAppear. When can I set the image?
My Code right now:
//Collection view choosing deal background
#IBOutlet var collectionView: UICollectionView!
var chosenNumber: Int?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
if(self.restorationIdentifier == "NewDeal") {
if(chosenNumber != nil) {
newDealBackgroundImage.image = UIImage(named: "food_\(chosenNumber!)")
}
}
}
extension BusinessOwnerVC: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
//self.newDealBackgroundImage.image = UIImage(named: "food_\(indexPath.row + 1)")!
chosenNumber = indexPath.row + 1
self.dismiss(animated: true, completion: nil)
}
}
If your presentation style is fullScreen … then your willAppear will be called
navigationController.modalPresentationStyle = .fullScreen
but if you dont want full screen presentation Make yourself the presentation controller's delegate and override presentationControllerDidDismiss(_:).
class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
print("viewWillAppear")
}
#IBAction func show(_ sender: Any) {
let newViewController = NewViewController()
//set delegate of UIAdaptivePresentationControllerDelegate to self
newViewController.presentationController?.delegate = self
present(newViewController, animated: true, completion: nil)
}
}
extension UIViewController: UIAdaptivePresentationControllerDelegate {
public func presentationControllerDidDismiss( _ presentationController: UIPresentationController) {
if #available(iOS 13, *) {
//Call viewWillAppear only in iOS 13
viewWillAppear(true)
}
}
}
After much experimentation, here's what I found works for me. All I wanted was to access and update a view controller (of the same kind) under a modal I am presenting. Turns out my previous VC was still active, so all I had to do was pass a reference to that VC into the modal, and then I could change its properties.
let modal = UIStoryboard.init(name: "BusinessOwner", bundle: nil).instantiateViewController(withIdentifier: "DealBackgroundSelection") as! BusinessOwnerVC
modal.newDealView = self // This is a weak var of BusinessOwnerVC? declared in the view controller itself
present(modal, animated: true, completion: nil)
And then when I was ready to dismiss that modal:
self.newDealView!.updateDealImage(number: indexPath.row + 1)
updateDealImage was just a function I created to handle all the update. And that works for me.

Send data only if view controller is being popped off stack by swipe or back button press but not by switching tabs

I looked through SO and compiled these methods below but none of them works for me.
I have a TabBarController with 2 tabs. In the Second tab I have a NavigationController > TableViewController > DetailViewController.
In my DetailViewController I have a custom delegate to send some data to the TableViewController when the Back Button is pressed or the view is Swiped to Dismiss (right swipe). I only want the data sent when the Back Button or Swipe to Dismiss is fully finished and not get sent when the tab is switched or if swiping 3/4 of the way but the user decides NOT to complete the back swipe (basically they stay on the same DetailVC scene).
I tried all of these methods below and they either get triggered when the tab is switched to the first tab, when the DetailVC gets pushed on and popped off, or during the 1/2 way Swipe to Dismiss the DetailVC they still run meaning the data should not have been sent.
DetailViewController:
protocol DetailViewDelegate: class {
func sendSomeData(value: Bool)
}
class DetailViewController: UIViewController{
weak var delegate: DetailViewDelegate?
//1. runs when Tab switches, the Back Button is pressed, and Swipe to Dismiss is triggered
override func viewWillDisappear(_ animated : Bool) {
super.viewWillDisappear(animated)
if (self.isMovingFromParentViewController) || (self.isBeingDismissed){
//doesn't run at all
}else{
//runs whenever view is no longer on scene
sendData()
}
}
//2. runs when Tab switches, Back Button is pressed, Swipe to Dismiss is triggered, and when the view is Pushed on AND Popped off
override func didMove(toParentViewController parent: UIViewController?) {
if parent != nil {
sendData()
}else{
//if parent == nil doesn't run at all
}
}
//3. if switching from the second tab it doesn't run but when switching back to the second tab it does run, also runs when view is being Pushed on and Not Popped on
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
sendData()
}
//4. if switching from the second tab it doesn't run but when switching back to the second tab it does run, also runs when view is being Pushed on and Not Popped on
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
sendData()
}
//MARK:- Custom Func
fileprivate func sendData(){
let value = true
delegate?.sendSomeData(value: value)
}
}
TableViewController:
class TableVC: UIViewController, DetailViewDelegate, UITableViewData..., UITableViewDele...{
var setValue = false
func sendSomeData(value: Bool){
//setValue should only update to true if DetailVC's Back Button is pressed or Right Swipe to Dismiss is fully complete
self.setValue = value
}
}
The TableView never has a problem receiving the data. The problem is when I switch tabs (data still gets sent) or a swipe to dismiss on the DetailVC isn't fully completed (data still gets sent).
What's the best way to send the data from the DetailVC but making sure the Back Button is pressed or Right Swipe to Dismiss is fully complete?
You need to use custom back button and a delegate to call the parent.
this is your parent ViewController:
import UIKit
class ViewController: UIViewController, ViewControllerSecondDelegate {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Next" {
let vc = segue.destination as? ViewControllerSecond
vc?.delegate = self
}
}
func secondDelegate() {
print("delegate") //GetData()
}
}
and this is the child view controller, which you want to back from it to your parent:
import UIKit
protocol ViewControllerSecondDelegate {
func secondDelegate()
}
class ViewControllerSecond: UIViewController, UIGestureRecognizerDelegate {
var isTouched = false
var isPopTouch = true
var delegate: ViewControllerSecondDelegate?
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(false, animated:false)
let myBackButton:UIButton = UIButton.init(type: .custom)
myBackButton.addTarget(self, action: #selector(ViewControllerSecond.popToRoot(sender:)), for: .touchUpInside)
myBackButton.setTitle("Back", for: .normal)
myBackButton.setTitleColor(.blue, for: .normal)
myBackButton.sizeToFit()
let myCustomBackButtonItem:UIBarButtonItem = UIBarButtonItem(customView: myBackButton)
self.navigationItem.leftBarButtonItem = myCustomBackButtonItem
self.navigationController?.interactivePopGestureRecognizer?.delegate = self
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isTouched {
isPopTouch = true
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.isPopTouch = false
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if isPopTouch {
delegate?.secondDelegate()
}
}
func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
self.isTouched = true
}
return true
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
self.isTouched = false
}
func popToRoot(sender:UIBarButtonItem){
delegate?.secondDelegate()
self.navigationController?.popToRootViewController(animated: true)
}
}
the above code, handle back button and back gesture.

hide / show tab bar when push / back. swift

Answer:
Use self.tabBarController?.tabBar.hidden instead of hidesBottomBarWhenPushed in each view controller to manage whether the view controller should show a tab bar or not.
override func viewWillAppear(animated: Bool) {
self.tabBarController?.tabBar.hidden = true/false
}
I want
view controller 1: tab bar should be showed
view controller 2: tab bar should be showed
view controller 3: tab bar should not be showed.
view controller 4: tab bar should not be showed.
I wrote
// prepareForSegue in view controller 1,
let upcoming = segue.destinationViewController as! viewcontroller3
upcoming.hidesBottomBarWhenPushed = true
// in view controller 3,
func clickOnButton(button: UIButton) {
self.hidesBottomBarWhenPushed = false
self.performSegueWithIdentifier("viewController2", sender: self)
self.hidesBottomBarWhenPushed = true
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewController2" {
let upcoming = segue.destinationViewController as! viewController2
upcoming.hidesBottomBarWhenPushed = false
}
}
// prepareForSegue in view controller 2
let upcoming = segue.destinationViewController as! viewController4
upcoming.hidesBottomBarWhenPushed = true
if 1 -> 3 then back to 1, works.
if 1 -> 3 -> 2 then back to 3 and back to 1, works.
if 2 -> 4, then back to 2, works.
if 1 -> 3 -> 2 -> 4 then back to 2, tab bar is not showed. Wondering why. Any suggestions or some explanation of hidesBottomBarWhenPushed as it confuses me a lot
As it's name suggest, hiddenBottomBarWhenPushed only hide bottom bar if needed, it will not unhide bottomBar.
You can do this to get it works:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.hidden = true/false
}
or simply put self.tabBarController?.tabBar.hidden = true/false in prepareForSegue
But I would not recommend you to do so, as it would be weird if bottomBar suddenly popped out, user will thought they suddenly back to rootViewController while they are not.
Users should always know where they are in your app and how to get to their next destination.
Add hidesBottomBarWhenPushed property to destination view controller, and set to true.
Example with push VC with identifier:
let storyboard = UIStoryboard(name: STORYBOARD_NAME, bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: VC_IDENTIFIER) as! YourViewController
vc.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(vc, animated: true)
Here's my two cents.
Swift 3/4/5:
Approach 1: (Recommended)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegueIdentifier" {
let destinationController = segue.destinationViewController as! YourViewController
destinationController.hidesBottomBarWhenPushed = true // Does all the hide/show work.
}
}
Approach 2:
override func viewWillAppear(_ animated: Bool) { // As soon as vc appears
super.viewWillAppear(true)
self.tabBarController?.tabBar.isHidden = false
}
override func viewWillDisappear(_ animated: Bool) { // As soon as vc disappears
super.viewWillDisappear(true)
self.tabBarController?.tabBar.isHidden = true
}
Add this implementation in ViewController you want to hide/show tabbar on pushed/popped. it will also work for all next pushed view controllers.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if wilmove {
hidesBottomBarWhenPushed = true
}
wilmove = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if wilmove {
hidesBottomBarWhenPushed = false
}
wilmove = false
}
var wilmove = false
override func willMove(toParentViewController parent: UIViewController?) {
super.willMove(toParentViewController: parent)
wilmove = true
if !isViewLoaded {
hidesBottomBarWhenPushed = true
}
}
Swift 5
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.hidesBottomBarWhenPushed = true
}
It's Better to Fade Out than to Hide Away...
(.... my my, hey hey)
Swift 5:
Yet another approach... is to fade the tab bar in and out.. Not saying this is strategically more advantageous than any of the other ones, such as those entailing prepareForSegue, but it can be adapted to other triggers. In any case, animating tab bar alpha avoids the harsh disappear/appear effect when setting .isHidden on the tabBar (UIView) by fading it in and out. This is done in any VC that needs it hidden when the VC is pushed or loaded and unhidden when the VC is popped or unloaded.
This will not bother to reinstate the tab bar until the back button is pushed in the navBar or equivalent action, such that when a child VC is pushed onto it, the tab bar will remain hidden, which usually (but doesn't always) makes sense.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
UIView.animate(withDuration: 0.4, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.tabBarController?.tabBar.alpha = 0.0
}, completion: { (finished: Bool) -> Void in
self.tabBarController?.tabBar.isUserInteractionEnabled = false
})
}
override func viewWillDisappear(_ animated: Bool) {
if self.isMovingFromParent {
UIView.animate(withDuration: 0.4, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.tabBarController?.tabBar.alpha = 1.0
}, completion: { (finished: Bool) -> Void in
self.tabBarController?.tabBar.isUserInteractionEnabled = true
})
}
}

How to hide and show label when navigation VC1 to VC2 back and forth

Say, I have a label show : Loading...
problem: When return from VC(2). The label is not hidden.
How to hide it when return from VC(2) and dont hide it when in navigating to VC(2) and show the message : Loading....
in VC(1)
#IBOutlet weak var lbLoadingMsg
In viewDidLoad() {
lbLoadingMsg.hidden = true
}
-2-- turn it on when prepare to navigate to VC(2)
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool
{
--code--
lbLoadingMsg.hidden = false
}
Override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!){
}
You can use NSNotificationCenter for that.
Follow this simple steps:
1.In your VC(2) add this code into your button from where you are going back:
#IBAction func goBack(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName("hide", object: nil)
self.dismissViewControllerAnimated(true, completion: nil)
}
2.In your First View add this code into viewDidLoad method:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideLabel:", name:"hide", object: nil)
}
now this method will call this function:
func hideLabel(notification: NSNotification){
self.lbLoadingMsg.hidden = true
}
And this will hide your label in first view when ever goBack button will pressed from first view.
Hope this will help you.
Write this in VC2
,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var identifier = segue.identifier
if(identifier! == "yourIdentifier"){
var vc1:VC1 = segue.destinationViewController as! VC1
vc1.lbLoadingMsg.hidden = true
}
}
func viewDidAppear(_ animated: Bool) {
lbLoadingMsg.hidden = true
}
Move
lbLoadingMsg.hidden = true
line from viewDidLoad to viewDidAppear. I think most quicker way.