How to show back button in a tab bar viewcontroller? - ios

I have a navigation controller with a home view controller on its root. Then I push a tab bar view controller. The back button disappears. How can I return from the tab bar view controller to the home view controller via back button? How can I make it visible again?
I have tried:
let navItem = self.navigationController?.navigationItem
let navItem2 = self.navigationItem;
leftBarButton = UIBarButtonItem()
leftBarButton.image = UIImage(named: "arrows-back-icon-24.png")
leftBarButton.action = #selector(self.popViewController);
leftBarButton.target = self
navItem?.leftBarButtonItem = leftBarButton
navItem2.leftBarButtonItem = leftBarButton;
I also have tried:
let navItem = self.navigationController?.navigationItem
let navItem2 = self.navigationItem;
navItem?.leftBarButtonItem = nil;
navItem2.leftBarButtonItem = nil;
All is not working. Please help. Thanks.

Try this: Assign UITabBarController class file to Tab Bar Controller, just like view controller.
import UIKit
//this is TabBarController.swift file
class TabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
And push it from the HomeVC like this (here, I am using UIButton for Push):
#IBAction func btnPush(_ sender: UIButton) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
self.navigationController?.pushViewController(vc, animated: true)
}

I had the same issue, but in my case, it was due to the viewWillAppear method. Try adding the following to each of the UIViewControllers embedded in the corresponding UITabBarController:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.navigationController?.setNavigationBarHidden(false, animated: true)
}
Hope this works for you.

