How to add a default ViewController along with the navigation drawer ViewController - ios

I have implemented a menuViewVontroller for navigation drawer that works fine. I am able to render the view controllers on their respective menu buttons. I have one ViewController that loads up first and then I display other view controllers from the navigation menu i.e. Home menu displays the homeViewVontroller etc. as you can see in the screen shots.
But I also want my app to show the homeControllerView when the viewController loads. It is currently rendering an empty screen as the view controller is empty. When I write the below code in viewDidLoad() method, I am not getting the navigation drawer, just the homeViewController gets displayed (as shown in the picture below).
let view = UIView()
let controller = storyboard!.instantiateViewController(withIdentifier: "SliderController")
addChild(controller)
view.frame = self.view.bounds
self.view.addSubview(controller.view)
self.topView = view
What can I do to render both the homeViewController and the menuViewController in the same screen.
EDIT: My code when the user presses the menu item
#IBAction func didTapMenuButton(_ sender: UIBarButtonItem) {
guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "MenuViewController") as? MenuViewController else { return }
menuViewController.didTapMenuType = {
menuType in
self.transitionToNew(menuType)
}
menuViewController.modalPresentationStyle = .overCurrentContext
menuViewController.transitioningDelegate = self
present(menuViewController, animated: true)
}
func transitionToNew(_ menuType: MenuType) {
let title = String(describing: menuType).capitalized
self.title = title
topView?.removeFromSuperview()
switch menuType {
case .promotion:
let view = UIView()
view.backgroundColor = .yellow
view.frame = self.view.bounds
self.view.addSubview(view)
self.topView = view
case .billPayment:
let view = UIView()
view.backgroundColor = .blue
view.frame = self.view.bounds
self.view.addSubview(view)
self.topView = view
default:
break
}
}

Related

Get rid of shadow form popover view controller (Swift)

I am trying to create number pad in iPad using popover view controller. Everything is achieved but there is a shadow beneath the pop view. I tried to remove that shadow but nothing worked for me. Here is my code which presents pop over view in my view controller.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PopOverVC") as? PopOverVC
vc?.modalPresentationStyle = .popover
vc?.preferredContentSize = CGSize(width: 280, height: 425)
vc?.delegate = self
if let popoverPresentationController = vc?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = [.down, .up, .right, .left]
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = txtNumber.frame
popoverPresentationController.delegate = self
if let popoverController = vc {
present(popoverController, animated: false, completion: nil)
}
}
Can anybody help me removing the shadow? Thanks in advance!!

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)

UIViewController's view background color is not changed

I have a UIViewController that only shows a UIView at the bottom with a fixed height:
fileprivate lazy var box: UIView = {
let box = UIView()
box.backgroundColor = UIColor.blue
return box
}()
And I'd like to set a clear color to the rest of the view (self.view). I'm presenting the view controller from a navigation controller. I'm trying this:
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
view.isOpaque = false
configView()
configConstraints()
}
The subview is displayed in blue but the rest of the view is opaque white, I need it to be transparent to see the previous viewController's view.
What can I be missing?
To have transparent Model view controller background you have to set UIViewController providesPresentationContextTransitionStyle and definesPresentationContext properties while presenting the view controller.
Example
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "YOUR_VIEW_CONTROLLER_ID") {
vc.providesPresentationContextTransitionStyle = true
vc.definesPresentationContext = true
vc.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
self.present(vc, animated: true, completion: nil)
}

Multiple child viewControllers in single container view

