Hide switching of storyboards - ios

In my application I have 2 storyboards: Onboarding and Main. When user opens app for a first time - Onboarding storyboard is presented. After that, he presses a button and I show him Main Storyboard with such code:
let storyboard = UIStoryboard(name: "Shop", bundle: nil)
let navigationVc = storyboard.instantiateViewController(withIdentifier: "ShopScreens") as UIViewController
navigationVc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(navigationVc, animated: false, completion: nil)
I wanted to make custom animation, when user switches storyboards. To do so - I've created simple view that I'm presenting on top of everything like this:
UIApplication.shared.keyWindow?.addSubview(self.containerViewForAnimatedView)
And this view is working fine, but only in terms of one Storyboard, it covers everything while app changes screens.
But when I try to switch storyboards - this view gets covered by newly presented storyboard.
I also tried presenting view in such way and brining it to front:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.addSubview(self.containerViewForAnimatedView)
But that din't work as well.
How can I hide switching of storyboards by presenting custom created view during that transition?
Would be grateful for any help.

Just played around some with it, no animations or anything special but this will give you the flow that you want:
class ViewController: UIViewController {
let viiew = UIView.init(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.viiew.backgroundColor = .blue
UIApplication.shared.keyWindowInConnectedScenes?.resignKey()
UIApplication.shared.keyWindowInConnectedScenes?.addSubview(self.viiew)
self.viiew.layer.zPosition = 1000
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
let vc = ViewController2()
UIApplication.shared.keyWindowInConnectedScenes?.rootViewController = vc
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
self.viiew.removeFromSuperview()
}
}
}
}
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
}
extension UIApplication {
var keyWindowInConnectedScenes: UIWindow? {
return windows.first(where: { $0.isKeyWindow })
}
}

Related

SWIFT: If I implement the UITAB bar programmatically, is it possible to edit the different tabItems inside the storyBoard

I have implemented a tabBar programmatically:
class ViewController: UIViewController {
let tabBarCnt = UITabBarController()
override func viewDidLoad() {
super.viewDidLoad()
tabBarCnt.tabBar.tintColor = UIColor.black
createTabBarController()
}
func createTabBarController() {
let firstVc = UIViewController()
let downloadViewController = DownloadsViewController()
downloadViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .downloads, tag: 0)
let bookmarkViewController = BookmarksViewController()
bookmarkViewController.tabBarItem = UITabBarItem(tabBarSystemItem: .bookmarks, tag: 1)
let favoritesViewControllers = FavoritesViewController()
favoritesViewControllers.tabBarItem = UITabBarItem(tabBarSystemItem: .favorites, tag: 2)
// Adding navigationControllers
let controllerArray = [downloadViewController, bookmarkViewController, favoritesViewControllers]
// For somereason this made the word Home appear in first tab
tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: $0)}
self.view.addSubview(tabBarCnt.view)
}
}
DownloadViewController.swift
class DownloadsViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.green
self.title = "Downloads"
}
}
This is the result:
MY QUESTION IS:
Do I have to implement all the layouts of the different viewControllers downloadViewController, bookmarkViewController, favoritesViewControllers programmatically?
Can't I use the storyboard and associate a viewController and do all the UI design and implementation there like this:
The problem here is that if I will implement all the layout programmatically, it won't be an obvious or a practical thing to do.
And assigning DownloadsViewController to that storyboard ViewController, doesn't make it get display it when I navigate to DownloadsTab in the UITabBar.
Yes, you can do that currently your creating view controller instance programmatically, instead of that you have to load/get viewController instance from storyBoard like this.
UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(identifier: "downloadsVC") as! DownloadsViewController
before calling this method set viewController StoryboardID

Side bar menu icon is invisible in Tab bar based view controller

