How to center custom tabbar item on iPhone X - ios

I have an app where I created a custom tabbar item in a UITabbarController, that someone can press to take a picture, and it looks like it does below.
That is exactly what I want, the problem is that when I test it on an iPhone X, the tabbar item for the camera looks lower then I would like, for example:
I have tried a couple of things to fix this, such as fixing the height of the tabbar in the viewDidLayoutSubviews(), but it messed with the tabbar on the iPhone 8. I also made sure that the "Use Safe Area Layout Guides" is selected, but it still doesn't work.
I also tried to change the frame of the tabbar item, but that doesn't work either.
This is the code that I used for the custom tabbar Controller:
import UIKit
class OtherTabController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setupBtn()
// Do any additional setup after loading the view.
}
func setupBtn() {
let centerBtn = UIButton(frame: CGRect(x: 0, y: 10, width: 45, height: 45))
var centerBtnFrame = centerBtn.frame
centerBtnFrame.origin.y = (view.bounds.height - centerBtnFrame.height) - 2
centerBtnFrame.origin.x = view.bounds.width/2 - centerBtnFrame.size.width/2
centerBtn.frame = centerBtnFrame
centerBtn.layer.cornerRadius = 35
view.addSubview(centerBtn)
let centerImg = UIImage(named: "Other")
centerBtn.setBackgroundImage(centerImg, for: .normal)
centerBtn.addTarget(self, action: #selector(centerBtnAction(sender:)), for: .touchUpInside)
view.layoutIfNeeded()
}
#objc private func centerBtnAction(sender: UIButton) {
print("Camera")
cameraAction()
}
func cameraAction() {
let alertController = UIAlertController.init(title: nil, message: nil, preferredStyle: .actionSheet)
let takePhotoAction = UIAlertAction(title: "Take a Photo", style: .default, handler: nil)
alertController.addAction(takePhotoAction)
let selectFromAlbumAction = UIAlertAction(title: "Select from Album", style: .default, handler: nil)
alertController.addAction(selectFromAlbumAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertController.addAction(cancelAction)
self.present(alertController, animated: true, completion: nil)
//OtherTabController?.present(alertController, animated: true, completion: nil)
}
}
If there is anything else I could help with, please ask. Thank you
EDIT:
I tried to make the y view the same as the super view but all it did was move the button to the top of the screen.
var centerBtnFrame = centerBtn.frame
centerBtnFrame.origin.y = view.bounds.minY //Make centerBtn' top equal to that of view's
centerBtnFrame.origin.x = view.bounds.width/2 - centerBtnFrame.size.width/2
centerBtn.frame = centerBtnFrame
If you need any more info, please ask. Thank you
Edit:
With the help of #Revanth Kausikan, I decided to create a custom tabbar with a view and a few buttons. It works very well in my opinion. It looks a little rough around the edges, but this is just a test for now.
Here is the code for the view:
import UIKit
class ItemScene: UIViewController {
#IBOutlet var customTab: UIView!
#IBOutlet weak var cameraBtn: UIButton!
#IBOutlet weak var twoBtn: UIButton!
#IBOutlet weak var oneBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func cameraBtnPressed(_ sender: Any) {
print("Camera")
}
#IBAction func twoBtnPressed(_ sender: Any) {
self.performSegue(withIdentifier: "segue", sender: nil)
print("Two")
}
#IBAction func oneBtnPressed(_ sender: Any) {
print("One")
}
}
this is the code for the second ViewController:
import UIKit
class TestingViewController: UIViewController {
#IBOutlet weak var cameraBtn: UIButton!
#IBOutlet weak var oneBtn: UIButton!
#IBOutlet weak var twoBtn: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func cameraBtnPressed(_ sender: Any) {
print("Camera")
}
#IBAction func twoBtnPressed(_ sender: Any) {
print("Two")
}
#IBAction func oneBtnPressed(_ sender: Any) {
performSegueToReturnBack()
print("One")
}
}
extension UIViewController {
func performSegueToReturnBack() {
if let nav = self.navigationController {
nav.popViewController(animated: true)
} else {
self.dismiss(animated: true, completion: nil)
}
}
}
If anyone has anything else to add it would be greatly appreciated. Thank you

The Y point of button frame must be the same as that of it's super view (i.e. tab bar here).
If you've not created an outlet for the tab bar, first do so.
Ctrl (or right) click and drag from your tab bar in the storyboard to its associated view controller, to create an outlet. (assume that I'm naming it as tabBar)
now try this:
var centerBtnFrame = centerBtn.frame
let tabBarFrame = self.convert(tabBar.frame, to: view)
centerBtnFrame.origin.y = tabBarFrame.minY //Make centerBtn' top equal to that of tabBar's
centerBtnFrame.origin.x = view.bounds.width/2 - centerBtnFrame.size.width/2
centerBtn.frame = centerBtnFrame
Alternatively, you can try this:
var centerBtnFrame = centerBtn.frame
centerBtnFrame.origin.y = view.height - tabBar.height //This is the same logic as you've used for fixing x value of centerBtnFrame.
centerBtnFrame.origin.x = view.bounds.width/2 - centerBtnFrame.size.width/2
centerBtn.frame = centerBtnFrame
Yet another approach is to go back to the constraints, and make sure the elements and the base view are within the safe area.
If my guess is right (since you've not provided details about the constraints in the question), you've set the base view (the view that is in the bottom/base, holding all other elements on it) to fit itself with the entire view are of the view controller. Consider changing it to fit itself within the safe area.
Let me know the results.
EDIT
My opinion on your new attempt: It's really good, and I see that you are now able to customize it a lot more.
To improve it, you can try these:
Here, there are three views that are clickable in nature. we can use this to increase the interactive area as per our wish, to provide a natural experience for the user.
Here are it's constraints:
Other than these suggestions, I don't think I need to provide any. Your method is self sufficient for your implementation idea.
:)

Try this code working 100% ...
Declare the button
var menuButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
setupMiddleButton()
}
//create the method
func setupMiddleButton(flag:String) {
menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = tabBar.bounds.height - menuButtonFrame.height
menuButtonFrame.origin.x = tabBar.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame
menuButton.backgroundColor = UIColor.red
menuButton.layer.borderColor = UIColor.black.cgColor
menuButton.layer.borderWidth = 1
tabBar.addSubview(menuButton)
menuButton.layer.cornerRadius = menuButtonFrame.height/2
menuButton.setImage(UIImage(named: "Homenormal"), for: .normal)
menuButton.addTarget(self, action: #selector(menuButtonAction(sender:)), for: .touchUpInside)
}
//button methods inside add background colour
#objc private func menuButtonAction(sender: UIButton) {
menuButton.backgroundColor = UIColor.red
selectedIndex = 2
print("menuButtonActio=\(sender.tag)")
}
//next Tabbar didselect
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
let Selected_index = tabBar.items?.lastIndex(of: item)
print("selectedIndex123=\(Selected_index)")
if Selected_index == 0 {
menuButton.backgroundColor = UIColor.darkGray
menuButton.layer.borderColor = UIColor.black.cgColor
}
else if Selected_index == 1 {
menuButton.backgroundColor = UIColor.darkGray
menuButton.layer.borderColor = UIColor.black.cgColor
}
else if Selected_index == 2 {
menuButton.backgroundColor = UIColor.red
menuButton.layer.borderColor = UIColor.black.cgColor
}
else if Selected_index == 3 {
menuButton.backgroundColor = UIColor.darkGray
menuButton.layer.borderColor = UIColor.black.cgColor
}
else if Selected_index == 4 {
menuButton.backgroundColor = UIColor.darkGray
menuButton.layer.borderColor = UIColor.black.cgColor
}
}