I am newbie with Swift and I have to manage multiple child view controllers in one UIContainerView with storyboard layout but, container view only provide one child view at once.
I have already used below code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewControllerWithIdentifier("HomeVC") as UIViewController
self.containerViewA.addSubview(controller.view)
self.addChildViewController(controller)
In ios at a container view only connect with one child controller "at a time" , instead of child view controller we can take one UINavigationController with its RootviewControlle, We can load multiple child view controller using multiple UINavigationController as mentioned below.
var navController1 = UINavigationController()
var navController2 = UINavigationController()
var navController3 = UINavigationController()
var navController4 = UINavigationController()
override func viewDidLoad()
{
super.viewDidLoad()
//navController1
let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("HomeVC") as! HomeVC
navController1 = UINavigationController(rootViewController: VC1)
navController1.navigationBarHidden = true
//navController2
let VC2 = self.storyboard!.instantiateViewControllerWithIdentifier("RoomControlesVC") as! RoomControlesVC
navController2 = UINavigationController(rootViewController: VC2)
navController2.navigationBarHidden = true
//navController3
let VC3 = self.storyboard!.instantiateViewControllerWithIdentifier("TelevisionVC") as! TelevisionVC
navController3 = UINavigationController(rootViewController: VC3)
navController3.navigationBarHidden = true
//navController4
let VC4 = self.storyboard!.instantiateViewControllerWithIdentifier("EntertainmentVC") as! EntertainmentVC
navController4 = UINavigationController(rootViewController: VC4)
navController4.navigationBarHidden = true
let btn = UIButton()
btn.tag = 1
self.ActionOnSideMenuButtons(btn)
}
#IBAction func ActionOnSideMenuButtons(sender: AnyObject)
{
for view in containerViewA.subviews{
view.removeFromSuperview()
}
switch sender.tag
{
case 1:// Home
NSLog("Home Button tapped")
addChildViewController(self.navController1)
self.navController1.view.frame = self.containerViewA.bounds
self.containerViewA.addSubview(navController1.view)
break
case 2:// Room Controles
NSLog(" Room Controles Button tapped")
self.navController2.view.frame = self.containerViewA.bounds
self.containerViewA.addSubview(navController2.view)
break
case 3:// Television
NSLog("Television Button tapped")
self.navController3.view.frame = self.containerViewA.bounds
self.containerViewA.addSubview(navController3.view)
break
case 4:// Entertainment
NSLog("Entertainment Button tapped")
self.navController4.view.frame = self.containerViewA.bounds
self.containerViewA.addSubview(navController4.view)
break
default:
break
}
}
In above code i have created 4 navigation controllers and initialise them in viewDidLoad method & on "ActionOnSideMenuButtons" method i have load childview controller whenever any of side menu button tapped.

Can't push because self.navigationController is nil

I'm presenting a Navigation Controller as a popover. First time a do it I can push to another View Controller with any issue. But, second time a present another controller the same way I can't perform a push because the self.navigationController of the presented Controller is nil. This is the piece of code I'm using to present the Controller
func instantiateEditController(view : UIView) -> UINavigationController
{
let popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("Edit Controller") as MCMEditController
popoverContent.preferredContentSize = CGSizeMake(320, 480)
let navController = MCMBaseNavigationController(rootViewController: popoverContent)
navController.modalPresentationStyle = UIModalPresentationStyle.Popover
navController.navigationBar.tintColor = UIColor.whiteColor()
let popover = navController.popoverPresentationController
popover?.sourceView = view
popover?.sourceRect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
return navController
}
Note: The Navigation Controller is always presented, but just the first time I perform pushes. And this code is for be used in iPad.
Because of the nature of Storyboards and not being able to show that here, I'm not sure where your issue is exactly.
However I successfully setup a project that works using segues. I changed your code to the following:
func instantiateEditController(view : UIView) -> UINavigationController
{
if let popoverContent = self.storyboard?.instantiateViewControllerWithIdentifier("Edit Controller") as? MCMEditController {
popoverContent.preferredContentSize = CGSizeMake(320, 480)
let navController = UINavigationController(rootViewController: popoverContent)
navController.modalPresentationStyle = UIModalPresentationStyle.Popover
navController.navigationBar.tintColor = UIColor.whiteColor()
let popover = navController.popoverPresentationController
popover?.sourceView = view
popover?.sourceRect = CGRectMake(0, 0, view.frame.size.width, view.frame.size.height)
return navController
} else {
return UINavigationController(rootViewController: self) // not recommend to keep this, I'm on Swift 1.2 and this was an easy fix to resolve the errors
}
}
To present this popover I set up this IBAction in my ViewController (sender in my case was a UIButton)
#IBAction func presentPopover(sender: AnyObject) {
if let view = sender as? UIView {
let controller = instantiateEditController(view)
let popover = UIPopoverController(contentViewController: controller)
popover.presentPopoverFromRect(view.frame, inView: self.view, permittedArrowDirections:UIPopoverArrowDirection.Any, animated: true)
}
}
The last thing that could be the issue is make sure your segues are using the segue type "show" for all the segues to make sure the transition between them use the navigation controller push:
Hope this helps!

Resources