let btn1 = UIButton(type: .custom)
btn1.setImage(UIImage(named: "image"), for: .normal)
btn1.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
btn1.addTarget(self, action: #selector(methodname), for: .touchUpInside)
let item1 = UIBarButtonItem(customView: btn1)
let btn2 = UIButton(type: .custom)
btn2.setImage(UIImage(named: "image"), for: .normal)
btn2.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
btn2.addTarget(self, action: #selector(methodName), for: .touchUpInside)
let item2 = UIBarButtonItem(customView: btn2)
self.navigationItem.setLeftBarButtonItems([item1,item2], animated: true)
Try this

Related

Trying to call a navigation bar function in another class pragmatically

I created a navigation bar which I'm trying to call in another view controller. I set it up by calling the methods which I separated into left, center and right buttons. In my other controller I call the navbarcontroller and try and call the method for which i setup the navigation toolbar. Nothing happens, however there is no crash.
import UIKit
class NavBarController : UIViewController{
var screenSize: CGRect!
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
setupToolBarItems()
self.navigationController?.isToolbarHidden = false
self.view!.backgroundColor = .white
}
and my method for the navigation bar is this
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
}
func showCalendarController() {
let navController = CalendarController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func showEventsController() {
let navController = EventsController()
self.present(navController, animated: true, completion: nil)
} //connect bottom bar buttons to controller
func setupNavigationBarItems() {
setupCenterNavButton()
setupLeftNavButton()
setupRightNavButton()
} // top bar button setup
private func setupCenterNavButton() {
let buttonFrame = UIView(frame: CGRect(x: 0, y: 0, width: 165,
height: 20))
mainFeedButton.frame = CGRect(x: 0,y: 0, width: 80,height: 20) as
CGRect
mainFeedButton.backgroundColor = UIColor.blue
peekFeedButton.frame = CGRect(x: 85,y: 0, width: 80,height: 20) as
CGRect
peekFeedButton.backgroundColor = UIColor.blue
buttonFrame.addSubview(mainFeedButton)
buttonFrame.addSubview(peekFeedButton)
navigationItem.titleView = buttonFrame
} //center bar buttons / action setup
private func setupLeftNavButton() {
navigationItem.leftBarButtonItem = UIBarButtonItem(customView:
favoriteButton)
}// left bar buttons / action setup
private func setupRightNavButton() {
navigationItem.rightBarButtonItem = UIBarButtonItem(customView:
moreButton)
} //right bar buttons / action setup
lazy var mainFeedButton: UIButton! = {
let button = UIButton(type: .custom) // button type
button.setTitle("Main",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 0, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showMainFeedController),
for: .touchUpInside)
return button
}() //mainFeed button connected to Feed Controller
lazy var peekFeedButton: UIButton! = {
let button = UIButton(type: .custom) //button type
button.setTitle("Spy",for: .normal) //button title
button.sizeToFit() // size button to fit the title
var frame = button.frame //create frame to manipulate the body
button.frame = CGRect(x: 20, y: 0, width: 100, height: 40)
button.addTarget(self, action:
#selector(self.showSpyFeedController),
for: .touchUpInside)
return button
}()//peekFeed button frame and action setup
lazy var favoriteButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"star").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(favoriteButton_tapped),
for: .touchUpInside)
return button
}() //favorites button frame and action setup
lazy var moreButton: UIButton! = {
let button = UIButton(type: .system) //default button with blue
text
button.setImage(#imageLiteral(resourceName:
"more").withRenderingMode(.alwaysOriginal), for: .normal)
button.contentMode = .scaleAspectFit
button.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
button.addTarget(self, action: #selector(moreButton_tapped),
for: .touchUpInside)
return button
}() //more button frame and action setup
func showMainFeedController() {
let navController = MainFeedController()
self.present(navController, animated: true, completion: nil)
} //mainFeed button connected to Feed Controller
func showSpyFeedController() {
let navController = SpyFeedController()
self.present(navController, animated: true, completion: nil)
}//peekFeed button connected to SpyFeedController
func favoriteButton_tapped(sender: UIButton) {
print("You touched this!")
}
func moreButton_tapped(sender: UIButton) {
print("You touched this!")
}
}
I then try and call the function by setupNavigationBarItems() like this
import UIKit
class EventsController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
let navbar = NavBarController()
navbar.setupNavigationBarItems()
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
}
}
I'm not sure if this a valid way. I'm still kinda new to all of this.
It's not clear what you expect to happen, but here's what does happen:
let navbar = NavBarController()
A completely new NavBarController object is created.
navbar.setupNavigationBarItems()
That NavBarController object's setupNavigationBarItems is called.
self.navigationController?.isToolbarHidden = false
self.view.backgroundColor = .white
Your code comes to an end. navbar was a local variable, so the NavBarController object vanishes in a puff of smoke. The end. This object was created and configured to no purpose.
I remember my first month in iOS way back 2015 :D, didn't have any knowledge in OOP, I didn't know too how to pass a data to another screen or class.
Anyways, you DO NOT create a new instance of your NavBarController class in your EventsController. If you want to talk to your NavBarController from your EventsController, then you will need a reference that is currently alive. You can also use delegate (search for that later).
So before you show or present your EventsController from your NavBarController, pass your current NavBarController instance to the next screen which is EventsController. BUT FIRST, you need to declare a variable in your EventsController, correct? :)
Declare a variable with a type of NavBarController inside your EventsController class, like so:
var navBarController: NavBarController!
Then in this piece of code of yours, pass your self (the NavBarController instance) to the EventsController class before showing or presenting, take note that you mistakenly gave a wrong name to your EventsController new instance, so I renamed it:
func showEventsController() {
let eventsController = EventsController()
eventsController.navBarController = self // THIS :)
self.present(eventsController, animated: true, completion: nil)
}
Lastly, instead of this:
let navbar = NavBarController()
navbar.setupNavigationBarItems()
Make use of your declared variable, like so:
self.navBarController.navbar.setupNavigationBarItems()
Hope this helps! :)

How to replace back button in Material in Swift

