Bobby Redjeans
Bobby Redjeans

Reputation: 599

Swift background color while pushing view controller

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 whiteenter image description here

Upvotes: 3

Views: 1789

Answers (4)

Casey
Casey

Reputation: 6701

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)!

gif

Upvotes: 7

Enricoza
Enricoza

Reputation: 1142

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.

Upvotes: 0

Thijs van der Heijden
Thijs van der Heijden

Reputation: 1176

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.

Upvotes: 0

dasdom
dasdom

Reputation: 14063

You can style the transition as you like if you implement the protocol UIViewControllerAnimatedTransitioning. Here is an example how this can look like:

https://github.com/dasdom/DDHCustomTransition/blob/master/DDHCustomTransition/Transition/CustomTransition.swift

Upvotes: 3

Related Questions