Related

How to change label text of a label in a popover view controller when pressing a button in the main view?

I'm relatively new to Swift. I have a main view controller, ViewControllerMain, and a popover view controller, PopUpVC, which only has a label. I have a function, infoClicked that displays the popover with another function (showPopover) when the info button is clicked. When I click the button, I want to change the label text of the popover. However, with the current code, the label always displays "Default".
Here is my code:
class ViewControllerMain: UIViewController, UIPopoverPresentationControllerDelegate, GetTimesUsed {
let tipController: PopUpVC = PopUpVC().self
#IBAction func infoClicked(_ sender: UIButton) {
tipController.tipText = "Success"
showPopover()
}
func showPopover() {
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController")
myViewController?.preferredContentSize = CGSize(width: 350, height: 200)
myViewController?.modalPresentationStyle = .popover
let popOver = myViewController?.popoverPresentationController
popOver?.delegate = self
UIView.animate(withDuration: 1, animations: {
self.GifView.alpha = 0.7
})
DispatchQueue.main.asyncAfter(deadline: .now() + 0.45) {
UIView.animate(withDuration: 0.5, animations: {
self.present(myViewController!, animated: true, completion: nil)
})
}
popOver?.permittedArrowDirections = .down
popOver?.sourceView = self.view
var passthroughViews: [AnyObject]?
passthroughViews = [infoButton]
myViewController?.popoverPresentationController?.passthroughViews = (NSMutableArray(array: passthroughViews!) as! [UIView])
popOver?.sourceRect = infoButton.frame
}
}
class PopUpVC: UIViewController {
#IBOutlet weak var tip: UILabel!
var tipText: String = "Default"
override func viewDidLoad() {
super.viewDidLoad()
tip.text = tipText
// Do any additional setup after loading the view.
}
}
Thank you for your help.
As mentioned in comments, you seem to be instantiating the popup controller 2 times, so try it like this in your showPopOver code:
let myViewController = storyboard?.instantiateViewController(withIdentifier: "popupController") as! PopUpVC
myViewController.preferredContentSize = CGSize(width: 350, height: 200)
myViewController.modalPresentationStyle = .popover
myViewController.tipText = '' //set to what ever

Send TitleLabel from UIButton to a UILabel in a different UIView Controller, But it's not being sent

I have one UIViewController that I am trying to send the titleLabel of a UIButton to a UILabel held within a different UIView Controller.
I have followed the same steps and pattern as within a previous method that worked fine, but the Title Text is just not getting passed onto the next VC.
I have a Button class called MtsCardsButton, but this just sets the animation and appearance of the button.
Thank you for reviewing.
Here is my code for the Button in the first VC:
import UIKit
class MTSCardsPage: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//This is to make mtsCardsSetArray available to this ViewController
let otherVC = MTSDiscriminators()
mtsCardsSetArray2 = otherVC.mtsCardsSetArray
let otherVC2 = MTSDiscriminators()
allMtsDescriminatorsArray2 = otherVC2.allMtsDescriminatorsArray
//Set Card Titles from Array
Card1ButtonOutlet.setTitle(mtsCardsSetArray2[0], for: .normal)
Card2ButtonOutlet.setTitle(mtsCardsSetArray2[1], for: .normal)
Card3ButtonOutlet.setTitle(mtsCardsSetArray2[2], for: .normal)
Card4ButtonOutlet.setTitle(mtsCardsSetArray2[3], for: .normal)
Card5ButtonOutlet.setTitle(mtsCardsSetArray2[4], for: .normal)
//Do any additional setup after loading the view.
}
var mtsCardsButton = MtsCardsButton()
func addActionToMtsCardsButton() {
mtsCardsButton.addTarget(self, action: #selector(CardButton), for: .touchUpInside)
}
//This is to TELL the Button to do something AND to goto
//the MTS Discriminators UIView.
var cardButtonPressed = ""
#IBAction func CardButton(_ sender: MtsCardsButton) {
let secondVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(identifier: "DiscrimUIViewCollection") as! DiscrimUIViewCollection
cardButtonPressed = sender.currentTitle!
secondVC.updateTheLabel2 = cardButtonPressed
////I'VE TRIED THIS SECTION INSTEAD OF ABOVE AND IT STILL DOESN'T WORK.
// func prepare(for segue: UIStoryboardSegue, sender: Any?)
// {
// if segue.destination is DiscrimUIViewCollection
// {
// let vc = segue.destination as? DiscrimUIViewCollection
// vc?.updateTheLabel2 = cardButtonPressed
// }
// }
//switch area to make button move when pressed
switch cardButtonPressed {
case mtsCardsSetArray2[0]:
Card1ButtonOutlet.shakeMtsCardsButton()
case mtsCardsSetArray2[1]:
Card2ButtonOutlet.shakeMtsCardsButton()
case mtsCardsSetArray2[2]:
Card3ButtonOutlet.shakeMtsCardsButton()
case mtsCardsSetArray2[3]:
Card4ButtonOutlet.shakeMtsCardsButton()
case mtsCardsSetArray2[4]:
Card5ButtonOutlet.shakeMtsCardsButton()
default:
print("Error, unrecognised button pressed!")
}
guard let destinationVC = storyboard?.instantiateViewController(withIdentifier: "DiscrimUIViewCollection") as? DiscrimUIViewCollection else {
return
}
present(destinationVC, animated: true, completion: nil)
}
//Outlet for sending anything to the MTS Card Button
#IBOutlet weak var Card1ButtonOutlet: MtsCardsButton!
#IBOutlet weak var Card2ButtonOutlet: MtsCardsButton!
#IBOutlet weak var Card3ButtonOutlet: MtsCardsButton!
#IBOutlet weak var Card4ButtonOutlet: MtsCardsButton!
#IBOutlet weak var Card5ButtonOutlet: MtsCardsButton!
}
Here is the code for the second receiving VC:
class DiscrimUIViewCollection: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
#IBOutlet weak var discriminatorTitle: UILabel!
var updateTheLabel2: String?
#IBAction func discrimButtonPressed(_ sender: UIButton) {
//action here to name what discriminator means.
print(sender.currentTitle!)
}
#IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
Card1ButtonOutlet.setTitle(mtsCardsSetArray2[0], for: .normal)
Card2ButtonOutlet.setTitle(mtsCardsSetArray2[1], for: .normal)
Card3ButtonOutlet.setTitle(mtsCardsSetArray2[2], for: .normal)
Card4ButtonOutlet.setTitle(mtsCardsSetArray2[3], for: .normal)
Card5ButtonOutlet.setTitle(mtsCardsSetArray2[4], for: .normal)
collectionView.dataSource = self
collectionView.delegate = self
self.collectionView.backgroundColor = .black
discriminatorTitle.text = updateTheLabel2
discriminatorTitle.font = UIFont(name: "Mukta Mahee", size: 18)
discriminatorTitle.font = UIFont.boldSystemFont(ofSize: 18)
discriminatorTitle.numberOfLines = 2
discriminatorTitle.minimumScaleFactor = 0.1
discriminatorTitle.baselineAdjustment = .alignCenters
discriminatorTitle.textAlignment = NSTextAlignment.center
discriminatorTitle.clipsToBounds = true
discriminatorTitle.backgroundColor = colourYellow
discriminatorTitle.textColor = .black
discriminatorTitle.layer.borderColor = UIColor.black.cgColor
discriminatorTitle.layer.borderWidth = 2.0
discriminatorTitle.layer.cornerRadius = 7
discriminatorTitle.layer.shadowColor = UIColor.black.cgColor
discriminatorTitle.layer.shadowOffset = CGSize(width: 0.0, height: 6.0)
discriminatorTitle.layer.shadowRadius = 7
discriminatorTitle.layer.shadowOpacity = 0.5
discriminatorTitle.clipsToBounds = false
discriminatorTitle.layer.masksToBounds = true
let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
layout.minimumInteritemSpacing = 2
layout.itemSize = CGSize(width: (self.collectionView.frame.size.width - 0)/1, height:self.collectionView.frame.size.height/3)
//Do any additional setup after loading the view.
func numberOfSections(in collectionView: UICollectionView) -> Int {
// 1
return 1
}
}
So, after many hours of studying up various websites I found the answer. I needed to add code and re-position the code. I Changed the Storyboard ID to match the DiscrimUIViewCollection.swift file.
I place the following code at the bottom of the 'switch' statement in the MTSCardsPage.swift file.
//To capture the card title and store it for
//preparation for changing based on Label.
guard let DiscrimUIViewCollection : DiscrimUIViewCollection = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DiscrimUIViewCollection") as? DiscrimUIViewCollection else {
return
}
DiscrimUIViewCollection.updateTheLabel = sender.currentTitle!
present(DiscrimUIViewCollection, animated: true, completion: nil)
}
And to my delight, it all works fine!
The website I used to help me the most was this one:
https://fluffy.es/3-ways-to-pass-data-between-view-controllers/
Thanks for your assistance guys, each little comment made me think.
It's big learning curve!

