I have two white view controllers. When i try to push a second view controller, i notice a grey background (basically it changes alpha value during transition). Is there any hack to disable this fade? I just want my background being white

- 575
- 1
- 4
- 18
-
Have you checkt what the background color of the window is? – dasdom Dec 24 '19 at 21:59
-
Both viewcontrollers have white color. window.backgroundColor is nil by default, i've tried to changed it but it's not work – Bobby Redjeans Dec 24 '19 at 22:11
-
You could set the other view controller to start with the color you like but when it starts to transition set that view to white – Evan Mar 11 '20 at 21:59
-
Have you tried my solution? I'm quite confident that this works. – dasdom Mar 11 '20 at 22:31
4 Answers
Using a UIViewControllerAnimatedTransitioning
would definitely be the "correct" way, but it seems you really want to not use it.
If you want to "hack" it, then swizzling is the way to go.
Here is an extension on UIView
that prevents the underlying class _UIParallaxDimmingView
from being displayed.
extension UIView {
static func preventDimmingView() {
guard let originalMethod = class_getInstanceMethod(UIView.self, #selector(addSubview(_:))), let swizzledMethod = class_getInstanceMethod(UIView.self, #selector(swizzled_addSubview(_:))) else { return }
method_exchangeImplementations(originalMethod, swizzledMethod)
}
static func allowDimmingView() {
guard let originalMethod = class_getInstanceMethod(UIView.self, #selector(addSubview(_:))), let swizzledMethod = class_getInstanceMethod(UIView.self, #selector(swizzled_addSubview(_:))) else { return }
method_exchangeImplementations(swizzledMethod, originalMethod)
}
@objc func swizzled_addSubview(_ view: UIView) {
let className = "_UIParallaxDimmingView"
guard let offendingClass = NSClassFromString(className) else { return swizzled_addSubview(view) }
if (view.isMember(of: offendingClass)) {
return
}
swizzled_addSubview(view)
}
}
I would recommend using it along the lines of this:
class SomeViewController: UIViewController {
func transition(to viewController: UIViewController) {
navigationController?.delegate = self
navigationController?.pushViewController(viewController, animated: true)
}
}
extension SomeViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
UIView.preventDimmingView()
}
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
UIView.allowDimmingView()
}
}
Of course if you want this to make it through App Store review, you will likely get flagged for the "_UIParallaxDimmingView"
string. I would recommend initializing it from a byte array instead:
let className = String(bytes: [95, 85, 73, 80, 97, 114, 97, 108, 108, 97, 120, 68, 105, 109, 109, 105, 110, 103, 86, 105, 101, 119], encoding: .utf8)!

- 6,531
- 24
- 43
-
This is great! Thank you, upvoted. Do you know of any examples with the custom transition? – Kyle Browning Mar 26 '20 at 22:04
You can style the transition as you like if you implement the protocol UIViewControllerAnimatedTransitioning
. Here is an example how this can look like:

- 13,975
- 2
- 47
- 58
-
Seems like this thing is breaking swipe left gesture. Basically, i'm fine with standard transition, i just want to set background color to white – Bobby Redjeans Dec 24 '19 at 21:30
-
You can implement swipe left gesture with that. Just an idea because right now I don't have another solution. – dasdom Dec 24 '19 at 21:58
I think you'll need to do it using a custom UIViewControllerAnimatedTransitioning
as dasdom said. You can just create a simple transition where you move the new view controller over the old one. This way the old view controller won't change color as it does with the normal transition. Adding a left swipe to go back shouldn't be too difficult as there isn't much going on animation wise.

- 1,147
- 1
- 10
- 25
-
Yeah not an acceptable to answer to re-implement back swipe gesture in my opinion. – Kyle Browning Mar 11 '20 at 22:17
-
I don't think there really is any other way. This is just the default transition animation. Don't think there is a way to change that. – Thijs van der Heijden Mar 11 '20 at 22:18
-
Like explained here, on stackoverflow: https://stackoverflow.com/questions/2215672/how-to-change-the-push-and-pop-animations-in-a-navigation-based-app – Thijs van der Heijden Mar 11 '20 at 22:20
-
Right, this bounty is for seeing if there is any other way rather than all that work. – Kyle Browning Mar 11 '20 at 22:28
Actually there is this pod you could use.
Just install it and use EZNavigationController instead of UINavigationController and by default the behavior should just be the one you want.
You could also pass your own animator without overriding any sort of swipe gesture if you need to change it a little.
PS: this pod was not intended specifically for your request, but luckily should just work for you.
PpS: This pod will also add a pan-to-pop gesture from the middle of the screen (much like instagram). Actually this is the main purpose of the library, so that may be not what you want, but I hope it can help anyway.

- 1,101
- 6
- 18