Howto Size the Modal View programmatically (SWIFT) - swift

I simply want to present a small option dialog over an existing main UIViewController/UIView , so that on an IPad I would see a small Dialog and in the Background I will see the Main View.
I managed to show a UIViewController/UIView in a modal view style as follow:
func showoptions(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("Options") as! UIViewController
controller.modalPresentationStyle = UIModalPresentationStyle.Popover
let popoverPresentationController = controller.popoverPresentationController
// result is an optional (but should not be nil if modalPresentationStyle is popover)
if let _popoverPresentationController = popoverPresentationController {
// set the view from which to pop up
_popoverPresentationController.sourceView = self.view;
//_popoverPresentationController.sourceRect = CGRectMake(60, 100, 500, 500)
//_popoverPresentationController. .setPopoverContentSize(CGSizeMake(550, 600), animated: true)
//_popoverPresentationController.sourceView.sizeToFit();
// present (id iPhone it is a modal automatic full screen)
self.presentViewController(controller, animated: true, completion: nil)
}
}
But I have still some issues:
1. Howto get rid of the arrow shown at the border.
2. Howto size this modal view. It is shown to small and I would like to fit it to the largest controls in the UIControllerView/UIView.
any help ?

Try changing _popoverPresentationController.permittedArrowDirections to empty option set
Change controller.preferredContentSize to match your desired size

I needed something similar and ended up presenting a view controller as a modal with a transparent background over the current context. There I made a smaller opaque UIView with what I wanted.

Related

Height of view as a modal popup - Swift

In the app that I am creating with XCode I created a view that opens as a modal popup. I used this code:
#IBAction func open_view(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let VC_test_object = storyBoard.instantiateViewController(withIdentifier: "VC_Test")
VC_test_object.modalPresentationStyle = .popover;
self.present(VC_test_object, animated: true, completion: nil)
}
VC_Test is the ViewController in Storyboard. This is the result:
However, I would like to manage the height of the view to create a sort of menu.
As in this example screenshot (from the Documents app):
I can't understand how to do it.
Could you tell me how to manage the height of the popup?
Thanks for your help.
For a code-your-self version this SO link and the answers below it is what you need: Present modal view controller in half size parent controller
However, I would rather use this third party library : Presentr

View on View controllers are all square shaped on Xcode 11, how?

I have tried to change it's size manually (by dragging) and still not working.
You can set its Content size to whatever you want.
On iOS 13 a "Modal" segue will show the presenting controller that way by default.
If you want the view controller to occupy the whole screen, set "Full Screen" on the "presentation" setting of any segue entering that controller.
Segue Settings
If your view presents programmatically (not via storyBoard), have this in mind:
From XCode 11 and up, when you build for iPad you have to add "modalPresentationStyle = .fullScreen" to get full screen views, not square ones. As follows:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
If you want transparency on the new viewController, you can write ".overFullScreen", as follows:
let vc = UIViewController()
vc.modalPresentationStyle = .overFullScreen
self.present(vc, animated: true, completion: nil)

NSPopover animate contentViewController change and change popover size

I am making a NSPopover, and have made it so I can transition between view controllers with a sliding animation using a parent view controller with my view controllers as children. It works well, except before I added this, the popover automatically resized to my view's size, but now, the popover is stuck at a fixed size.
The code for creating the popover:
self.homeVC = PopoverViewController(nibName: "PopoverViewController", bundle: nil)
self.loginVC = SignInViewController(nibName: "SignInViewController", bundle: nil)
self.containerView.view.wantsLayer = true
self.containerView.view.frame = self.homeVC!.view.bounds
self.containerView.addChildViewController(self.homeVC!)
self.containerView.view.addSubview(self.homeVC!.view)
popover.contentViewController = self.containerView
The code for transitioning the view controllers:
self.loginVC!.view.frame = self.homeVC!.view.bounds
self.containerView.addChildViewController(self.loginVC!)
let transition: NSViewControllerTransitionOptions = .SlideLeft
self.containerView.transitionFromViewController(self.homeVC!, toViewController: self.loginVC!, options: transition, completionHandler: { finished in
self.homeVC!.view.removeFromSuperview()
self.homeVC!.removeFromParentViewController()
self.containerView.view.bounds = self.loginVC!.view.bounds
})
Is there anyway that I can make the popover automatically resize to the size it is supposed to be after transitioning?
Thanks in advance.
I was able to fix it by instead of explicitly setting the frame, setting constraints to make the container view match the child's size.

Displaying UIDatePicker as Popover within UITableView on iPhone

I would like display a date picker with a UIModalPresentationPopover presentation style, and while it's working fine on an iPad, it results in a black screen when displayed on an iPhone. Edit: Specifically, it is simply presented full screen, so the effect is a black screen unless I add colours to elements manually. The default colours work fine for Popover presentation.
The code generating the picker and the popover is:
let datePicker = UIDatePicker()
datePicker.datePickerMode = .DateAndTime
datePicker.minuteInterval = 5
let vc = UIViewController()
vc.view.addSubview(datePicker!)
vc.preferredContentSize = datePicker.frame.size
vc.modalPresentationStyle = .Popover
let cell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 1))
let ppc = vc.popoverPresentationController
ppc?.delegate = self
ppc?.sourceRect = cell.accessoryView!.frame
ppc?.sourceView = cell
self.presentViewController(vc, animated: true, completion: nil)
The value of cell appears correct on both types of devices, but I notice that the size of the view of the presented viewController gets messed up on the iPhone. After presentation is finished, the value of preferredContentSize property is (320.0, 216.0) on both devices, but view.frame.size becomes (320.0, 568.0) on the iPhone (which is full screen on iPhone 5, and it acts similarly on iPhone as well), while on iPad it still matches the preferred size.
Looks great on iPad:
I've had no trouble presenting the same kind of UI from a UIViewController that was not UITableViewController. Is there anything else that needs to be done to allow this style of presentation with a tableView on an iPhone?
Edit
Fixed. To allow this, need to add a method to the presenting controller:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return .None
}
This is what I found on the docs from UIViewController
modalPresentationStyle
The presentation style determines how a modally
presented view controller is displayed onscreen. In a horizontally
compact environment, modal view controllers are always presented
full-screen
What you can do is, on the iPhone, set the backgroundColor of the view to black with `.15 alpha, and position the picker at the bottom.
You might need to set modalPresentationStyle to UIModalPresentationOverFullScreen, because otherwise it removes the views underneath.

[SWIFT]Presenting Modal View Controller with transparency in ios7

Im trying to present a modal view controller in my app with the background transparent, it works for ios8, but for some reason in ios7 the background becomes gray and doesnt show transparency, Ive already set the alpha to the view in storyboard and below is the code Im using:
let storyboard = UIStoryboard(name: "PopUpStoryboard", bundle: nil);
let vc = storyboard.instantiateViewControllerWithIdentifier("PopUpQuestionController") as PopUpQuestionController;
vc.currentEvent = self.currentEvent
vc.questionMode = "text"
self.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
self.presentViewController(vc, animated: true, completion: nil);
the result in ios8:
and the result in ios 7:
anyone can help me with this?