how to add mathwidget as a subview inside another UIViewController
Currently, mathwidget is working fine when loading UIViewController.
let subViewEE = MathWidgetClassName()
self.present(subViewEE, animated: true, completion: nil)
But when I am trying to add it as a subview inside present view controller nothing shows up, here is the code:
let mathWidget= MathWidgetClassName()
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
Can anyone help to display MathWidget as a subview in present UIViewController?
you are creating viewcontroller programmatically then you need to set frame and background color of it like,
let mathWidget = MathWidgetClassName()
mathWidget.view.bounds = self.view.bounds
mathWidget.view.backgroundColor = UIColor.green // you should set white here , it is for demonstration
self.addChildViewController(mathWidget)
self.view.addSubview(mathWidget.view)
mathWidget.didMove(toParentViewController: self)
If you have view controller in storyboard then you should do like,
let mathWidget = self.storyboard?.instantiateViewController(withIdentifier: "storyBoardID") //storyBoardID is Storyboard id - can be set from identity inspector of storyboard
// mathWidget?.view.bounds = self.view.bounds
// mathWidget?.view.backgroundColor = UIColor.green
self.addChildViewController(mathWidget!)
self.view.addSubview((mathWidget?.view)!)
mathWidget?.didMove(toParentViewController: self)
Related
I have created a UIViewControlller and designed in storyboard, I want to show it as popup for my another view.
I used the following to show as popup:
guard let popupVC = UIStoryboard(name: "More", bundle: nil).instantiateViewController(withIdentifier: "LanguageSelectionViewController") as? LanguageSelectionViewController else{
fatalError("Unexpected destination VC")
}
self.addChildViewController(popupVC)
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.tableView.frame)")
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.view.frame)")
popupVC.modalPresentationStyle = .currentContext
popupVC.modalTransitionStyle = .crossDissolve
popupVC.view.bindFrameToSuperviewBounds()
self.view.addSubview(popupVC.view)
popupVC.didMove(toParentViewController: self)
It is working properly, but on my device half of the screen space is not filled by popup view.
Have you tried using the present() function? Something like this:
let popupVC = PopupViewController()
popupVC.modalPresentationStyle = .overFullScreen
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)
I find designing view controllers in their own xib files easier, and it allows initialisation like in my example, rather than having to use the storyboard in code, but thats personal preference.
If it still isnt working, maybe check that the layout constraints for the popup's background are correct, so it actually fills it's parent.
You need to swap these two lines:
self.view.addSubview(popupVC.view)
popupVC.view.bindFrameToSuperviewBounds()
You can't bind a view to it's superview if the view hasn't been added to it yet.
First thing you need click on popup(storyboard or xib) and set it to background color .clear Color
Then make it to center. like
popupVC.center = self.view.center
it's just a guide you can change and set names accordingly. But my question why you are using storyboard why not have created Xib or View
Hope it will serve your purpose.
I implemented the share extension and I want animate my View Controller with a crossDissolve, so i set the modalPresentationStyle = .overFullScreen and modalTransitionStyle = crossDissolve but it seems not working. The VC still appear from the bottom to the top and with the new iOS 13 modal style (not completly full screen).
Anyone know how to solve it? It tried both with and without storyboard.
NB: I'm not talking about a normal VC presentation, but the presentation of the share extension, it means that it's another app that present my VC.
One way to do it would be to have the system presented viewcontroller as a container.
And then present your content viewcontroller inside modally.
// this is the entry point
// either the initial viewcontroller inside the extensions storyboard
// or
// the one you specify in the .plist file
class ContainerVC: UIViewController {
// afaik presenting in viewDidLoad/viewWillAppear is not a good idea, but this produces the exact result you are looking for.
// meaning the content slides up when extension is triggered.
override func viewWillAppear() {
super.viewWillAppear()
view.backgroundColor = .clear
let vc = YourRootVC()
vc.view.backgroundColor = .clear
vc.modalPresentationStyle = .overFullScreen
vc.loadViewIfNeeded()
present(vc, animated: false, completion: nil)
}
}
and then use the content viewcontroller to show your root viewcontroller and its view hierarchy.
class YourRootVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = UIViewController() // your actual content
vc.view.backgroundColor = .blue
vc.view.frame = CGRect(origin: vc.view.center, size: CGSize(width: 200, height: 200))
view.addSubview(vc.view)
addChild(vc)
}
}
Basically a container and a wrapper in order to get the control over the views being displayed.
Source: I had the same problem. This solution works for me.
I have two controller in the screen as the pic shows.
now I wanna present the ThirdViewController in the red area when the tableView is selected, I try to use present() but it shows in the whole screen. how Can I fix that?
Try this
Download 3 files from Folder Presentation
Add this to the VC from which you will open Second VC
lazy var slideInTransitioningDelegate = SlideInPresentationManager()
When going to another VC;
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Ident") as? YourViewController
vc?.delegate = self
let navigationController = UINavigationController(rootViewController: vc!)
slideInTransitioningDelegate.direction = .bottom // or .left, .right
slideInTransitioningDelegate.disableCompactHeight = true
navigationController.transitioningDelegate = slideInTransitioningDelegate
navigationController.modalPresentationStyle = .custom
self.present(navigationController, animated: true, completion: nil)
Tutorial - https://www.raywenderlich.com/915-uipresentationcontroller-tutorial-getting-started
Project - https://koenig-media.raywenderlich.com/uploads/2016/08/Medal_Count_Completed.zip
You can do this by first adding your SecondViewController into you MainViewController as child controller and then add SecondViewController's view as subview in UITableViewCell.
var secondViewController : SecondViewController!
override func viewDidLoad() {
super.viewDidLoad()
secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.addChildViewController(setupProfileViewController)
}
Now somewhere in your code when you want to show that SecondViewController view controller like on didSelectRowAt indexPath method of UITableViewCell or any other button action, add following line of code:
let cell = yourTableView.cellForRow(at: selectedIndexPath)
cell.contentView.addSubview(secondViewController.view)
There is no image attached, so I am not sure what exactly you want; but to show a view controller in area of another view controller, you could use container view, this is a useful tutorial about container view
and to load by some action, you could present your container view when your action done.
So I'm having a table view controller and using https://github.com/jonkykong/SideMenu i'm trying to display a "slide in" sidebar which works, but it doesn't show me the view that I want in the sidebar is black
// Define the menus
let menuLeftNavigationController = UISideMenuNavigationController()
menuLeftNavigationController.leftSide = true
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration of it here like setting its viewControllers.
SideMenuManager.menuLeftNavigationController = menuLeftNavigationController
// Enable gestures. The left and/or right menus must be set up above for these to work.
// Note that these continue to work on the Navigation Controller independent of the View Controller it displays!
SideMenuManager.menuAddPanGestureToPresent(toView: self.navigationController!.navigationBar)
SideMenuManager.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController!.view)
When It clicks on the sidebar button I have this, which creates the animation but doesn't show the viewcontroller
func someAction(){
present(SideMenuManager.menuLeftNavigationController!, animated: true, completion: nil)
debugPrint("clicked")
}
On the repo readme, you will find your answer, take a look on the Customization
section https://github.com/jonkykong/SideMenu#sidemenumanager.
Simply set menuFadeStatusbar = false
SideMenuManager.default.menuFadeStatusBar = false
From the previous link, you will find the following information : "Draws the menuAnimationBackgroundColor behind the status bar. Default is true.
If menuFadeStatusBar is true, this color is used to fade it. Default is black."
The answer is in the comment of the snippet you posted:
// UISideMenuNavigationController is a subclass of UINavigationController,
// so do any additional configuration of it here like setting its viewControllers.
let btnMenu:UIButton = UIButton()
btnMenu.frame = CGRect(x: 20*valuePro, y: 20*valuePro, width: 40*valuePro, height: 40*valuePro)
btnMenu.backgroundColor = .red
btnMenu.addTarget(self, action: #selector(self.displayMenu), for: .touchUpInside)
self.view.addSubview(btnMenu)
#objc func displayMenu(sender: UIButton!) {
print("Button Clicked")
present(SideMenuManager.default.menuLeftNavigationController!, animated: true, completion: nil)
}
https://github.com/jonkykong/SideMenu
The SideMenu README
Code Implementation
First:
import SideMenu
From a button, do something like this:
// Define the menu
let menu = UISideMenuNavigationController(rootViewController: YourViewController)
// UISideMenuNavigationController is a subclass of UINavigationController, so do any additional configuration
// of it here like setting its viewControllers. If you're using storyboards, you'll want to do something like:
// let menu = storyboard!.instantiateViewController(withIdentifier: "RightMenu") as! UISideMenuNavigationController
present(menu, animated: true, completion: nil)
Answer
You need use UISideMenuNavigationController(rootViewController:), not UISideMenuNavigationController().
If you open the side menu from left, set leftSide to true.
let menu = UISideMenuNavigationController(rootViewController: YourViewController)
menu.leftSide = true
present(menu, animated: true, completion: nil)
Of course, you should give instance variable to UISideMenuNavigationController(rootViewController:).
I can recommend using JASidePanels
It's pretty simple and it just works. You're creating an JASidePanelController, setting this class to empty viewcontroller in your Storyboard and making this controller initial. (do not forget to import JASidePanels at the top of the class)
Then, in this class, you're implementing awakeFromNib() method like this:
leftPanel = ..//instantiating menu controller
let centerController = ...//instantiating center controller
centerPanel = UINavigationController(rootViewController: centerController)
That's it.
You can instantiate controller via their ID which can be set in Identity Inspector
let stb = UIStoryboard(name: "Main", bundle: nil) //instantiating a storyboard we will use for instantiating controllers
let someController = stb.instantiateViewController(withIdentifier: "here_your_identifier_goes") as! YourControllerClass
I am trying to create a pop over and when I present the view controller, the background is always solid black and the size is full screen.
I can't seem to figure out what's wrong and here is the code that I have
#IBAction func distancePopOver( sender : UIBarButtonItem){
//a UIViewController that I created in the storyboard
let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver")
controller.modalPresentationStyle= UIModalPresentationSTyle.PopOver
controller.preferredContentSize = CGSizeMake(200,30)
self.presentViewController(controller, animated: true, completion: nil)
//Configure the Popover presentation controller
let popController = (controller.popoverPresentationController)!
popController.permittedArrowDirections = UIPopoverArrowDirection.Down
popController.barButtonItem = sender
popController.delegate = self
}
Whenever I click on the UIBarButtonItem, it presents the view in full screen, but shouldn't it be the size I specify in line 5?
Popovers are quite finicky now. First, you are going to want to configure the popoverPresentationController before you present it.
Secondly, make sure your arrow direction is pointing the way the arrow points and not where the content is respective to the UIBarButtonItem. So, if its inside UIToolbar (and is near the bottom of the screen) you'll want .Down otherwise if its a navigation bar (near the top) you'll want to use .Up.
#IBAction func distancePopOver( sender : UIBarButtonItem){
//Configure the Popover presentation controller
let popController = (controller.popoverPresentationController)!
popController.permittedArrowDirections = .Down // .Up
popController.barButtonItem = sender
popController.delegate = self
//a UIViewController that I created in the storyboard
let controller = storyboard!.instantiateViewControllerWithIdentifier("distancePopOver")
controller.modalPresentationStyle = .Popover
controller.preferredContentSize = CGSizeMake(200,30)
presentViewController(controller, animated: true, completion: nil)
}
Now if you got this far and its still not working, its because the popover's default behavior in a compact size class is to fill the screen. Since you are already setting your view controller to be the popover's delegate you'll just need to implement this delegate function: adaptivePresentationStyleForPresentationController(_:traitCollection:) and return .None for the presentation style. This will allow you to even show a real looking popover on the iPhone. See my blog post: iPhone Popover for a full example of doing this.