Removing the displayModeButton in UISplitViewController in iOS - 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)
}

Related

UITabBarController - need to show either of view controller on selecting one of the tabs, depending on certain condition

I have a UITabBarController which has 4 tabs. I want to show different view controllers for second tab bar item. Depending on the condition I want to show the view controllers for that tab bar item.
I wrote following code :
UITabbarController -
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if item.tag == 2 {
if UserDefaults.standard.bool(forKey: "FirstTimeUser") == true {
// let vc1 = storyboard?.instantiateViewController(identifier: "CreateNewProjectViewController") as! CreateNewProjectViewController
// self.navigationController?.pushViewController(vc1, animated: true)
let vc1 = self.storyboard?.instantiateViewController(withIdentifier: "CreateNewProjectViewController") as! CreateNewProjectViewController
let window = UIApplication.shared.windows.first
window?.rootViewController = vc1
tabBarController?.tabBar.isHidden = false
}else {
// let vc2 = storyboard?.instantiateViewController(identifier: "ProjectsViewController") as! ProjectsViewController
// self.navigationController?.pushViewController(vc2, animated: true)
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "ProjectsViewController") as! ProjectsViewController
let window = UIApplication.shared.windows.first
window?.rootViewController = vc2
tabBarController?.tabBar.isHidden = false
}
}
}
I tried with navigationController but it is showing blank screen.
If I try setting the view controller with "
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "ProjectsViewController") as! ProjectsViewController
let window = UIApplication.shared.windows.first
window?.rootViewController = vc2
tabBarController?.tabBar.isHidden = false
".
It does not show the tab bar, please help me get it correctly.
Thank you!

Segue to a View Controller from my AppDelegate removes my UI Tab Bar

I am having a problem when performing segue from my AppDelegate. I am using this code to do a segue from appdelagate:
let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)
let viewController: MyViewController = storyboard.instantiateViewController(withIdentifier: "myviewcontroller") as! MyViewController
let rootViewController = self.window!.rootViewController as! UINavigationController
rootViewController.show(viewController, sender: self)
When I use this, my UITabBar is removed. I want to segue to a ViewController that is not a TabBar item and retain my UITabbar. I am also using navigation. How should I approach this?
To show the tab , you need to do the push from 1 of the tab vcs , and it should be embeded inside a navigation and use
let nav = self.window!.rootViewController as! UINavigationController
if let tab = nav.viewControllers.first as? UITabBarController ,
let innerNav = tab.viewControllers.first as? UINavigationController {
innerNav.pushViewController(viewController,animated:true)
}

Tabbar won't hide when pushed into a ViewController inside a UITabBarController

For the purpose of this question, I'm showing a stripped down version of my view hierarchy. My app contains a UITabBarController as the base. Each tab's top most view controller is a navigation controller and it has view controllers embedded in each of them.
Let's take the first tab.
UITabBarController -> UINavigationController -> UITableViewController -> UIViewController
Let's say the UITableViewController instance is some sort of a list and the UIViewController is the detail view. When the user taps on an item from the list, it takes you to the detail view. And when that happens I have set the UIViewController's hidesBottomBarWhenPushed property to true so that the tabbar at the bottom would hide when the user is in the detail view.
My app receives push notifications. When tapped on them, it should open directly into the detail view. I can get it to navigate there. But the issue is the tabbar at the bottom is still visible!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
window = UIWindow(frame: UIScreen.main.bounds)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as! TabBarController
if openingFromPush {
let firstNavigationController = storyboard.instantiateViewController(withIdentifier: "FirstNavigationController") as! UINavigationController
let tableViewController = storyboard.instantiateViewController(withIdentifier: "TableViewController") as! TableViewController
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
viewController.hidesBottomBarWhenPushed = true
firstNavigationController.viewControllers = [tableViewController, viewController]
tabBarController.viewControllers?[0] = firstNavigationController
// tabBarController.tabBar.isHidden = true
window?.rootViewController = tabBarController
} else {
window?.rootViewController = tabBarController
}
window?.makeKeyAndVisible()
return true
}
I set that same hidesBottomBarWhenPushed property to true in the when I instantiate the view controller but that doesn't seem to have any effect. I even tried straight up hiding the tabbar like this tabBarController.tabBar.isHidden = true but that doesn't do anything at all either.
I can't figure how how to resolve this. Any help would be appreciated.
I attached a sample Xcode project here as well if that helps.
You can use this code for pushing detail view controller:
if openingFromPush {
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
viewController.hidesBottomBarWhenPushed = true
if let nvc = tabBarController.viewControllers?[0] as? UINavigationController {
nvc.pushViewController(viewController, animated: false)
}
window?.rootViewController = tabBarController
}
You don't need to init navigation view controller and table view controller again its already inside tab bar controller

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