I have created a small project to replicate this problem.
The only file is this one...
Brief bit of code
class RootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
showBlue()
}
#objc func showBlue() {
let vc = UIViewController()
vc.view.backgroundColor = .blue
let nvc = UINavigationController(rootViewController: vc)
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(showGreen))
transition(to: nvc)
}
#objc func showGreen() {
let vc = UIViewController()
vc.view.backgroundColor = .green
let nvc = UINavigationController(rootViewController: vc)
vc.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(showBlue))
transition(to: nvc)
}
func transition(to toVC: UIViewController) {
if let fromVC = children.first {
transitionWithAnimation(fromVC: fromVC, toVC: toVC)
} else {
addWithoutAnimation(child: toVC)
}
}
func addWithoutAnimation(child toVC: UIViewController) {
addChild(toVC)
view.addSubview(toVC.view)
toVC.view.frame = view.bounds
toVC.didMove(toParent: self)
}
func transitionWithAnimation(fromVC: UIViewController, toVC: UIViewController) {
addChild(toVC)
toVC.view.frame = view.bounds
fromVC.willMove(toParent: nil)
transition(
from: fromVC,
to: toVC,
duration: 1.0,
options: .transitionCrossDissolve,
animations: nil) { _ in
fromVC.removeFromParent()
toVC.didMove(toParent: self)
}
}
}
Explaining the code
The RootViewController first does showBlue. This adds a child UINavigationController with a rootViewController with a blue background. The blue view controller has a Done button that then targets showGreen.
showGreen transitions to a UINavigationController with a green background and a Done button that targets showBlue.
What I expected
What I expected (and what I want to happen) is for the navigation bar to cross dissolve in place without resizing.
Animation of the problem
The problem is that during the animated transition the navigation bar has a strange animation to it. Which you can see here...
Apple documentation around this
All the code is followed exactly from the Apple documentation about adding child view controllers to a custom container view controller... https://developer.apple.com/library/archive/featuredarticles/ViewControllerPGforiPhoneOS/ImplementingaContainerViewController.html
Things I tried
I have also tried by using AutoLayout constraints rather than setting the view's frame directly but this didn't change anything.
I've tried running view.setNeedsLayout and then view.layoutIfNeeded() on the new view controller's view but that doesn't seem to have fixed it either.
No strange animation if child is not UINavigationController
The really odd thing is that if you use any other type of view controller (other than UINavigationController) then this animation glitch doesn't happen. For example: If one of the view controllers is a UITabBarController then the tabs do not have this odd animation. Even stranger, if the tab contains a UINavigationController then it doesn't have this animation either. It's literally just if the direct child is a UINavigationController.
Has anyone experienced this before? And did you manage to stop the strange animation?
If you place the transition code within a CATransaction and use the kCATransactionDisableActions key to turn off implicit actions it will resolve the issue:
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey:kCATransactionDisableActions)
transition(
from: fromVC,
to: toVC,
duration: 1.0,
options: [.transitionCrossDissolve],
animations: nil) { _ in
fromVC.removeFromParent()
toVC.didMove(toParent: self)
}
CATransaction.commit()
Related
I am presenting a layover VC programmatically.
However, I would like to keep the swipe down functionality of dismissing the VC and keep the background blur while ultimately presenting the VC half way from the bottom of view.
Presenting VC:
let navVC = UINavigationController(rootViewController: SnackBarViewController())
present(navVC, animated: true)
I have found a way to present the VC half way like so, but I lose the swipe functionality and the background blur.
let navVC = UINavigationController(rootViewController: SnackBarViewController())
navVC.transitioningDelegate = self
navVC.modalPresentationStyle = .custom
present(navVC, animated: true)
extension SettingsViewController : UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
return HalfSizePresentationController(presentedViewController: presented, presenting: presenting)
}
}
class HalfSizePresentationController : UIPresentationController {
override var frameOfPresentedViewInContainerView: CGRect {
get {
guard let theView = containerView else {
return CGRect.zero
}
return CGRect(x: 0, y: theView.bounds.height/2, width: theView.bounds.width, height: theView.bounds.height/2)
}
}
}
Is there a better way to present the VC half way in swift 5 without losing the presentation style format by default?
I have also heard of UISheetPresentationController using detents but im not entirely sure how to implement.
Reference here
base ios version 13.0. xcode version 12.5.1
In my application I have 2 storyboards: Onboarding and Main. When user opens app for a first time - Onboarding storyboard is presented. After that, he presses a button and I show him Main Storyboard with such code:
let storyboard = UIStoryboard(name: "Shop", bundle: nil)
let navigationVc = storyboard.instantiateViewController(withIdentifier: "ShopScreens") as UIViewController
navigationVc.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
self.present(navigationVc, animated: false, completion: nil)
I wanted to make custom animation, when user switches storyboards. To do so - I've created simple view that I'm presenting on top of everything like this:
UIApplication.shared.keyWindow?.addSubview(self.containerViewForAnimatedView)
And this view is working fine, but only in terms of one Storyboard, it covers everything while app changes screens.
But when I try to switch storyboards - this view gets covered by newly presented storyboard.
I also tried presenting view in such way and brining it to front:
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.addSubview(self.containerViewForAnimatedView)
But that din't work as well.
How can I hide switching of storyboards by presenting custom created view during that transition?
Would be grateful for any help.
Just played around some with it, no animations or anything special but this will give you the flow that you want:
class ViewController: UIViewController {
let viiew = UIView.init(frame: UIScreen.main.bounds)
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.viiew.backgroundColor = .blue
UIApplication.shared.keyWindowInConnectedScenes?.resignKey()
UIApplication.shared.keyWindowInConnectedScenes?.addSubview(self.viiew)
self.viiew.layer.zPosition = 1000
}
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
let vc = ViewController2()
UIApplication.shared.keyWindowInConnectedScenes?.rootViewController = vc
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
self.viiew.removeFromSuperview()
}
}
}
}
class ViewController2: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
}
}
extension UIApplication {
var keyWindowInConnectedScenes: UIWindow? {
return windows.first(where: { $0.isKeyWindow })
}
}
I am making an app that uses a tab bar to navigate between view controllers. I wanted to add a transition effect that would cross dissolve between each view when a tab button was pressed. I have implemented this transition with UIView.transitionFromView, however the navigation bar is not working as expected during the transition. During a transition to a view for the first time, the navigation bar is displayed too high, but jumps back into place once the transitions is complete. However, the next time you switch to the same view, the navigation bar is in the correct place during and after the transition.
I have seen an answer here to fix the problem for a custom animation, but I could not figure out how to get it to work with my current implementation.
MY Question
I have seen answers fixing the issue by forcing the view down by a few points (44 points), but is there a way to do it without directly changing the points? This might work the first time, but the issue resolves itself when any view is transitioned to a second time, thus making the view too low if you change the points.
Here is my code for the tab bar controller and the transition:
import UIKit
class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Method used to detect when a tab bar button has been tapped
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
// Creating the 'to' and 'from' views for the transition
let fromView = tabBarController.selectedViewController!.view
let toView = viewController.view
if fromView == toView {
// If views are the same, then don't do a transition
return false
}
self.view.userInteractionEnabled = false
UIView.transitionFromView(fromView, toView: toView, duration: 2.0, options: .TransitionCrossDissolve, completion: nil)
self.view.userInteractionEnabled = true
return true
}
}
And here is what the issue looks like:
You can try with this code:
import UIKit
class MainTabBarViewController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// Method used to detect when a tab bar button has been tapped
func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
// Creating the 'to' and 'from' views for the transition
let fromView = tabBarController.selectedViewController!.view
let toView = viewController.view
if fromView == toView {
// If views are the same, then don't do a transition
return false
}
self.view.userInteractionEnabled = false
if let window = fromView.window {
let overlayView = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
viewController.view.addSubview(overlayView)
UIView.transitionFromView(fromView, toView: toView, duration: 2.0, options: .TransitionCrossDissolve, completion: { (finish) in
window.rootViewController = viewController
UIView.animateWithDuration(0.4, delay: 0.0, options: .TransitionCrossDissolve, animations: {
overlayView.alpha = 0
}, completion: { (finish) in
overlayView.removeFromSuperview()
})
})
}
self.view.userInteractionEnabled = true
return true
}
}
In my case, call toView.layoutIfNeeded() before the transition fixed the issue.
I'm developing an ios app. I have a a main view and in this view
im trying to present a modal view controller with dimmed background(black with opacity).
The problem is that the status bar is not affected by this color and remains the same.
This is how i present the view controller:
let shareViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ShareViewController") as! ShareViewController
shareViewController.battle = battle
shareViewController.delegate = self
let animation = CATransition()
animation.duration = 1
animation.type = kCATransitionFade
self.view.window?.layer.addAnimation(animation, forKey: kCATransition)
presentViewController(shareViewController, animated: false) {
() in
// nothing here
}
Here are some screenshots to demonstrate the problem:
This is the problem(status bar color):
Problem illustration
This is the modal view in storyboard:
storyboard
I cannot reproduce your problem, the following code works without problems in my single view app:
let viewController = UIViewController()
viewController.modalPresentationStyle = .overFullScreen
viewController.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
let animation = CATransition()
animation.duration = 1
animation.type = kCATransitionFade
self.view.window?.layer.add(animation, forKey: kCATransition)
self.present(viewController, animated: false, completion: nil)
However note that you should be presenting over the root controller of the view. Sometimes you can get strange effects when presenting from your internal controllers:
self.view.window?.rootViewController?.present(viewController, animated: false, completion: nil)
Also make sure you are using the correct modalPresentationStyle.
Set your view controller as the root view controller of a UIWindow, then present the window at the UIWindowLevelAlert level.
Below is a Swift 3 class used to animate a modal popup over all other UI elements, including the status bar. A scrim view is used to shade background UI and intercept touches to dismiss the view.
import UIKit
class ModalViewController: UIViewController {
private let scrimView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.black
view.alpha = 0.0
return view
}()
private var myWindow: UIWindow?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.clear
// Setup scrim View
view.addSubview(scrimView)
view.topAnchor.constraint(equalTo: scrimView.topAnchor).isActive = true
view.leftAnchor.constraint(equalTo: scrimView.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: scrimView.rightAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: scrimView.bottomAnchor).isActive = true
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(dismiss as (Void) -> Void))
scrimView.addGestureRecognizer(tapGestureRecognizer)
// Layout custom popups or action sheets
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.25) {
self.scrimView.alpha = 0.5
// Animate in custom popups or action sheets
}
}
func present() {
myWindow = UIWindow(frame: UIScreen.main.bounds)
myWindow?.windowLevel = UIWindowLevelAlert
myWindow?.backgroundColor = UIColor.clear
myWindow?.rootViewController = self
myWindow?.isHidden = false
}
func dismiss() {
UIView.animate(
withDuration: 0.25,
animations: {
self.scrimView.alpha = 0.0
// Animate out custom popups or action sheets
},
completion: { success in
self.myWindow = nil
}
)
}
}
To present the view:
let modalView = ModalViewController()
modalView.present()
To dismiss the view, tap anywhere on the scrim.
this code works for me, when I am presenting UIViewController with alpha != 1. present UIViewController like:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let destinationVC = storyBoard.instantiateViewController(withIdentifier: "AddComment") as! AddCommentViewController
destinationVC.modalPresentationStyle = .overCurrentContext //this line is important
destinationVC.delegate = self
destinationVC.restId = self.restaurant.id
self.present(destinationVC, animated: true, completion: nil)
then in destinationVC view controller
override func viewWillDisappear(_: Bool) {
UIView.animate(withDuration: 1, animations: { () in
self.view.backgroundColor = .clear
})
super.viewWillDisappear(true)
}
override func viewWillAppear(_: Bool) {
UIView.animate(withDuration: 1, animations: { () in
self.view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
})
super.viewWillAppear(true)
}
and set its backgroundColor to .clear in viewDidLoad or storyboard. So UIViewController covers whole screen including status bar.
Here is the solution you might be looking for:
if let window = UIApplication.shared.keyWindow {
window.windowLevel = UIWindowLevelStatusBar + 1
}
The main idea behind this code is, window of your application has a window level which is lower than status bar window level. And what this code does is, just put your window's window level higher than status bar window level, and your window can now cover the status bar. Don't forget, this code has to be called on main thread, just before presenting your view controller. Good luck!
Custom animation transitions should be performed using UIViewControllerAnimatedTransitioning. Here is a tutorial for this purpose:
https://www.raywenderlich.com/110536/custom-uiviewcontroller-transitions
If all you want is a fade animation you can have it by changing the modalTransitionStyle property of the viewController you are going to display.
Try by fixing your code this way:
guard let shareViewController = self.storyboard?.instantiateViewControllerWithIdentifier("ShareViewController") as! ShareViewController else {
//Fallback in case of nil?
return
}
shareViewController.modalTransitionStyle = .crossDissolve
presentViewController(shareViewController, animated: true, completion: nil)
Also please note that presentViewController(shareViewController, animated: true, completion: nil) is for swift 2. The equivalent swift 3 would be present(shareViewController, animated: true, completion: nil)
you can add this code to view controller for Swift 3:
let statusView: UIView = UIView(frame: CGRect(x: 0.0, y: -20.0, width: UIScreen.main.bounds.size.width, height: 20.0))
statusView.backgroundColor = UIColor.black
statusView.alpha = 0.8
self.addSubview(self.statusView)
You could be extremely practical and simply hide the status bar when your modal view controller is up:
override func prefersStatusBarHidden() -> Bool {
return true
}
I encountered a strange bug. I am just using iOS's custom transitioning method for UIViewControllers using UIViewControllerTransitioningDelegate together with an implementation of UIViewControllerAnimatedTransitioning. It all seems to work fine, until I do exactly the following:
open the app
present another view controller with my custom transition
rotate to landscape
dismiss the just presented view controller
That's all! What happens now is the following: I see a large black bar on the right side of the initial view controller (as if that controller's view wasn't rotated to landscape).
The funny thing is this only goes wrong in iOS 9, in iOS 8 everything seems to work just fine. Did anything change with custom transition API I don't know of? Or is this simply a really nasty iOS 9 bug? If anyone can tell me what I did wrong or if anyone can provide me with a workaround I would really appreciate that!
These classes reproduce the problem:
import UIKit
class ViewController: UIViewController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap")
view.addGestureRecognizer(tapGestureRecognizer)
}
func tap() {
let controller = ModalViewController()
controller.transitioningDelegate = self
presentViewController(controller, animated: true, completion: nil)
}
func animationControllerForPresentedController(presented: UIViewController,
presentingController presenting: UIViewController,
sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transitioning()
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return Transitioning()
}
}
The presented view controller:
import UIKit
class ModalViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.redColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "tap")
view.addGestureRecognizer(tapGestureRecognizer)
}
func tap() {
dismissViewControllerAnimated(true, completion: nil)
}
}
And finally the UIViewControllerAnimatedTransitioning implementation:
import UIKit
class Transitioning: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
let containerView = transitionContext.containerView()
if let fromView = fromView, toView = toView {
containerView?.addSubview(fromView)
containerView?.addSubview(toView)
toView.alpha = 0
UIView.animateWithDuration(0.5, animations: {
toView.alpha = 1
}, completion: {
finished in
transitionContext.completeTransition(true)
})
}
}
}
I generally use the following in animateTransition:
toView.frame = fromView.frame
FYI, you don't have to add fromView to the hierarchy, as it's already there.