Displaying UIDatePicker as Popover within UITableView on iPhone - 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.

Related

How can i set master/detail view side by side in portrait mode using uisplitviewcontroller

I am using the new UISplitViewController, coming with iOS 14 as follows:
let splitViewController = UISplitViewController(style: .doubleColumn)
splitViewController.preferredDisplayMode = .oneBesideSecondary
let primaryVC = PrimaryViewController()
splitViewController.setViewController(primaryVC, for: .primary)
let secondaryVC = SecondaryViewController()
splitViewController.setViewController(secondaryVC, for: .secondary)
It works as expected, but i wanted to get a similiar result like this:
Landscape
i want to only have a expand icon in the secondaryViewController to hide or show the sidebar as you maybe also know from the Apple Notes App for iPad. I want to hide/remove default Sidebar Icon.
Portrait
i want the primary and secondary view side-by-side. With the new UISplitViewController, the primary view always collapsed and you get a "Back" Button in the NavigationBar.
So is there a way to manipulate or customize the new UISplitViewController to get the result, as i described above?
What i tried
switch to the "classic" UISplitViewController like this:
let splitViewController = UISplitViewController(style: .unspecified)
but with the style unspecified i get the following runtime error:
API misuse. -initWithStyle: may not be used with UISplitViewControllerStyleUnspecified
I think that in your case the style should be doubleColumn, as it's what you want to achieve in both portrait and landscape mode.
Try to implement this method in the UISplitViewControllerDelegate:
optional func splitViewController(_ splitViewController: UISplitViewController,
collapseSecondary secondaryViewController: UIViewController,
onto primaryViewController: UIViewController) -> Bool
And if not already set, set your UISplitViewController's delegate. Return false if you don't want to collapse the split view controller (which means that if not collapsed, it will display two columns; Which is your case).

How to use different modalPresentationStyles based on size in swift?

In my app, I want to present a bottom card like in the maps app. On the iPhone it should be visible on half of the screen on the iPad it should be visible as a full formSheet.
So I want to use 2 different modalPresentationStyles: .overCurrentContext and .formSheet.
Of course, when it is presented you can decide with a normal if-closure which style should be used, but I want it to adapt to traitCollection changes. How do I change the modalPresentationStyle after the UIViewController is already presented?
let card = CardController()
card.modalPresentationStyle = .formSheet
// card.modalPresentationStyle = .overCurrentContext
self.present(card, animated: true, completion: nil)
Here are pictures of the expected result:
The dark background behind the iPhone-card is the background of the presented ViewController, the card a UIView.

Swift iOS -How to Add Subview to Window's Center?

I have an activity indicator that gets presented on an iPhone and iPad. In the iPad in split screen mode it gets presented to whichever side of the view that called it. I would instead like it to get presented in the middle/center the window's screen. If I do it this way wether on the iPhone in portrait or iPad in split screen mode it will always be in the center of the screen.
How do I do this?
MyView: UIViewController{
let actInd = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
#IBAction fileprivate func buttonPressed(_ sender: UIButton) {
guard let window = UIApplication.shared.keyWindow else { return }
//how to add actInd as subview to the window' screen?
actInd.startAnimating()
}
}
It's pretty simple. Turn off the auto-resizing mask. Add the add actInd to window, then set the center anchors.
actInd.translatesAutoresizingMaskIntoConstraints = false
window.addSubview(actInd)
actInd.centerXAnchor.constraint(equalTo: window.centerXAnchor).isActive = true
actInd.centerYAnchor.constraint(equalTo: window.centerYAnchor).isActive = true
Window is subclass of UIView. Just add it as it's subview like you're adding a view to another view. But remember that window is shared throughout your app, so adding it every-time will consume memory, remove it after your job is done.
If you want to center it in the window, you can use autoResizingMask or add constraints to it.

Howto Size the Modal View programmatically (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.

iPhone popup menu like iPad popover?

How can i implement this popup menu in iphone app like a popover in ipad?
EDIT: This is the best at moment: https://github.com/runway20/PopoverView
iOS 8 and later
Beginning with iOS 8, you can use UIPopoverPresentationController for iPhones in addition to iPads.
Setup
Add a UIBarButtonItem to your main View Controller.
Add another View Controller to the storyboard. Change it to the size that you want the popover to be and add any content that you want it to have. For my example I just added a UILabel. If you want a whole menu, then just add a table view or list of buttons.
Add a segue from the bar button item to the view controller that you will use as the popover. Rather than show, choose Present as Popover.
Select the segue in the storyboard and set the identifier to popoverSegue (or whatever string you called it in the code).
In the Attributes inspector for the popover view controller, check Use Preferred Explicit Size and confirm that it is the size you want it to be.
Code
This is the code for the main view controller that has the bar button item in it.
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "popoverSegue" {
let popoverViewController = segue.destinationViewController
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
popoverViewController.popoverPresentationController!.delegate = self
}
}
// MARK: - UIPopoverPresentationControllerDelegate method
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
// Force popover style
return UIModalPresentationStyle.None
}
}
Popover at an arbitrary anchor point
If you want to set the popover to appear somewhere besides a bar button item (on a UIButton for example) then you need to set the sourceView and sourceRect. See this answer for details.
Further reading
The above example comes mostly from the first link.
iPad Style Popovers on the iPhone with Swift
iOS 8 Popover Presentations
UIPopoverPresentationController on iOS 8 iPhone
General overview of popup options in iOS
Have a look at the iPhone UIPopoverController implementation: WEPopover
On iPhone you would generally use a UIActionSheet for a stack of buttons like that. It slides up from the bottom, rather than popping up next to the button, but that's the standard behavior on iPhone.
There is one that is even better than WEPopover. Developed by a company called 50pixels, it is called FPPopover.
You can download FPPopover at https://github.com/50pixels/FPPopover
You would have to manually instantiate a UIView using a custom background image or drawing with transparency, add some UIButtons (or other type of custom view) on top, and also somehow handle all touches outside that view.
Note that is is non-standard UI. An actionsheet would be more HIG compliant.
To get a popover from a right side bar button item on a navigation controller that is part of a tableview controller, the following worked for me for Swift 4 and Xcode 9.
Follow the steps in Suragch answer above (as edited by the Community.)
Do not implement the Segue as shown in the answer above. For some reason, the segue causes the popover to go full screen despite setting the explicit size.
Give your popover view controller a title in Attributes Inspector
Add the following code in the TableView controller where the popup will show.
Modify the string identifier (the one here is referencing a Constant.swift file)
Modify "as! FilterVC" to use the title of the your popover view controller.
/// Shows a filter popover view
#IBAction func filterBtnPressed(_ sender: UIBarButtonItem) {
let popover = storyboard?.instantiateViewController(withIdentifier: FILTER_VC) as! FilterVC
popover.modalPresentationStyle = UIModalPresentationStyle.popover
popover.popoverPresentationController?.backgroundColor = UIColor.green
popover.popoverPresentationController?.delegate = self
popover.popoverPresentationController?.backgroundColor = ColorPalette.Blue.Medium
popover.popoverPresentationController?.sourceView = self.view
popover.popoverPresentationController?.sourceRect = CGRect(x: self.view!.bounds.width, y: 0, width: 0, height: 0)
popover.popoverPresentationController?.permittedArrowDirections = .up
self.present(popover, animated: true)
} }
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.none
}
You can check WYPopoverController: https://github.com/sammcewan/WYPopoverController
The screenshot above is not a UIActionSheet. It looks like a simple UIView subclass with custom UIButtons on top of it. So go ahead and create the subclass according to your needs and then add it as a subview to your view every time you need it.