UITransitionView on top of ViewController using UIModalPresentationStyle.OverCurrentContext [duplicate] - ios

This question already has an answer here:
Pass touches through a UIViewController
(1 answer)
Closed 7 days ago.
I'm trying to set a view controller on top of another while keeping some of the first on visible.
However, I can't see the first one because I have a UITransitionView with a UIView on top of it (the first one is in the hierarchy and that's how I know .OverCurrentContext is working)
This should be a very simple code and I'm not sure what's going on. I know I can't touch the UITransitionView and that's why I'm not sure what to do.
let vc = storyBoard.instantiateInitialViewController() as MyPresenterViewController
vc.view.backgroundColor = .clearColor()
vc.modalPresentationStyle = .OverCurrentContext
viewControllerToPresentIn.presentViewController(vc, animated: true, completion: nil)
Suggestions would be highly appreciated.
Best regards,

A little late to the party with this response, but I recently had at a similar problem. A modal I wanted to present (with a semi-transparent veil) was being contained by a white UITransitionView, hiding the underlying views. I suspect this TransitionView gets injected when you issue a controller.present() call to show your Modal in some circumstances (but don't know why or when)
As a quick and dirty workaround, you can try to clear the background of the parent views of your modal before you present. For some reason, the UITransitionView then also gets a clear colour once you present your modal.
extension UIView {
//This will eventually get to the UITransitionView and clear the background.
func clearHierarchyBackground() {
self.backgroundColor = .clear
if let superView = self.superview {
superview.clearHierarchyBackground()
}
}
}
let parentController = MyParentViewController()
let childController = MyModalViewController()
childController.modalPresentationStyle = .overFullScreen //.overCurrentContext should also work..
childController.view.clearHierarchyBackground()
/* The above makes your controller root view .clear
so you need to add subviews with transparency &
content as you see fit.*/
parentController.present(childController, animated:true....)

Related

Swift - Present a UIView (not a ViewController)

I have two UIViewControllers and I would like to .present the gray UIView like you would usually present a ViewController (appearing bottom up and user can slide down to dismiss).
Screenshots for a better understanding:
The bottomBar is a ContainerView and should not change by switching between the VC's, only the gray UIView which you can see in the 2nd picture.
I know I could just .present ViewControllerB without an animation and then just let the UIView appear from out of the screen. But if I do it like this the user is not able to drag down the UIView to dismiss it.
This is how I present ViewControllerB ("wishlistViewController) at the moment.
let wishlistViewController = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as! WishlistViewController
wishlistViewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.navigationController?.present(wishlistViewController, animated: false)
}
Main Problem is that my UIView is not fullscreen. Otherwise I could just .present ViewControllerB. However, with my design the background image would be animated by .presenting or .dismissing as well and I do not want that.
There is probably a very easy solution for this but I couldn't find on presenting ONLY a UIView.
Grateful for any tips :)
Well, there are two answers I can think of:
1) If you want, you could just animate it in from the bottom using UIView.animate() on a y-position constraint it has, or on its layer's y-position attribute. You could then attach a UISwipeGestureRecognizer to it so that if you swipe down on it, it will animate back down.
2) Make your views that you'd like to present viewControllers and have them be presented with a custom modal animation. This sounds like more work than you wanna do though. Here's a cool tutorial on youtube that walks you through how to create a custom animation transition between 2 view controllers:
https://www.youtube.com/watch?v=B9sH_VxPPo4
However, as far as I know, those .dismiss and .present methods are only meant for view controllers. Hopefully one of the two options I gave were helpful!

How is this settings popup created? [duplicate]

This question already has answers here:
Present a popover from an arbitrary anchor point in Swift
(3 answers)
Closed 4 years ago.
I really like the way the settings are shown on the Books app and am trying to figure out how replicated it.
Screenshot of Books app with settings shown
Is this an .actionsheet that is somehow moved to show up under the Settings image or a container view that shows up on tap or something else? I'm still at level noob with a basic app and would like to implement this.
It is iOS default presentation style for controllers. It's called UIPopOverPresentationController. Here's a good article on presentation controllers. After that you might want to create two to three cells for your options in a UITableView and add that as controller for the popover from your storyboard.
You can also change the popover arrow pointing location and direction which you might need when you want to support your app for iPad as well. :)
#IBAction func actionWasTapped(sender: UIBarButtonItem) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "PopOverVC")
vc.modalPresentationStyle = .popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.barButtonItem = sender
present(vc, animated: true, completion: nil)
}
This can done with a-lot of way , you can just create by
UIView work as background with custom subview (PopOverMenu) done with UIBezierPath to draw this arrow with corner radius view
upon this pop over you can create MultiSection tableview or collection View or any
Background is add as SubView to UIApplication.sharedApplication().keyWindow?.addSubview(background)
Background have tab gesture to dismiss when tab on background
some closure to bring back data that user select it

Container View Controller disappears when Child View Controller presents a Modal View Controller iOS Swift