I've implemented side bar using SWRevealViewController. But when i do same using Tab bar based view controller, menu icon is invisible only in tab based screens.
Icon is visible in interface builder but invisible in simulator/device.
Functionality is working in every screen, even in tab based view controller.
When i drag from left to right and vice versa, functionality is working fine.
Only the issue is menu icon is invisible in simulator.
Can anyone help me?
Thanks in advance.
below is the swift file for tab
import UIKit
class ItemOneViewController: UIViewController {
#IBOutlet weak var menuBar: UIBarButtonItem!
override func viewDidLoad() {
super.viewDidLoad()
setMenuBarBtn(menuBar: menuBar)
navigationController!.navigationBar.barTintColor = UIColor.color(.blue)
}
func setMenuBarBtn(menuBar: UIBarButtonItem) {
menuBar.target = revealViewController()
menuBar.action = #selector(SWRevealViewController.revealToggle(_:))
view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.portrait
}
}
Code is similar for three tabs.
I've done below tasks before asking question here
Deleted and recreated screens two times.
Cross checked everything and compared tab bar based scenes source code with other scenes.
googled it.
Below are the images for your reference
Tab based screen with side bar functionality (working good)
Tab based screen without menu icon (Problem persist here , i want menu icon to be displayed)
HomeScreen with menu icon (Working good)
Navigation Controller attribute inspector
my storyboard
Item One View Controller view hierarchy
Item One view Controller related navigation controller scene
You gave to add normal button on HomeViewController
Then directly perform segue through storyboard(from burger menu button).
#IBAction func sideMenuBtnAction(_ sender: UIButton) {
performSegue(withIdentifier: "sideMenu", sender: self)
}
Then on SideMenuController :
import UIKit
import SideMenu
class SideMenuViewController: UIViewController {
#IBOutlet var sideMenuTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
SideMenuManager.default.menuPresentMode = .menuSlideIn
SideMenuManager.default.menuFadeStatusBar = false
SideMenuManager.default.menuWidth = self.view.frame.width / 1.4
SideMenuManager.default.menuShadowOpacity = 0.3
sideMenuTableView.tableFooterView = UIView(frame: .zero)
}
}
You need to create a tabbarController class like below, and assign this ViewController class to tabbar on storyboard ...
I have not tested this code but something like this should work for your case
class ViewController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let firstSw = setRootViewControllerFor(identifier: "firstViewController") as! SWRevealViewController
let secondsw = setRootViewControllerFor(identifier: "secondViewController") as! SWRevealViewController
let thirdSw = setRootViewControllerFor(identifier: "thirdController") as! SWRevealViewController
self.viewControllers = [firstSw,secondsw, thirdSw]
// Do any additional setup after loading the view.
}
func setRootViewControllerFor(identifier:String)->SWRevealViewController
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewController(withIdentifier: "sw") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: identifier)
let navigationController = UINavigationController(rootViewController: destinationController!)
navigationController.navigationBar.isHidden=false
navigationController.setNavigationTints() //call your UI method to set
sw.setFront(navigationController, animated: true)
return sw
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
}
OR Following by doing this there is no need to create a tabbarController
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// create UIWindow with the same size as main screen
window = UIWindow(frame: UIScreen.mainScreen().bounds)
// create story board. Default story board will be named as Main.storyboard in your project.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create view controllers from storyboard
// Make sure you set Storyboard ID for both the viewcontrollers in
// Interface Builder -> Identitiy Inspector -> Storyboard ID
let firstSw = setRootViewControllerFor(identifier: "firstViewController") as! SWRevealViewController
let secondsw = setRootViewControllerFor(identifier: "secondViewController") as! SWRevealViewController
let thirdSw = setRootViewControllerFor(identifier: "thirdController") as! SWRevealViewController
// Set up the Tab Bar Controller to have two tabs
let tabBarController = UITabBarController()
tabBarController.viewControllers = [firstSw,secondsw, thirdSw]
// Make the Tab Bar Controller the root view controller
window?.rootViewController = tabBarController
window?.makeKeyAndVisible()
return true
}
func setRootViewControllerFor(identifier:String)->SWRevealViewController
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewController(withIdentifier: "sw") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewController(withIdentifier: identifier)
let navigationController = UINavigationController(rootViewController: destinationController!)
navigationController.navigationBar.isHidden=false
navigationController.setNavigationTints() //call your UI method to set
sw.setFront(navigationController, animated: true)
return sw
}

