UIViewController. How to present a small view to always in the top - ios

How to present the View or ViewController like this form.
I have a VideoChatViewController that displays streaming video. I read about picture in picture, but this is not the most suitable option for me.
I create View Controller and i tried present MainTabBarController like this:
let tabBarVC = storyboard?.instantiateViewController(withIdentifier: "MainTabBarController") as! MainTabBarController
self.view.addSubview(tabBarVC.view)
// And after present Mini View
let vc = UIViewController()
vc.view.frame = CGRect(x: self.view.frame.midX,
y: self.view.frame.midY,
width: 200,
height: 200)
vc.view.backgroundColor = .red
vc.modalPresentationStyle = .overCurrentContext
self.view.addSubview(vc.view)
and i have
I know this is the wrong way. But how do I do it best?

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!!

PUSH to ViewController from other viewcontroller which is presented as popover - WITHOUT STORY BOARD

I am NOT USING StoryBoard in my project. I have presented one view controller as popOver Style.
let vcOne = ViewControllerOne()
vcOne.showCorrectionFactor = true
vcOne.modalPresentationStyle = .popover
let pop = vcOne.popoverPresentationController
pop?.sourceView = self.view
pop?.sourceRect = sender.frame
vcOne.preferredContentSize = CGSize(width: 400, height: 500)
self.present(vcOne, animated: true, completion: nil)
Now i want to navigate to other view controller from ViewControllerOne() and with in this pop up.
I am doing it like
let vc = PairViewController()
self.navigationController?.pushViewController(vc, animated: true)
But self.navigationController? is getting nil value. How can i achieve this without story board.
You did not embed ViewControllerOne in a UINavigationController, so of course self.navigationController is nil.
Just do this:
// embed it!
let navController = UINavigationController(rootViewController: vcOne)
// get the popover presentation controller from the navigation controller!
let pop = navController.popoverPresentationController
pop?.sourceView = self.view
pop?.sourceRect = sender.frame
vcOne.preferredContentSize = CGSize(width: 400, height: 500)
// present navController rather than vcOne!
self.present(navController, animated: true, completion: nil)

How to make a window in Xcode

I know this is going to sound confusing but I am making a to-do app for iOS, what I want to do is when a user taps on the add button, instead of user going to another view controller to enter in the new tasks information, I want a little window to pop up in that same view controller where the user can then enter the information. Is their a way to do this?
If i am not wrong you are talking about modal presentation where a view would appear over another view as a popup. This is how it can be done:
let vc = UIStoryboard.storyboard(storyboard: .Vote).instantiateViewController(YourController.self)
under YourController use controller of the popup view
vc.modalPresentationStyle = .popover
let nav = UINavigationController(rootViewController: vc)
nav.modalPresentationStyle = UIModalPresentationStyle.popover
let popover = nav.popoverPresentationController
vc.preferredContentSize = CGSize(width: 280,height: 300) //use own height and width
vc.navigationController?.isNavigationBarHidden = true
popover!.delegate = self
popover!.sourceView = self.view
popover!.sourceRect = CGRect(x: self.view.bounds.midX, y: self.view.bounds.midY - 20,width: 0,height: 0)
popover!.permittedArrowDirections = UIPopoverArrowDirection(rawValue: 0)
self.present(nav, animated: true, completion: nil)
this will make the view appear as a popup in your root view

programmatic navigation back button not showing

here is my code at the appdelegate:
func showMainView()
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "PRODUCT_TABBAR_VC_ID")
let nav = UINavigationController()
nav.pushViewController(secondViewController, animated: true)
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()
}
simulator output:
basicaly i want when user press back button then came back main page . i mean initial page .
i am trying with this one but it not working at least one answer for me Navigate Back to previous view controller
Note : #matt said it is imposible. so could you tell me please what should i do . i am new to iOS
Update:
when user select Man that time tabviewcontroller two page only showing list of product about Man. so if user want to see Woman then user back to main page to select Woman then he will see Woman tabviewcontroller two page.
The back button is for returning to an earlier view controller pushed onto a UINavigationController. In your code, there is no back button because there is nothing to come back to; secondViewController is the only view controller pushed onto the UINavigationController.
You should create your hierarchy like so:
----> UIViewController
----> UINavigationController ----> UIViewController ----> UITabBarController
----> UIViewController
EDIT: It will look like that:
This way it will be possible to pop UIViewControllers contained in UITabBarController.
Place following lines within didFinishLaunchingWithOptions in AppDelegate.swift
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav = UINavigationController(rootViewController: FirstViewController())
self.window!.rootViewController = nav
self.window!.makeKeyAndVisible()
Create FirstViewController.swift with following:
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let button1 = UIButton(frame: CGRect(x: 0, y: 0, width: 200, height: 200))
button1.setTitle("Man", for: .normal)
button1.tag = 1
self.view.addSubview(button1)
button1.addTarget(self, action: #selector(showAction(sender:)), for: .touchUpInside)
let button2 = UIButton(frame: CGRect(x: 0, y: 250, width: 200, height: 200))
button2.setTitle("Woman", for: .normal)
button2.tag = 2
self.view.addSubview(button2)
button2.addTarget(self, action: #selector(showAction(sender:)), for: .touchUpInside)
}
func showAction(sender: UIButton) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let secondViewController = storyBoard.instantiateViewController(withIdentifier: "PRODUCT_TABBAR_VC_ID")
if (sender.tag == 1) {
// SHOW MAN
} else if (sender.tag == 2) {
// SHOW WOMAN
}
self.navigationController?.pushViewController(secondViewController, animated: true)
}
}

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