UIBarButtonItem created programmatically not Tapping and linked

I created a view controller for a UIBarButtonItem, a toggle on the main controller. but the UIAction button is inactive
import UIKit
class SideMenuContentViewController: UIViewController {
//Manipulation for Dashboard Side Menu Hamburger
let profileButton = UIButton(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
profileButton.setImage(UIImage(named: "hamburger") , for: .normal)
profileButton.contentMode = .scaleAspectFit
profileButton.translatesAutoresizingMaskIntoConstraints = false
// function performed when the button is tapped
profileButton.addTarget(self, action: #selector(profileButtonTapped(_:)), for: .touchUpInside)
// Add the profile button as the left bar button of the navigation bar
let barbutton = UIBarButtonItem(customView: profileButton)
self.navigationItem.leftBarButtonItem = barbutton
// Set the width and height for the profile button
NSLayoutConstraint.activate([
profileButton.widthAnchor.constraint(equalToConstant: 35.0),
profileButton.heightAnchor.constraint(equalToConstant: 35.0)
])
// Make the profile button become circular
profileButton.layer.cornerRadius = 35.0 / 2
profileButton.clipsToBounds = true
}
#IBAction func profileButtonTapped(_ sender: Any){
}
}
import UIKit
class MainSideViewController: SideMenuContentViewController {
#IBOutlet weak var sideMenuContainer: UIView!
#IBOutlet weak var sideMenuViewLeadingConstraint: NSLayoutConstraint!
#IBOutlet weak var dashBoardViewLeadingConstraint: NSLayoutConstraint!
var sideMenuVisible = false
override func viewDidLoad() {
super.viewDidLoad()
sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
}
#objc func toggleSideMenu(fromViewController: SideMenuContentViewController) {
if(sideMenuVisible){
UIView.animate(withDuration: 0.5, animations: {
// hide the side menu to the left
self.sideMenuViewLeadingConstraint.constant = 0 - self.sideMenuContainer.frame.size.width
// move the content view (tab bar controller) to original position
self.dashBoardViewLeadingConstraint.constant = 0
self.view.layoutIfNeeded()
})
} else {
self.view.layoutIfNeeded()
UIView.animate(withDuration: 0.5, animations: {
// move the side menu to the right to show it
self.sideMenuViewLeadingConstraint.constant = 0
// move the content view (tab bar controller) to the right
self.dashBoardViewLeadingConstraint.constant = self.sideMenuContainer.frame.size.width
self.view.layoutIfNeeded()
})
}
sideMenuVisible = !sideMenuVisible
}
}
// THIS IS THE ACTION NOT WORKING
#IBAction override func profileButtonTapped(_ sender: Any){
if let mainVC = self.navigationController?.tabBarController?.parent as? MainSideViewController {
mainVC.toggleSideMenu(fromViewController: self)
}
}
}
I want the action to google in the side menu.

Xcode Swift: Cannot close popup Image

