Popover View is Blank - ios

I am trying to add a popover to my controller using code. For some reason, the popover appears, but there is no content inside.
The code I'm using for the transition is:
#IBAction func presentPopover(_ sender: UIButton) {
//performSegueWithIdentifier("Popover", sender: self)
let vc = PopoverViewController()
vc.modalPresentationStyle = .popover
let popover = vc.popoverPresentationController!
popover.delegate = self
popover.permittedArrowDirections = .right
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
...
present(vc, animated: true, completion: nil)
}
The popover view is made in the storyboard, and is of class PopoverViewController After doing some testing it says that the PopoverViewController's viewDidAppear is triggered.

as rmaddy mention The line let vc = PopoverViewController() does not use the storyboard. so you need to do it like that
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PopoverViewController") as? PopoverViewController {
vc.modalPresentationStyle = .popover
let popover = vc.popoverPresentationController!
popover.delegate = self
popover.permittedArrowDirections = .right
vc.popoverPresentationController?.sourceView = sender
vc.popoverPresentationController?.sourceRect = sender.bounds
self.present(vc, animated: true, completion: nil)
}

Related

Present VC programmatically

How to present UIViewController() inside of UINavigationController ? My controller presenting not fullscreen. I want my app to look like this enter image description here, but it is end up like this enter image description here
I making app without Storyboard(Programmatically)
let customVC = UIViewController()
customVC.view.backgroundColor = .green
customVC.modalPresentationStyle = .fullScreen
let navVC = UINavigationController(rootViewController: customVC)
self.present(navVC, animated: true, completion: nil)
let customVC = DestinationController()
let navVC = UINavigationController(rootViewController: customVC)
// this line overFullScreen
navVC.modalPresentationStyle = .overFullScreen
// for more style
navVC.modalTransitionStyle = .crossDissolve
self.present(navVC, animated: true, completion: nil)
your class where you want to go or present
class DestinationController:UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
}

How can I switch programmatically between view controllers that are not on the same storyboard?

I am trying to switch between a view controller (that is coded programmatically) to a ViewController with the UI Elements on the storyboard.
I am trying to switch between a HomeController to a TabBarController using a button but by pressing the button it switches to a dark screen. I would be glad if some of you could help me out.
Here is my code:
var welcomeLabel: UIButton = {
let label = UIButton()
label.tintColor = .white
label.translatesAutoresizingMaskIntoConstraints = false
label.alpha = 0
label.addTarget(self, action: #selector(handleLogin), for: .touchUpInside)
return label
}()
...............
#objc func handleLogin() {
navigationController?.show(TabBarController(), sender: Any?.self)
}
use this code if you're pushing view from NavigationController
navigationController?.pushViewController("YourViewController", animated: true)
You can do it in following way
let storyboard = UIStoryboard(name: "Your_storyboard", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "Your_View_controller") as! Your_View_controller
Now to present
self.present(viewController, animated: true, completion: nil)
Or through navigation controller
navigationController?.pushViewController(viewController, animated: true)
You can try this -
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) // Give the name .e.g if you have the different story board name
let YourNextViewController = storyBoard.instantiateViewController(withIdentifier: "your story board identifier") as! StoryBoardName/ClassName
self.navigationController?.pushViewController(YourNextViewController, animated: true)
//Or if you want to present viewController
self.present(YourNextViewController, animated:true, completion:nil)

Add navigationController to specif viewController programmatically without storyboard swift

I want to add navigationController to just only for HomeViewController for example. I know how to do it from AppDelegate and like this below
let navBar = UINavigationController(rootViewController: homeViewController())
self.present(navBar, animated: true, completion: nil)
Is there another way that I can add navigationController inside viewDidLoad and viewWillAppear?
Edited:
My logic is when I pressed Login button which is the code below. Then it will present SWRevealViewController
#IBAction func loginPressed(_ sender: Any) {
let frontViewController = HomeViewController()
let rearViewController = TableViewController()
let swRevealVC = SWRevealViewController(rearViewController: rearViewController, frontViewController: frontViewController)
swRevealVC?.toggleAnimationType = SWRevealToggleAnimationType.easeOut
swRevealVC?.toggleAnimationDuration = 0.30
self.present(swRevealVC!, animated: true, completion: nil)
}
I just only want to set navigationController to HomeViewController
Replace
let frontViewController = HomeViewController()
with
let frontViewController = UINavigationController(rootViewController: HomeViewController())
and it will work.
Look below code hope it works for you...
This will be in App delegate
if UserDefaults.standard.bool(forKey: REMEMBER_ME) {
let menuVC = UINavigationController(rootViewController: SideMenuViewController())
let loginVC = UINavigationController(rootViewController: DashboardViewController())
let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
self.navigationController?.navigationBar.isHidden = true
window?.rootViewController = revealViewController
} else {
window?.rootViewController = LoginViewController()
}
This will be in your login action
if let window = UIApplication.shared.keyWindow {
let menuVC = UINavigationController(rootViewController: SideMenuViewController())
let loginVC = UINavigationController(rootViewController: DashboardViewController())
let revealViewController = SWRevealViewController(rearViewController: menuVC, frontViewController: loginVC)
self.navigationController?.navigationBar.isHidden = true
window.rootViewController = revealViewController
}

PopoverPresentationController coming as nil

Created a SingleViewApplication in that I have placed a button.
Now clicking on button I need to display a tableView as popover.
The TableViewController is created in xib.
The issue is tableViewController.popoverPresentationController always comes as nil see below code
let filterVC = TableViewController(nibName: "TableViewController", bundle: nil)
var filterDistanceViewController = UINavigationController(rootViewController: filterVC)
filterDistanceViewController.preferredContentSize = CGSize(width: 300, height: 200)
let popoverPresentationViewController = filterDistanceViewController.popoverPresentationController
popoverPresentationViewController?.permittedArrowDirections = .any
if let pop = filterDistanceViewController.popoverPresentationController {
pop.delegate = self
}
in above code
filterDistanceViewController.popoverPresentationController is always coming as nil
Any hint in right direction will be highly appreciated.
Until you've set a modalPresentationStyle on your VC, the popoverPresentationController property will be nil. Ensure that you set the modalPresentationStyle before accessing.
You are not presenting anything, so you need to present the popoverPresentationViewController on the current viewcontroller, for example:
#IBAction func importantButtonPressed(_ sender: UIButton) {
let tableViewController = UITableViewController()
tableViewController.modalPresentationStyle = .popover
present(tableViewController, animated: true, completion: nil)
if let pop = tableViewController.popoverPresentationController {
pop.delegate = self
}
}
You may do like below.
#IBAction func popoverBtnPressed(_ sender: Any) {
let vc2 = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController2")
vc2.modalPresentationStyle = .popover
vc2.popoverPresentationController?.delegate = self
vc2.popoverPresentationController?.barButtonItem = popoverBtn
vc2.popoverPresentationController?.sourceRect = .zero
present(vc2, animated: true, completion: nil)
}

How dismiss modal view form sheet controller on outside tap?

How dismiss modal view form sheet controller on outside tap?
#IBAction func btnAction(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("popoverID") as! PopoverViewController
vc.modalPresentationStyle = .FormSheet
vc.popoverPresentationController?.sourceView = self.view
self.presentViewController(vc, animated: true, completion: nil)
}

Resources