Tab bar item not showing - ios

if(!isUserLoggedIn){
var loginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}else{
var mainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! FirstViewController
var myDealsViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("myDealsViewController") as!MyDealsViewController
var settingsViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("settingsView") as! SettingsViewController
var centerNav = UINavigationController(rootViewController: mainViewController) as UINavigationController
var myDealNavController = UINavigationController(rootViewController: myDealsViewController) as UINavigationController
let controllers = [centerNav,myDealNavController,settingsViewController]
tabBarController.viewControllers = controllers
window!.rootViewController = tabBarController
window!.makeKeyAndVisible()
}
the above code is working but the app is not showing the tab bar item even though it works when I click on the center tab bar item.

Replace line of your code with :
var myDealNavController = mainStoryBoard.instantiateViewControllerWithIdentifier("myDealNavController") as! UINavigationController
And set "myDealNavController" as identifier to your UINavigationController in storyboard.

Related

Removing the displayModeButton in UISplitViewController in iOS

I want to remove the displayModeButton from the detailViewController's Navigation Bar in splitveiwconroller. I tried setting leftBarButtionItem.isEnable = false in AppDelegate.swift file(as below)
if let splitViewController = self.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem?.isEnabled = false
}
it hides the displaymodebutton intially when the application start. But when i swipt right to open the master view and then swipe left to dismiss the masterview controller the dispaly mode buttom reappars again. Please help me to hide the displayModeButton from detail view conroller.
Do the same in master viewController viewWillAppear:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window!.rootViewController = controller
if let splitViewController = appDelegate.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.leftBarButtonItem?. isEnabled = false
navigationController.topViewController!.navigationItem.leftBarButtonItem?. tintColor = UIColor.clearColor()
}
OR if above does not work, use below code in AppDelegate
navigationController.topViewController!.navigationItem.setLeftBarButton(nil, animated: true)
Alternate to Daljeet's answer we can also use navigationItem.setHidesBackButton on the detailview controller or use this in the AppDelegate.swift's didFinishLaunchingWithOptions :
if let splitViewController = appDelegate.window!.rootViewController as? UISplitViewController {
let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
navigationController.topViewController!.navigationItem.setHidesBackButton(true, animated: true)
}

How to programatically unload a view controller from Tab bar controller?

I have created a tab bar controller in storyboard, with 5 tab bar items. I want to remove one view controller programatically from the "viewcontrollers" array of the tab bar stack. I also want the app to be show some other tab item as selected when i remove the above view controller. I have tried with the below code, but its not working.
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
tabBarController.viewControllers?.remove(at: 2)
tabBarController.selectedIndex = 1
}
Reassign viewControllers property without the one you don't want:
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
tabBarController.selectedIndex = 1
var controllers = tabBarController.viewControllers
controllers.remove(at: 2)
tabBarController.viewControllers = controllers
}
Now this code is ok, but the problem is the following line:
let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController
This creates a new UITabBarController instance - but you want to access the one that was instantiated by the storyboads and that is presented on the screen. However, without more context it's hard to give you suggestions on how to access it. Considering that you call this code from a viewController directly embedded in the tab bar controller, I would start with this:
if let tabBarController = self.tabBarController {
tabBarController.selectedIndex = 1
var controllers = tabBarController.viewControllers
controllers.remove(at: 2)
tabBarController.viewControllers = controllers
}
Try this:
if let tabBarController = self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController {
var viewControllers = tabBarController.viewControllers
viewControllers.remove(at: 2)
tabBarController.viewControllers = viewControllers
tabBarController.selectedIndex = 1
}
if let tabBarController = self.tabBarController {
let indexToRemove = 3
if indexToRemove < tabBarController.viewControllers?.count {
var viewControllers = tabBarController.viewControllers
viewControllers?.remove(at: indexToRemove)
tabBarController.viewControllers = viewControllers
}
}

programmatically set initial view controller to tab controller swift 2

I have a tab controller on my iOS App and I want to check some conditions first and do as follow:
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if condition {
globalClass.token = u
let mvc: MainViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("mainView") as! MainViewController
self.window?.rootViewController = mvc
} else {
globalClass.token = ""
let mvc: LoginViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("loginView") as! LoginViewController
self.window?.rootViewController = mvc
}
self.window?.makeKeyAndVisible()
My problem is that when I programmatically set the initial view controller to first tab of the tab controller when it is loaded, the tab menus at bottom won't load. It just loads the view controller not tab menu.
MainViewController is the first tab of the tab view controller
Thanks,
Afshin
Try this code.
let tbc = mainStoryBoard.instantiateViewControllerWithIdentifier("tabbarStoryboardId") as! UItabBarController
self.window?.rootViewController = tbc
Thanks

How to navigate Inner view controller from App delegate

in my app delegate this is how I navigate to my SWRevealViewController
let revealViewController = mainStoryboard.instantiateViewControllerWithIdentifier("RevealView") as? SWRevealViewController
self.window!.rootViewController = revealViewController
self.window?.makeKeyAndVisible()
I navigate to that inner view controller from the SWRevealViewController's FrontViewcontrollerlike this.
let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("Read") as! ReadViewController
secondViewController.title = self.selectedTitle
self.navigationController?.pushViewController(secondViewController, animated: true)
Now in AppDelegate when receive a push notification I want to navigate this ReadViewController. and when I click the back button it should come back to the FrontViewController just like it happens in normal way. How can I do this in my notification delegates in AppDelegate
Please help me.
Thanks
Try this :
let yourVC = mainStoryboard.instantiateViewControllerWithIdentifier("Read") as! ReadViewController
let frontViewController = mainStoryboard.instantiateViewControllerWithIdentifier("frontViewController") as! FrontViewController
let navController = UINavigationController()
navController.viewControllers = [yourVC,frontViewController]
self.window!.rootViewController = navController
self.window?.makeKeyAndVisible()

navigation controller back button is not hidden

in my app i am using slider so i am using this library.
https://github.com/dekatotoro/SlideMenuControllerSwift
but while using this i am confused in use of navigation bar.
in my appdelegate i write down this code
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller123 : MainViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("MainViewController") as? MainViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
and in my mainviewcontroller i add drwer by this code
slideMenuController()?.addRightBarButtonWithImage(UIImage(named: "ic_menu_black_24dp")!)
because i want drawer on right side
and when i select one item from drawer i write this code
let controller123 : RightViewController = (self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as? RightViewController)!
let controllerright : drawerViewController = self.storyboard!.instantiateViewControllerWithIdentifier("drawerViewController") as! drawerViewController
let controllerleft : RightViewController = self.storyboard!.instantiateViewControllerWithIdentifier("RightViewController") as! RightViewController
let controller = SlideMenuController (mainViewController: controller123, leftMenuViewController: controllerleft, rightMenuViewController: controllerright)
navigationController?.pushViewController(controller, animated: false)
but now issue is that i can not set navigation bar title or even i can not hide back button so how can i solve that?
Try something like this. Add this line in your viewDidLoad() method of drawerViewController
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Your Title"
//If you want to create a custom title View then try this
self.navigationItem.titleView = yourview
Hope this will help you.
try this code "override func viewWillAppear(animated: Bool)" in current view controller :
navigationItem.hidesBackButton = true;
navigationItem.title = "Hello";
hope this code will help you.
Try this in viewDidLoad in each ViewController that navigates from Drawer
self.navigationItem.hidesBackButton = true
self.navigationItem.title = "Title"

Resources