I created a ViewController that displays three images and other info via JSON parsing through three separate UIImageViews. When you click any of the images, it takes you to another ViewController that pop-ups a UIScrollView in the background, one UIImageView which is linked to all three images and a Button that would close the pop-up ViewController and bring it back to the previous one. Here is a screenshot. The problem I am having is that I added this code:
func removeZoom()
{
UIView.animateWithDuration(0.25, animations: {
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}
#IBAction func closeZoom(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
And when I try to click on the close button, nothing happens. Don't know what I am missing. Any guidance would be helpful.
Here i'll put the code for both controllers:
JnsDetail.swift
import Foundation
import UIKit
class JnsDetail: UIViewController {
#IBOutlet var tituloLabel : UILabel!
#IBOutlet var marcaLabel : UILabel!
#IBOutlet var colorLabel : UILabel!
#IBOutlet var tipoLabel : UILabel!
#IBOutlet var refLabel : UILabel!
#IBOutlet var imageView : UIImageView!
#IBOutlet var imageView2 : UIImageView!
#IBOutlet var imageView3 : UIImageView!
#IBOutlet var backbutton : UIButton!
var jsonextrct : JsonExtrct!
var photos : [String]!
var transitionOperator = TransitionOperator()
override func viewDidLoad() {
super.viewDidLoad()
//titulo = jsonextrct.titulo
tituloLabel.font = UIFont(name: mTheme.fontName, size: 21)
tituloLabel.textColor = UIColor.blackColor()
tituloLabel.text = jsonextrct.titulo
//marca = jsonextrct.marca
marcaLabel.font = UIFont(name: mTheme.fontName, size: 21)
marcaLabel.textColor = UIColor.blackColor()
marcaLabel.text = jsonextrct.marca
//color = jsonextrct.color
colorLabel.font = UIFont(name: mTheme.fontName, size: 21)
colorLabel.textColor = UIColor.blackColor()
colorLabel.text = jsonextrct.color
//tipo = jsonextrct.tipo
tipoLabel.font = UIFont(name: mTheme.fontName, size: 21)
tipoLabel.textColor = UIColor.blackColor()
tipoLabel.text = jsonextrct.tipo
//ref = jsonextrct.ref
refLabel.font = UIFont(name: mTheme.fontName, size: 21)
refLabel.textColor = UIColor.blackColor()
refLabel.text = "\(jsonextrct.ref)"
if let imageData = jsonextrct.imageData {
imageView.image = UIImage(data: imageData)
}else{
Utils.asyncLoadJsonImage(jsonextrct, imageView: imageView)
}
//topImageViewHeightConstraint.constant = 240
imageView.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
imageView.layer.borderWidth = 0.5
if let imageData2 = jsonextrct.imageData2 {
imageView2.image = UIImage(data: imageData2)
}else{
Utils.asyncLoadJsonImage(jsonextrct, imageView2: imageView2)
}
imageView2.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
imageView2.layer.borderWidth = 0.5
if let imageData3 = jsonextrct.imageData3 {
imageView3.image = UIImage(data: imageData3)
}else{
Utils.asyncLoadJsonImage(jsonextrct, imageView3: imageView3)
}
imageView3.layer.borderColor = UIColor(white: 0.2, alpha: 1.0).CGColor
imageView3.layer.borderWidth = 0.5
var tapGestureZoom = UITapGestureRecognizer(target: self, action: "zoomJns:")
tapGestureZoom.numberOfTapsRequired = 1
tapGestureZoom.numberOfTouchesRequired = 1
imageView.userInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureZoom)
var tapGestureZoom2 = UITapGestureRecognizer(target: self, action: "zoomJns2:")
tapGestureZoom2.numberOfTapsRequired = 1
tapGestureZoom2.numberOfTouchesRequired = 1
imageView2.userInteractionEnabled = true
imageView2.addGestureRecognizer(tapGestureZoom2)
var tapGestureZoom3 = UITapGestureRecognizer(target: self, action: "zoomJns3:")
tapGestureZoom3.numberOfTapsRequired = 1
tapGestureZoom3.numberOfTouchesRequired = 1
imageView3.userInteractionEnabled = true
imageView3.addGestureRecognizer(tapGestureZoom3)
}
override func preferredStatusBarStyle() -> UIStatusBarStyle {
return UIStatusBarStyle.Default
}
func backTapped(sender: AnyObject?){
dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func zoomJns(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
self.modalPresentationStyle = UIModalPresentationStyle.Custom
controller.transitioningDelegate = transitionOperator
controller.jsonextrct = jsonextrct
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func zoomJns2(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
self.modalPresentationStyle = UIModalPresentationStyle.Custom
controller.transitioningDelegate = transitionOperator
controller.jsonextrct = jsonextrct
presentViewController(controller, animated: true, completion: nil)
}
#IBAction func zoomJns3(sender: AnyObject?){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("JnsZoomController") as! JnsZoomController
self.modalPresentationStyle = UIModalPresentationStyle.Custom
controller.transitioningDelegate = transitionOperator
controller.jsonextrct = jsonextrct
presentViewController(controller, animated: true, completion: nil)
}
}
JnsZoomController.swift
import Foundation
import UIKit
class JnsZoomController : UIViewController {
#IBOutlet var scrollView : UIScrollView!
#IBOutlet var jnsImageView : UIImageView!
#IBOutlet var jnsImageView2 : UIImageView!
#IBOutlet var jnsImageView3 : UIImageView!
var jsonextrct : JsonExtrct!
override func viewDidLoad() {
super.viewDidLoad()
if let imageData = jsonextrct.imageData {
let image = UIImage(data: imageData)
jnsImageView.image = UIImage(data: imageData)
//jnsImageView.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
}
if let imageData2 = jsonextrct.imageData2 {
let image2 = UIImage(data: imageData2)
jnsImageView2.image = UIImage(data: imageData2)
//jnsImageView2.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
}
if let imageData3 = jsonextrct.imageData3 {
let image3 = UIImage(data: imageData3)
jnsImageView3.image = UIImage(data: imageData3)
//jnsImageView3.bounds = CGRectMake(0, 0, image?.size.width, image?.size.height);
}
scrollView.contentSize = jnsImageView.frame.size
scrollView.contentSize = jnsImageView2.frame.size
scrollView.contentSize = jnsImageView3.frame.size
}
func removeZoom()
{
UIView.animateWithDuration(0.25, animations: {
self.view.transform = CGAffineTransformMakeScale(1.3, 1.3)
self.view.alpha = 0.0;
}, completion:{(finished : Bool) in
if (finished)
{
self.view.removeFromSuperview()
}
});
}
#IBAction func closeZoom(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true)
}
}
Here's the problem as I see it, if you are "popping" to root view controller this means that you must have PUSHED a view controller onto the navigation controller's stack and I don't see you pushing anything onto a navigation stack. Unless of course for some reason Apple decided to kill off Pushing view controllers, but I doubt this is the case. So, there's another problem with what I'm seeing in your code. You are PRESENTING view controllers by just presenting a view controller, I don't see where you are presenting a view controller by using a navigation controller to present the view controller SOOO, if you call
self.navigationController?.popToRootViewControllerAnimated(true)
then there's nothing on the stack that the navigation controller is to remove from the stack since you presented the viewcontroller modally over another viewcontroller without presenting the modal in the view controller's navigation controller.
Solution, maybe, but this isn't 100% becuase I don't have your code in front of me.
change this:
self.navigationController?.popToRootViewControllerAnimated(true)
to something like this
self.dismissViewControllerAnimated(animated: true, completion:nil)
I don't do swift so my solution is pseudo code, feel free to add the questions marks and what not that Apple decided has value for some reason.
You could also just change your presentations to this:
self.navigationController?.presentViewController(controller, animated: true, completion: nil)
Again, the above is psuedo code but I think I place the question mark in just the right spot for so that it does what it's suppose to do
Also, you can refer to this, although Apple doesn't really do a very thoroughly job of telling you how an advanced navigaiton stack works:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationController_Class/#//apple_ref/occ/instm/UINavigationController/pushViewController:animated:
Sometimes you will need to have maybe 4-10 navigation controllers running at one time, so make sure you understand how they interact with view controllers and make sure you understand what POPs, PUSHes, and PRESENTs do. And good luck, have a good day.
In the closeZoom I think you should use only
#IBAction func closeZoom(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}
Because you presented that View Controller, that popToRootViewControllerAnimated(true) is used when you push it

Execute action when back bar button of UINavigationController is pressed

I need to execute an action (emptying an array), when the back button of a UINavigationController is pressed, while the button still causes the previous ViewController on the stack to appear. How could I accomplish this using swift?
Replacing the button to a custom one as suggested on another answer is possibly not a great idea as you will lose the default behavior and style.
One other option you have is to implement the viewWillDisappear method on the View Controller and check for a property named isMovingFromParentViewController. If that property is true, it means the View Controller is disappearing because it's being removed (popped).
Should look something like:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParentViewController {
// Your code...
}
}
In swift 4.2
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
// Your code...
}
}
One option would be implementing your own custom back button. You would need to add the following code to your viewDidLoad method:
- (void) viewDidLoad {
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(back:)];
self.navigationItem.leftBarButtonItem = newBackButton;
}
- (void) back:(UIBarButtonItem *)sender {
// Perform your custom actions
// ...
// Go back to the previous ViewController
[self.navigationController popViewControllerAnimated:YES];
}
UPDATE:
Here is the version for Swift:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Bordered, target: self, action: "back:")
self.navigationItem.leftBarButtonItem = newBackButton
}
#objc func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
self.navigationController?.popViewControllerAnimated(true)
}
UPDATE 2:
Here is the version for Swift 3:
override func viewDidLoad {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
#objc func back(sender: UIBarButtonItem) {
// Perform your custom actions
// ...
// Go back to the previous ViewController
_ = navigationController?.popViewController(animated: true)
}
override func willMove(toParent parent: UIViewController?)
{
super.willMove(toParent: parent)
if parent == nil
{
print("This VC is 'will' be popped. i.e. the back button was pressed.")
}
}
I was able to achieve this with the following :
Swift 3
override func didMoveToParentViewController(parent: UIViewController?) {
super.didMoveToParentViewController(parent)
if parent == nil {
println("Back Button pressed.")
delegate?.goingBack()
}
}
Swift 4
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
debugPrint("Back Button pressed.")
}
}
No need of custom back button.
If you want to have back button with back arrow you can use an image and code below
backArrow.png backArrow#2x.png backArrow#3x.png
override func viewDidLoad() {
super.viewDidLoad()
let customBackButton = UIBarButtonItem(image: UIImage(named: "backArrow") , style: .plain, target: self, action: #selector(backAction(sender:)))
customBackButton.imageInsets = UIEdgeInsets(top: 2, left: -8, bottom: 0, right: 0)
navigationItem.leftBarButtonItem = customBackButton
}
func backAction(sender: UIBarButtonItem) {
// custom actions here
navigationController?.popViewController(animated: true)
}
I created this (swift) class to create a back button exactly like the regular one, including back arrow. It can create a button with regular text or with an image.
Usage
weak var weakSelf = self
// Assign back button with back arrow and text (exactly like default back button)
navigationItem.leftBarButtonItems = CustomBackButton.createWithText("YourBackButtonTitle", color: UIColor.yourColor(), target: weakSelf, action: #selector(YourViewController.tappedBackButton))
// Assign back button with back arrow and image
navigationItem.leftBarButtonItems = CustomBackButton.createWithImage(UIImage(named: "yourImageName")!, color: UIColor.yourColor(), target: weakSelf, action: #selector(YourViewController.tappedBackButton))
func tappedBackButton() {
// Do your thing
self.navigationController!.popViewControllerAnimated(true)
}
CustomBackButtonClass
(code for drawing the back arrow created with Sketch & Paintcode plugin)
class CustomBackButton: NSObject {
class func createWithText(text: String, color: UIColor, target: AnyObject?, action: Selector) -> [UIBarButtonItem] {
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -8
let backArrowImage = imageOfBackArrow(color: color)
let backArrowButton = UIBarButtonItem(image: backArrowImage, style: UIBarButtonItemStyle.Plain, target: target, action: action)
let backTextButton = UIBarButtonItem(title: text, style: UIBarButtonItemStyle.Plain , target: target, action: action)
backTextButton.setTitlePositionAdjustment(UIOffset(horizontal: -12.0, vertical: 0.0), forBarMetrics: UIBarMetrics.Default)
return [negativeSpacer, backArrowButton, backTextButton]
}
class func createWithImage(image: UIImage, color: UIColor, target: AnyObject?, action: Selector) -> [UIBarButtonItem] {
// recommended maximum image height 22 points (i.e. 22 #1x, 44 #2x, 66 #3x)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
negativeSpacer.width = -8
let backArrowImageView = UIImageView(image: imageOfBackArrow(color: color))
let backImageView = UIImageView(image: image)
let customBarButton = UIButton(frame: CGRectMake(0,0,22 + backImageView.frame.width,22))
backImageView.frame = CGRectMake(22, 0, backImageView.frame.width, backImageView.frame.height)
customBarButton.addSubview(backArrowImageView)
customBarButton.addSubview(backImageView)
customBarButton.addTarget(target, action: action, forControlEvents: .TouchUpInside)
return [negativeSpacer, UIBarButtonItem(customView: customBarButton)]
}
private class func drawBackArrow(frame frame: CGRect = CGRect(x: 0, y: 0, width: 14, height: 22), color: UIColor = UIColor(hue: 0.59, saturation: 0.674, brightness: 0.886, alpha: 1), resizing: ResizingBehavior = .AspectFit) {
/// General Declarations
let context = UIGraphicsGetCurrentContext()!
/// Resize To Frame
CGContextSaveGState(context)
let resizedFrame = resizing.apply(rect: CGRect(x: 0, y: 0, width: 14, height: 22), target: frame)
CGContextTranslateCTM(context, resizedFrame.minX, resizedFrame.minY)
let resizedScale = CGSize(width: resizedFrame.width / 14, height: resizedFrame.height / 22)
CGContextScaleCTM(context, resizedScale.width, resizedScale.height)
/// Line
let line = UIBezierPath()
line.moveToPoint(CGPoint(x: 9, y: 9))
line.addLineToPoint(CGPoint.zero)
CGContextSaveGState(context)
CGContextTranslateCTM(context, 3, 11)
line.lineCapStyle = .Square
line.lineWidth = 3
color.setStroke()
line.stroke()
CGContextRestoreGState(context)
/// Line Copy
let lineCopy = UIBezierPath()
lineCopy.moveToPoint(CGPoint(x: 9, y: 0))
lineCopy.addLineToPoint(CGPoint(x: 0, y: 9))
CGContextSaveGState(context)
CGContextTranslateCTM(context, 3, 2)
lineCopy.lineCapStyle = .Square
lineCopy.lineWidth = 3
color.setStroke()
lineCopy.stroke()
CGContextRestoreGState(context)
CGContextRestoreGState(context)
}
private class func imageOfBackArrow(size size: CGSize = CGSize(width: 14, height: 22), color: UIColor = UIColor(hue: 0.59, saturation: 0.674, brightness: 0.886, alpha: 1), resizing: ResizingBehavior = .AspectFit) -> UIImage {
var image: UIImage
UIGraphicsBeginImageContextWithOptions(size, false, 0)
drawBackArrow(frame: CGRect(origin: CGPoint.zero, size: size), color: color, resizing: resizing)
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
private enum ResizingBehavior {
case AspectFit /// The content is proportionally resized to fit into the target rectangle.
case AspectFill /// The content is proportionally resized to completely fill the target rectangle.
case Stretch /// The content is stretched to match the entire target rectangle.
case Center /// The content is centered in the target rectangle, but it is NOT resized.
func apply(rect rect: CGRect, target: CGRect) -> CGRect {
if rect == target || target == CGRect.zero {
return rect
}
var scales = CGSize.zero
scales.width = abs(target.width / rect.width)
scales.height = abs(target.height / rect.height)
switch self {
case .AspectFit:
scales.width = min(scales.width, scales.height)
scales.height = scales.width
case .AspectFill:
scales.width = max(scales.width, scales.height)
scales.height = scales.width
case .Stretch:
break
case .Center:
scales.width = 1
scales.height = 1
}
var result = rect.standardized
result.size.width *= scales.width
result.size.height *= scales.height
result.origin.x = target.minX + (target.width - result.width) / 2
result.origin.y = target.minY + (target.height - result.height) / 2
return result
}
}
}
SWIFT 3.0
class CustomBackButton: NSObject {
class func createWithText(text: String, color: UIColor, target: AnyObject?, action: Selector) -> [UIBarButtonItem] {
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -8
let backArrowImage = imageOfBackArrow(color: color)
let backArrowButton = UIBarButtonItem(image: backArrowImage, style: UIBarButtonItemStyle.plain, target: target, action: action)
let backTextButton = UIBarButtonItem(title: text, style: UIBarButtonItemStyle.plain , target: target, action: action)
backTextButton.setTitlePositionAdjustment(UIOffset(horizontal: -12.0, vertical: 0.0), for: UIBarMetrics.default)
return [negativeSpacer, backArrowButton, backTextButton]
}
class func createWithImage(image: UIImage, color: UIColor, target: AnyObject?, action: Selector) -> [UIBarButtonItem] {
// recommended maximum image height 22 points (i.e. 22 #1x, 44 #2x, 66 #3x)
let negativeSpacer = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.fixedSpace, target: nil, action: nil)
negativeSpacer.width = -8
let backArrowImageView = UIImageView(image: imageOfBackArrow(color: color))
let backImageView = UIImageView(image: image)
let customBarButton = UIButton(frame: CGRect(x: 0, y: 0, width: 22 + backImageView.frame.width, height: 22))
backImageView.frame = CGRect(x: 22, y: 0, width: backImageView.frame.width, height: backImageView.frame.height)
customBarButton.addSubview(backArrowImageView)
customBarButton.addSubview(backImageView)
customBarButton.addTarget(target, action: action, for: .touchUpInside)
return [negativeSpacer, UIBarButtonItem(customView: customBarButton)]
}
private class func drawBackArrow(_ frame: CGRect = CGRect(x: 0, y: 0, width: 14, height: 22), color: UIColor = UIColor(hue: 0.59, saturation: 0.674, brightness: 0.886, alpha: 1), resizing: ResizingBehavior = .AspectFit) {
/// General Declarations
let context = UIGraphicsGetCurrentContext()!
/// Resize To Frame
context.saveGState()
let resizedFrame = resizing.apply(CGRect(x: 0, y: 0, width: 14, height: 22), target: frame)
context.translateBy(x: resizedFrame.minX, y: resizedFrame.minY)
let resizedScale = CGSize(width: resizedFrame.width / 14, height: resizedFrame.height / 22)
context.scaleBy(x: resizedScale.width, y: resizedScale.height)
/// Line
let line = UIBezierPath()
line.move(to: CGPoint(x: 9, y: 9))
line.addLine(to: CGPoint.zero)
context.saveGState()
context.translateBy(x: 3, y: 11)
line.lineCapStyle = .square
line.lineWidth = 3
color.setStroke()
line.stroke()
context.restoreGState()
/// Line Copy
let lineCopy = UIBezierPath()
lineCopy.move(to: CGPoint(x: 9, y: 0))
lineCopy.addLine(to: CGPoint(x: 0, y: 9))
context.saveGState()
context.translateBy(x: 3, y: 2)
lineCopy.lineCapStyle = .square
lineCopy.lineWidth = 3
color.setStroke()
lineCopy.stroke()
context.restoreGState()
context.restoreGState()
}
private class func imageOfBackArrow(_ size: CGSize = CGSize(width: 14, height: 22), color: UIColor = UIColor(hue: 0.59, saturation: 0.674, brightness: 0.886, alpha: 1), resizing: ResizingBehavior = .AspectFit) -> UIImage {
var image: UIImage
UIGraphicsBeginImageContextWithOptions(size, false, 0)
drawBackArrow(CGRect(origin: CGPoint.zero, size: size), color: color, resizing: resizing)
image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
private enum ResizingBehavior {
case AspectFit /// The content is proportionally resized to fit into the target rectangle.
case AspectFill /// The content is proportionally resized to completely fill the target rectangle.
case Stretch /// The content is stretched to match the entire target rectangle.
case Center /// The content is centered in the target rectangle, but it is NOT resized.
func apply(_ rect: CGRect, target: CGRect) -> CGRect {
if rect == target || target == CGRect.zero {
return rect
}
var scales = CGSize.zero
scales.width = abs(target.width / rect.width)
scales.height = abs(target.height / rect.height)
switch self {
case .AspectFit:
scales.width = min(scales.width, scales.height)
scales.height = scales.width
case .AspectFill:
scales.width = max(scales.width, scales.height)
scales.height = scales.width
case .Stretch:
break
case .Center:
scales.width = 1
scales.height = 1
}
var result = rect.standardized
result.size.width *= scales.width
result.size.height *= scales.height
result.origin.x = target.minX + (target.width - result.width) / 2
result.origin.y = target.minY + (target.height - result.height) / 2
return result
}
}
}
In Swift 5 and Xcode 10.2
Please don't add custom bar button item, use this default behaviour.
No need of viewWillDisappear, no need of custom BarButtonItem etc...
It's better to detect when the VC is removed from it's parent.
Use any one of these two functions
override func willMove(toParent parent: UIViewController?) {
super.willMove(toParent: parent)
if parent == nil {
callStatusDelegate?.backButtonClicked()//Here write your code
}
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
callStatusDelegate?.backButtonClicked()//Here write your code
}
}
If you want stop default behaviour of back button then add custom BarButtonItem.
If you are using navigationController then add the UINavigationControllerDelegate protocol to class and add the delegate method as follows:
class ViewController:UINavigationControllerDelegate {
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController,
animated: Bool) {
if viewController === self {
// do here what you want
}
}
}
This method is called whenever the navigation controller will slide to a new screen. If the back button was pressed, the new view controller is ViewController itself.
You can subclass UINavigationController and override popViewController(animated: Bool). Beside being able to execute some code there you can also prevent the user from going back altogether, for instance to prompt to save or discard his current work.
Sample implementation where you can set a popHandler that gets set/cleared by pushed controllers.
class NavigationController: UINavigationController
{
var popHandler: (() -> Bool)?
override func popViewController(animated: Bool) -> UIViewController?
{
guard self.popHandler?() != false else
{
return nil
}
self.popHandler = nil
return super.popViewController(animated: animated)
}
}
And sample usage from a pushed controller that tracks unsaved work.
let hasUnsavedWork: Bool = // ...
(self.navigationController as! NavigationController).popHandler = hasUnsavedWork ?
{
// Prompt saving work here with an alert
return false // Prevent pop until as user choses to save or discard
} : nil // No unsaved work, we clear popHandler to let it pop normally
As a nice touch, this will also get called by interactivePopGestureRecognizer when the user tries to go back using a swipe gesture.
NO
override func willMove(toParentViewController parent: UIViewController?) { }
This will get called even if you are segueing to the view controller in which you are overriding this method. In which check if the "parent" is nil of not is not a precise way to be sure of moving back to the correct UIViewController. To determine exactly if the UINavigationController is properly navigating back to the UIViewController that presented this current one, you will need to conform to the UINavigationControllerDelegate protocol.
YES
note: MyViewController is just the name of whatever UIViewController you want to detect going back from.
1) At the top of your file add UINavigationControllerDelegate.
class MyViewController: UIViewController, UINavigationControllerDelegate {
2) Add a property to your class that will keep track of the UIViewController that you are segueing from.
class MyViewController: UIViewController, UINavigationControllerDelegate {
var previousViewController:UIViewController
3) in MyViewController's viewDidLoad method assign self as the delegate for your UINavigationController.
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}
3) Before you segue, assign the previous UIViewController as this property.
// In previous UIViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueID" {
if let nextViewController = segue.destination as? MyViewController {
nextViewController.previousViewController = self
}
}
}
4) And conform to one method in MyViewController of the UINavigationControllerDelegate
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
if viewController == self.previousViewController {
// You are going back
}
}
In my case the viewWillDisappear worked best. But in some cases one has to modify the previous view controller. So here is my solution with access to the previous view controller and it works in Swift 4:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isMovingFromParentViewController {
if let viewControllers = self.navigationController?.viewControllers {
if (viewControllers.count >= 1) {
let previousViewController = viewControllers[viewControllers.count-1] as! NameOfDestinationViewController
// whatever you want to do
previousViewController.callOrModifySomething()
}
}
}
}
Before leave current controller I need to show alert. So I did it this way:
Add extention to UINavigationController with UINavigationBarDelegate
Add selector to your controller navigationShouldPopOnBack(completion:)
It's worked)
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if let items = navigationBar.items, viewControllers.count < items.count {
return true
}
let clientInfoVC = topViewController as? ClientInfoVC
if clientInfoVC?.responds(to: #selector(clientInfoVC?.navigationShouldPopOnBack)) ?? false {
clientInfoVC?.navigationShouldPopOnBack(completion: { isAllowPop in
if isAllowPop {
DispatchQueue.main.async {
self.popViewController(animated: true)
}
}
})
}
DispatchQueue.main.async {
self.popViewController(animated: true)
}
return false
}
}
#objc func navigationShouldPopOnBack(completion: #escaping (Bool) -> ()) {
let ok = UIAlertAction(title: R.string.alert.actionOk(), style: .default) { _ in
completion(true)
}
let cancel = UIAlertAction(title: R.string.alert.actionCancel(), style: .cancel) { _ in
completion(false)
}
let alertController = UIAlertController(title: "", message: R.string.alert.contractMessage(), preferredStyle: .alert)
alertController.addAction(ok)
alertController.addAction(cancel)
present(alertController, animated: true, completion: nil)
}
When back button is pressed, ignore interactive pop with screen edge gesture.
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if isMovingFromParent, transitionCoordinator?.isInteractive == false {
// code here
}
}
It's not difficult as we thing. Just create a frame for UIButton with clear background color, assign action for the button and place over the navigationbar back button. And finally remove the button after use.
Here is the Swift 3
sample code done with UIImage instead of UIButton
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
imageView.backgroundColor = UIColor.clear
imageView.frame = CGRect(x:0,y:0,width:2*(self.navigationController?.navigationBar.bounds.height)!,height:(self.navigationController?.navigationBar.bounds.height)!)
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(back(sender:)))
imageView.isUserInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
imageView.tag = 1
self.navigationController?.navigationBar.addSubview(imageView)
}
write the code need to be executed
func back(sender: UIBarButtonItem) {
// Perform your custom actions}
_ = self.navigationController?.popViewController(animated: true)
}
Remove the subView after action is performed
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
for view in (self.navigationController?.navigationBar.subviews)!{
if view.tag == 1 {
view.removeFromSuperview()
}
}
This is my solution
extension UINavigationController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if let shouldBlock = self.topViewController?.shouldPopFromNavigation() {
return shouldBlock
}
return true
}
}
extension UIViewController {
#objc func shouldPopFromNavigation() -> Bool {
return true
}
}
In your view controller, you can handle like this:
#objc override func shouldPopFromNavigation() -> Bool {
// Your dialog, example UIAlertViewController or whatever you want
return false
}
Swift 4.2:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
// Your code...
}
}
For Swift 5, we can check it in view will disappear
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
delegate?.passValue(clickedImage: selectedImage)
}
}
Swift 3:
override func didMove(toParentViewController parent: UIViewController?) {
super.didMove(toParentViewController: parent)
if parent == nil{
print("Back button was clicked")
}
}
just do control + drag the bar item to below func. work like charm
#IBAction func done(sender: AnyObject) {
if((self.presentingViewController) != nil){
self.dismiss(animated: false, completion: nil)
print("done")
}
}
Swift 5 __ Xcode 11.5
In my case I wanted to make an animation, and when it finished, go back.
A way to overwrite the default action of the back button
and call your custom action is this:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
setBtnBack()
}
private func setBtnBack() {
for vw in navigationController?.navigationBar.subviews ?? [] where "\(vw.classForCoder)" == "_UINavigationBarContentView" {
print("\(vw.classForCoder)")
for subVw in vw.subviews where "\(subVw.classForCoder)" == "_UIButtonBarButton" {
let ctrl = subVw as! UIControl
ctrl.removeTarget(ctrl.allTargets.first, action: nil, for: .allEvents)
ctrl.addTarget(self, action: #selector(backBarBtnAction), for: .touchUpInside)
}
}
}
#objc func backBarBtnAction() {
doSomethingBeforeBack { [weak self](isEndedOk) in
if isEndedOk {
self?.navigationController?.popViewController(animated: true)
}
}
}
private func doSomethingBeforeBack(completion: #escaping (_ isEndedOk:Bool)->Void ) {
UIView.animate(withDuration: 0.25, animations: { [weak self] in
self?.vwTxt.alpha = 0
}) { (isEnded) in
completion(isEnded)
}
}
Or you can use this method one time to explore the NavigationBar view hierarchy, and get the indexes to access to the _UIButtonBarButton view, cast to UIControl, remove the target-action, and add your custom targets-actions:
private func debug_printSubviews(arrSubviews:[UIView]?, level:Int) {
for (i,subVw) in (arrSubviews ?? []).enumerated() {
var str = ""
for _ in 0...level {
str += "\t"
}
str += String(format: "%2d %#",i, "\(subVw.classForCoder)")
print(str)
debug_printSubviews(arrSubviews: subVw.subviews, level: level + 1)
}
}
// Set directly the indexs
private func setBtnBack_method2() {
// Remove or comment the print lines
debug_printSubviews(arrSubviews: navigationController?.navigationBar.subviews, level: 0)
let ctrl = navigationController?.navigationBar.subviews[1].subviews[0] as! UIControl
print("ctrl.allTargets: \(ctrl.allTargets)")
ctrl.removeTarget(ctrl.allTargets.first, action: nil, for: .allEvents)
print("ctrl.allTargets: \(ctrl.allTargets)")
ctrl.addTarget(self, action: #selector(backBarBtnAction), for: .touchUpInside)
print("ctrl.allTargets: \(ctrl.allTargets)")
}
override public func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.topItem?.title = GlobalVariables.selectedMainIconName
let image = UIImage(named: "back-btn")
image = image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: image, style: UIBarButtonItemStyle.Plain, target: self, action: #selector(Current[enter image description here][1]ViewController.back) )
}
func back() {
self.navigationController?.popToViewController( self.navigationController!.viewControllers[ self.navigationController!.viewControllers.count - 2 ], animated: true)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingToParent {
//your code backView
}
}
Try this .
self.navigationItem.leftBarButtonItem?.target = "methodname"
func methodname ( ) {
// enter code here
}
Try on this too.
override func viewWillAppear(animated: Bool) {
//empty your array
}
As I understand you want to empty your array as you press your back button and pop to your previous ViewController let your Array which you loaded on this screen is
let settingArray = NSMutableArray()
#IBAction func Back(sender: AnyObject) {
self. settingArray.removeAllObjects()
self.dismissViewControllerAnimated(true, completion: nil)
}
Here is the simplest possible Swift 5 solution that doesn't require you to create a custom back button and give up all that UINavigationController left button functionality you get for free.
As Brandon A recommends above, you need need to implement UINavigationControllerDelegate in the view controller you want to interact with before returning to it. A good way is to create an unwind segue that you can perform manually or automatically and reuse the same code from a custom done button or the back button.
First, make your view controller of interest (the one you want to detect returning to) a delegate of the navigation controller in its viewDidLoad:
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.delegate = self
}
Second, add an extension at the bottom of the file that overrides navigationController(willShow:animated:)
extension PickerTableViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool) {
if let _ = viewController as? EditComicBookViewController {
let selectedItemRow = itemList.firstIndex(of: selectedItemName)
selectedItemIndex = IndexPath(row: selectedItemRow!, section: 0)
if let selectedCell = tableView.cellForRow(at: selectedItemIndex) {
performSegue(withIdentifier: "PickedItem", sender: selectedCell)
}
}
}
}
Since your question included a UITableViewController, I included a way to get the index path of the row the user tapped.
I accomplished this by calling/overriding viewWillDisappear and then accessing the stack of the navigationController like this:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
let stack = self.navigationController?.viewControllers.count
if stack >= 2 {
// for whatever reason, the last item on the stack is the TaskBuilderViewController (not self), so we only use -1 to access it
if let lastitem = self.navigationController?.viewControllers[stack! - 1] as? theViewControllerYoureTryingToAccess {
// hand over the data via public property or call a public method of theViewControllerYoureTryingToAccess, like
lastitem.emptyArray()
lastitem.value = 5
}
}
}
You can do something in your Viewcontroller like
override func navigationShouldPopOnBackButton() -> Bool {
self.backAction() //Your action you want to perform.
return true
}
For complete answer use
Detecting when the 'back' button is pressed on a navbar
My preference was to override the popViewController in the Navigation Controller. The advantages of this is:
Your app keeps the default Back Button look and animations, and you don't have to manage it. This is particularly helpful if a user has Large Text set on their phone, since the default back button will increase or decrease in size based on the user settings.
You can stop the view from popping altogether, unlike using viewWillDisappear.
First, create a custom Navigation Controller class (and be sure to assign it to the Navigation Controller in your Story Board or wherever your navigation controller is created):
class NavControllerWithBackButtonOverride: UINavigationController {
var backButtonOverride: (() -> Void)? = nil
override func popViewController(animated: Bool) -> UIViewController? {
if backButtonOverride != nil {
//if anything is assigned to the backButtonOverride the override will run
self.backButtonOverride!()
return nil
} else {
//otherwise the default popViewController will run
return super.popViewController(animated: animated)
}
}
}
Then enable/disable the override in your View Controller by assigning a value to the backButtonOverride variable:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.enableCustomBackButton()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.disableCustomBackButton()
}
/**
Custom Back Button
*/
func customBackButtonAction() {
print("DO THIS INSTEAD")
}
func enableCustomBackButton() {
if let nav = self.navigationController as? NavControllerWithBackButtonOverride {
nav.backButtonOverride = { self.customBackButtonAction() }
nav.interactivePopGestureRecognizer?.isEnabled = false
}
}
func disableCustomBackButton() {
if let nav = self.navigationController as? NavControllerWithBackButtonOverride {
nav.backButtonOverride = nil
nav.interactivePopGestureRecognizer?.isEnabled = true
}
}
Note: I also disabled interactivePopGestureRecognizer because it was causing issues with the custom setup.
Swift 5+ (Back button with alert control)
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.hidesBackButton = true
let newBackButton = UIBarButtonItem(title: "<Back", style: UIBarButtonItem.Style.plain, target: self, action: #selector(PGWebViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
}
#objc func back(sender: UIBarButtonItem) {
let alert = UIAlertController(title: "Warning!", message: "Your payment process is not completed yet. Do you want to go back?", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: { action in
_ = self.navigationController?.popViewController(animated: true)
})
alert.addAction(ok)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})}
You can simply remove unnecessary controllers from the stack, something like this:
self.navigationController?.viewControllers.removeAll(where: {
$0 is FirstViewController || $0 is SecondWithPinController
})

Resources