How to add a navigationBar and show another controller by code in swift

Now I have two Controller, one is FirstViewController,and one is SecondViewController.
In FirstViewController
var secondViewController: SecondViewController!
let textView: UITextView = {
let textView = UITextView()
//textView.font = UIFont.systemFont(ofSize: 20)
textView.isEditable = true
textView.keyboardDismissMode = .interactive
textView.allowsEditingTextAttributes = true
return textView
}()
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = SecondViewController()
secondViewController.firstViewController = self
setUpLayout()
addKeyboardObserver()
// Do any additional setup after loading the view, typically from a nib.
}
then I add a button to show secondViewController's view as a custom keyboard
#objc func showSecondView() {
textView.resignFirstResponder()
textView.inputView = secondViewController.view
textView.becomeFirstResponder()
}
then in SecondViewController, as the pic show, now I wanna add a navigation bar in SecondViewController, how?(I can do it using storyboard, but I can't do it using code).
and I wanna show the ThirdViewController when the tableView in SecondViewController(the red area) is selected, and the ThirdViewController's view will show in the red area, not the whole screen,(just like the storyboard's "show" connection) how can I do that?
You have to take navigation controller from root view controller so you can hide and show whenever you want, Another thing you can do is, take navigation view controller for particular view controller. But you can not navigate (pushing) any navigation view controller. You can only present it. Check my code below
import UIKit
class ViewControllerWithoutNavigation: UIViewController { // This is without navigation
override func viewDidLoad() {
super.viewDidLoad()
}
func gotoNextViewController(){
let nvc = UINavigationController(rootViewController: ViewControllerWithNavigation())
let transition = CATransition()
transition.duration = 0.25
transition.type = kCATransitionPush
transition.subtype = kCATransitionFromRight
self.view.window!.layer.add(transition, forKey: kCATransition)
//transition is to show like pushing, Without it it will look like presenting from bottom
self.present(nvc, animated: false, completion: nil)
}
}
class ViewControllerWithNavigation: UIViewController { // This is with navigation
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
}
}
Adding NavigationController
Select the viewcontroller in storyboard and goto Editor -> Embedd In -> Navigation Controller
To show another view controller
*Add storyboard id to Navigation Controller
*Then add this code in action
let storyBoard = UIStoryboard(name: "storyboard_name", bundle: nil)
let destination = storyBoard.instantiateViewController(withIdentifier: "storyboard_id")
self.navigationController?.pushViewController(destination, animated: true)

How to add a navigation controller programmatically in code but not as initial view controller