I'm new to the iOS swift scene and i was hoping you would be able to help me find a solution or send me on an alternative route to achieving this.
Here is the diagram to my Application Interface Structure
So, i have the ContainerVC which has a UIScrollView, and that UIScrollView has a the four ChildVCs inside. My problem is when i present the ModalVC from one of the ChildVCs, the ContainerVC, which has the UIScrollView
and the application's background disappears from the screen completely and the background of the app becomes black. I believe that happens because iOS thinks that the ContainerVC is no longer being used so it removes it from the hierarchy when the modal is presented on top of the ChildVC.
I wanted to be able to have the ContainerVC, which provides a background for the app and the UIScrollView where the ChildVC are embedded, visible at all times even when a ChildVC presents the ModalVC modally. Before the ModalVC is presented, the ContainerVC can be seen behind the ChildVC around the edges (modal ahah) of the screen and everything is fine, but that changes when i trigger a tap event and i present the modal. Here is the before and after:
Before Tap Event >>>
After Tap Event & ModalVC appears
You can see that the purple background disappeared now that the ModalVC is presented and though you cant see it, I'm not able to scroll side ways anymore because the UIScrollView isn't there either.
How can i present the ModalVC from the ChildVC without losing the ContainerVC and its UIScrollView inside? i.e. from disappearing and staying the the view hierarchy
Here is the code is use to present the ModalVC from a given ChildVC.
func didTapCell(origin: String) {
let modalVC = storyboard?.instantiateViewController(withIdentifier: "overlayModal") as! OverlayModalViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = self // i think this self has something to do with the background disappearing
modalVC.modalPresentationStyle = .overCurrentContext
self.present(modalVC, animated: true, completion: nil)
}
I have been looking for a solution for a while now but i can't seem the figure it out. I would be great if you could help work this out or help find a way around it and achieving the same. Please help me out.
Thank you in advance. Please let me know if there's anything i can do to clarify anything.

Swift Modal View Controller with transparent background [duplicate]

This question already has answers here:
Transparent background for modally presented viewcontroller
(5 answers)
Closed 7 years ago.
I know this topic is quite popular, but I'm a little iniciate problem in a programming language, the fact is that I still do not understand where I put the code. Well, I'll tell the whole case:
I'm trying to make a modal Swift in a little different from normal: By clicking on a button, the ViewController is displayed (following modal type) on the screen, but with transparent background. Only the blue View with label will be displayed. When this ViewController is presented, it is with transparent background, but as soon as it completes the transition, it will stay with the black background. Already deactivated the opaque option, and tested some options, but nothing this troubleshooting.
Some can help me?
The video is a test in the simulator on the case (https://www.youtube.com/watch?v=wT8Uwmq9yqY).
I'm starting with swift, and I'm still pretty lost with how to program in Xcode, I read an answer to a question that has the following code to solve this:
self.presentingViewController.providesPresentationContextTransitionStyle = YES;
self.presentingViewController.definesPresentationContext = YES;
modal.modalPresentationStyle = UIModalPresentationOverCurrentContext;
Where do I put this code?
You can do it like this:
In your main view controller:
func showModal() {
let modalViewController = ModalViewController()
modalViewController.modalPresentationStyle = .overCurrentContext
presentViewController(modalViewController, animated: true, completion: nil)
}
In your modal view controller:
class ModalViewController: UIViewController {
override func viewDidLoad() {
view.backgroundColor = UIColor.clearColor()
view.opaque = false
}
}
If you are working with a storyboard:
Just add a Storyboard Segue with Kind set to Present Modally to your modal view controller and on this view controller set the following values:
Background = Clear Color
Drawing = Uncheck the Opaque checkbox
Presentation = Over Current Context
As Crashalot pointed out in his comment: Make sure the segue only uses Default for both Presentation and Transition. Using Current Context for Presentation makes the modal turn black instead of remaining transparent.

Can't get my ViewController to appear on top of my current one without issues

Alright so I've tried multiple methods at this point.
Method 1: Present the new viewcontroller using its view. By the way both methods involve adding a VisualEffectView first, I don't know if it effects views placed on top of it.
var vsView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Dark))
vsView.frame = parentView!.frame
self.parentView!.addSubview(vsView)
var vc = self.parentController!.storyboard?.instantiateViewControllerWithIdentifier("GoalTableView") as GoalsViewController
vc.view.backgroundColor! = UIColor.clearColor()
var vcView = vc.view
vcView.frame = CGRectMake(0, 0, self.parentView!.frame.width, self.parentView!.frame.height)
self.parentView!.addSubview(vc.view)
The issue I had with this method was that the new view would appear perfectly over the new one for about half a second then boom its disappeared. It is (possibly) still active as it still catches my taps but eventually it crashes.
Method 2: Present the new ViewController modally AKA on top of the new one without extracting its view.
var vsView = UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffectStyle.Dark))
vsView.frame = parentView!.frame
self.parentView!.addSubview(vsView)
var vc = self.parentController!.storyboard?.instantiateViewControllerWithIdentifier("GoalTableView") as GoalsViewController
vc.view.backgroundColor! = UIColor.clearColor()
self.parentController!.presentViewController(vc, animated: true, completion: nil)
vc.presentingViewController!.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.parentController!.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
vc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
This time around it does appear over the new viewcontroller but now with a black background (which I believe is being done for resource management reasons - it gets rid of the old vc). I am not sure if I am using UIModalPresentationStyle correctly but it doesn't appear to make a difference no matter what vc I apply it to or what.
Can anyone suggest some ideas? At this point I am not sure what to do because I thought the original method of using the vc's view would definitely work, but apparently not...I have tried not even adding the vsView and just adding the vc.view to the parentView but that doesn't work either!

Resources