I want to change behavior of back button and I replace back button of Material with new bar button like bellow:
let backButton: UIButton = {
let btn = UIButton()
btn.setImage(UIImage(named: "prev"), for: .normal)
btn.frame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
return btn
}()
In viewDidLoad function:
override func viewDidLoad() {
super.viewDidLoad()
backButton.action = { [weak self] in self?.showAlert() }
let barButton = UIBarButtonItem(customView: backButton)
navigationItem.backButton.isHidden = true
navigationItem.leftBarButtonItems = [barButton]
}
But I can not click to new back button because it was coverred by UINavigationBarBackIndicatorView.
How can I replace back button with Material. I want to keep Material library because in other features of project I need Material.
Thanks in advance for help.
Do this in your viewDidLoad() method :
self.navigationItem.backBarButtonItem.hidden = YES
let newBackButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.plain, target: self, action: #selector(YourViewController.back(sender:)))
self.navigationItem.leftBarButtonItem = newBackButton
self.navigationItem.leftBarButtonItem?.image = UIImage(named : "prev")
self.navigationItem.leftBarButtonItem?.accessibilityFrame = CGRect(x: 0, y: 0, width: 33/2, height: 27/2)
Then define a function named back in your view controller,this function will perform the desired action for your new back button
func back(sender: UIBarButtonItem) {
// Perform your custom action
}
Note No need to define a separate button variable
**If self.navigationItem.backBarButtonItem.hidden = YES doesnt works then replace it with self.navigationItem.backBarButtonItem = nil; **
Using backButton.isHidden with leftViews containing the new custom IconButtom works for me, I'm using Material 2.17.0.
func setNavigation() {
let backButton = IconButton(image: Icon.cm.arrowBack)
backButton.addTarget(self, action: #selector(onBackTapped), for: .touchUpInside)
navigationItem.leftViews = [backButton]
navigationItem.backButton.isHidden = true
}

Custom button in navigation bar - added and gets tapped but not visible

I try to add custom back button in navigation controller. I create button like this
func setupBackButton() {
let backButton = UIButton.init(type: .custom)
backButton.addTarget(self, action: #selector(backButtonHandler(_:)), for: .touchUpInside)
backButton.setTitleColor(UIColor.white, for: .normal)
backButton.setTitle("<", for: .normal)
backButton.tintColor = UIColor.white
let barBackButton = UIBarButtonItem(customView: backButton)
self.navigationItem.leftBarButtonItem = barBackButton
}
The problem is that button is being added (I can tap it and action on button is being executed) but i can't see button in controller - it is invisible.
Controller's color is blue.
you missed the frame of your button
backButton.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
dont forget to add the action handler
func backButtonHandler(_ sender : UIButton) {
}
try this :
func setupBackButton(){
let backBtn : UIBarButtonItem = UIBarButtonItem(title: "<", style: UIBarButtonItemStyle.plain, target: self, action:#selector(backToPreviousVC))
self.navigationItem.leftBarButtonItem = backBtn
backBtn.tintColor = UIColor.white
}
func backToPreviousVC() {
self.navigationController?.popViewController(animated: true)
}
call setupBackButton() in viewDidLoad

Eliminate repeated code Swift 3

I have multiple navigation controllers and their root view controllers in my app. I want each navigation bar to have social media buttons closely placed on the right side of the bar. For the same I have used this code to show the buttons in 1 view controller:
let fbImage = UIImage(named: "Facebook.png")!
let twitterImage = UIImage(named: "Twitter.png")!
let youtbImage = UIImage(named:"YouTube.png")!
let fbBtn: UIButton = UIButton(type: .custom)
fbBtn.setImage(fbImage, for: UIControlState.normal)
fbBtn.addTarget(self, action: #selector(HomeViewController.fbBtnPressed), for: UIControlEvents.touchUpInside)
fbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let fbBarBtn = UIBarButtonItem(customView: fbBtn)
let twitterBtn: UIButton = UIButton(type: .custom)
twitterBtn.setImage(twitterImage, for: UIControlState.normal)
twitterBtn.addTarget(self, action: #selector(HomeViewController.twitterBtnPressed), for: UIControlEvents.touchUpInside)
twitterBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let twitterBarBtn = UIBarButtonItem(customView: twitterBtn)
let youtbBtn: UIButton = UIButton(type: .custom)
youtbBtn.setImage(youtbImage, for: UIControlState.normal)
youtbBtn.addTarget(self, action: #selector(HomeViewController.youtubeBtnPressed), for: UIControlEvents.touchUpInside)
youtbBtn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
let youtbBarBtn = UIBarButtonItem(customView: youtbBtn)
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
Now I want the same buttons on all navigations bars. I can easily copy this code and respective target methods in viewDidLoad() of each view controller, but too much code is getting repeated. So how can avoid this situation?
I am using Swift 3. I am new to iOS. Any help will be appreciated!
Most duplications are solved by using functions. The first step is to extract that code into a function, the second step is to use the same function from multiple places.
You can add it to an extension, for example:
extension UIViewController {
func addShareButtons() {
...
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
}
}
and only call
self.addShareButtons()
from every controller that needs the buttons.
You can add button handlers to the extension too.
Another method is to use a UIViewController subclass but that's always a problem if you want to use a UITableViewController subclass.
You can design a custom navigation controller and add these code to the navigation controller. inherit the navigation controller to your storyboard or programmatically where you want to use.
//sample code
class "YourNavigationCorollerName": UINavigationController, UINavigationControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
//declare here you code, what you want
}
}
Assuming that these buttons ALWAYS have the same behavior, you can create a custom class for them, and place the repeated code there. For instance:
class FacebookButton : UIButton {
override init() {
super.init()
self.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.setImage(UIImage(named: "Facebook.png")!, for: .normal)
}
}
...I think the compiler will yell at you for initializing a UIView without a decoder and frame, but hopefully you get the idea.
To improve on this, you could 1) create a custom UINavigationController with the UIBarButtonItems you want to use, and reuse that controller, and/or 2) create a protocol like the following:
protocol FacebookBtnHandler {
func fbBtnPressed()
}
...and have any relevant VCs conform to it. This would allow you to assign the target and selector for the button in the FacebookButton init method, where you assign the image and frame, and hence prevent repetition of that line, as well.
Try to implement bar button by creating subclass of UIBarButton
class Button: UIBarButtonItem {
convenience init(withImage image : UIImage,Target target: Any, andSelector selector: Any?){
let button = UIButton(type: .custom)
button.setImage(image, for: UIControlState.normal)
button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
button.addTarget(target, action: selector, for: UIControlEvents.touchUpInside)
self.init(customView: button)
}
}
let fbImage = UIImage(named: "Facebook.png")!
let twitterImage = UIImage(named: "Twitter.png")!
let youtbImage = UIImage(named:"YouTube.png")!
let fbBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.fbBtnPressed))
let twitterBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.twitterBtnPressed))
let youtubeBtn = Button(withImage: fbImage, Target: self, andSelector: #selector(HomeViewController.youtubeBtnPressed))
self.navigationItem.setRightBarButtonItems([youtbBarBtn, twitterBarBtn, fbBarBtn], animated: false)
and make it for all your view controller
extension UIViewController {
func addButtons() {
// add above code
}
}

UIButton - Giving the button an action?

I have made a button through code (not on my storyboard) and yes it shows up no problem but now I want it to do an action when it is pressed.
Code for button:
let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "Action: ", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(backToHomeButton)
and I want it to go back to the mainScreen.
I have tried this and did not work:
let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "Action: ", forControlEvents: UIControlEvents.TouchUpInside)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuViewController") as! UIViewController
self.view?.addSubview(backToHomeButton)
Can someone tell me what I am doing wrong and how can I fix it? Thanks!
Try this:
let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(backToHomeButton)
// present
func Action(sender:UIButton)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MainMenuViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
}
//second Option if you are pushing back to main view
func Action(sender:UIButton)
{
navigationController?.popToRootViewControllerAnimated(true)
}
// or the previous controller
func Action(sender:UIButton)
{
navigationController?.popViewControllerAnimated(true)
}
You just need to add an Action to the button, and Dismiss the view in the action method:
For the button copy this:
let backToHomeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
backToHomeButton.frame = CGRectMake(100, 100, 100, 50)
backToHomeButton.backgroundColor = UIColor.greenColor()
backToHomeButton.setTitle("Button", forState: UIControlState.Normal)
backToHomeButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
Then just add this method. And choose the dismiss method depending on how you presented the view. If not sure try one then the other.
func pressed(sender: UIButton!) {
//IF YOU PRESENTED THE VIEW AS A MODAL
self.dismissViewControllerAnimated(true, completion: {});
//IF YOU PUSHED INTO THE VIEW
navigationController.popViewControllerAnimated(true)
}
Hope it helps.

Resources