I am relatively new to swift and iOS and can't find an answer or any help for my problem that works.
I want to create a navigation controller view when I click on a button in my previous view. My previous view is an on-boarding with a button on the last page. I successfully connected the button to my next view but I am not able to make the view act like a navigation controller. For example, when a navigation bar is displayed, I am not able to add navigation items to it.
Is there a way to set the new view I create by clicking my button in the first view to be the root controller for my navigation view?
A short version of my code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button = UIButton(frame: CGRect(x: view.frame.width / 2, y: view.frame.height / 2, width: 40, height: 40))
button.backgroundColor = .red
button.tintColor = .white
button.setTitle("Test", for: .normal)
button.addTarget(self, action: #selector(change), for: .touchUpInside)
view.addSubview(button)
}
func change () {
let otherView = SecondViewController()
self.present(otherView, animated: true, completion: nil)
}
}
class SecondViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .yellow
}
}
The SecondViewController gets displayed with a navigation bar but I had problems implementing navigation items, because they weren't shown.
Does the second view controller I present have to be of type UIViewController or UINavigationController?
I am aware that there are easier ways to achieve my goal with the use of the storyboard but in my opinion I understand it better by making my own references via code instead of creating them by dragging them out of the storyboard.
Edit: I have no Objective-C background and learning Swift for about 4 weeks now.
You can add UINavigationController like below :
First you have to create object of SecondViewController,
let myViewController: SecondViewController? = storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
Or
let myViewController: SecondViewController? = SecondViewController(nibName: "SecondViewController", bundle: nil)
Or
let myViewController: SecondViewController? = SecondViewController()
Then add Navigation to SecondViewController
let myNavigationController = UINavigationController(rootViewController: myViewController!)
If you want to present then use :
self.present(myNavigationController, animated: true) {
}
If you want to push then use :
self.navigationController?.pushViewController(myNavigationController, animated: true)
If you want to set as root controller then use :
let appDelegate: AppDelegate = (UIApplication.shared.delegate as? AppDelegate)!
appDelegate.window?.rootViewController = myNavigationController
var objVC: UIViewController? = storyboard.instantiateViewController(withIdentifier: "ViewController")
var aObjNavi = UINavigationController(rootViewController: objVC)
Now, instead of using view controller object use navigation controller object.

UIPageViewController + UIImagePicker with custom controls not working - view is not in window hierarchy

I'm trying to create effect similar to Snapchat in Swift - swiping between UIImagePicker with custom controls and other VCs.
The problem is:
when CameraVC is presented for the first time background is black and swipe between VCs works only on controls (on empty space where should be image from camera it isn't) and warning shows up "Attempt to present UIImagePickerController on CameraVC whose view is not in the window hierarchy!"
when I swipe to another VC and then back to CameraVC UIImagePicker is presented properly and everything works great instead of swiping between VCs which is not working at all. There's also no "window hierarchy" warning
So I think the reason why it's not working is that UIImagePicker is presenting over PageViewController not "in" it, but I have no idea how to fix this.
I'm presenting PageViewController like this:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vcPageView = storyboard.instantiateViewControllerWithIdentifier("PageViewID") as! UIViewController
self.presentViewController(vcPageView, animated: false, completion: nil)
}
Loading VCs to table in PageViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
self.dataSource = self
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vc0 = storyboard.instantiateViewControllerWithIdentifier("CameraID") as! UIViewController
var vc1 = storyboard.instantiateViewControllerWithIdentifier("vc2ID") as! UIViewController
self.myViewControllers = [vc0, vc1]
self.setViewControllers([myViewControllers[0]], direction: UIPageViewControllerNavigationDirection.Forward, animated: false, completion: nil)
}
And finally CameraVC:
#IBOutlet var cameraOverlay: UIView!
var camera = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
self.camera.delegate = self
self.camera.sourceType = UIImagePickerControllerSourceType.Camera;
self.camera.mediaTypes = [kUTTypeImage]
self.camera.allowsEditing = false
self.camera.showsCameraControls = false
self.cameraOverlay.frame = self.camera.cameraOverlayView!.frame
self.cameraOverlay.bringSubviewToFront(self.cameraOverlay)
self.camera.cameraOverlayView = self.cameraOverlay
self.cameraOverlay = nil
}
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.topMostViewController().presentViewController(self.camera, animated: false, completion: nil)
}
topMostViewController code:
extension UIViewController {
func topMostViewController() -> UIViewController {
// Handling Modal views
if let presentedViewController = self.presentedViewController {
return presentedViewController.topMostViewController()
}
// Handling UIViewController's added as subviews to some other views.
else {
for view in self.view.subviews
{
// Key property which most of us are unaware of / rarely use.
if let subViewController = view.nextResponder() {
if subViewController is UIViewController {
let viewController = subViewController as! UIViewController
return viewController.topMostViewController()
}
}
}
return self
}
}
Try this, In the CameraVC move the camera code in the viewDidLoad() to the viewDidAppear()

Resources