简体   繁体   中英

Not able to animate a UIView which is a subview of UIWindow

I have created a custom class where I getting a parentViewController and a cardViewController . Then I am adding a blackView (with 0.4 alpha) on a keyWindow and on that keyWindow I am also adding the cardViewController.view.

The thing is that it shows up initially but the interaction / swipe gestures doesn't work on the card view.

Here is the code:

    window = UIApplication.shared.keyWindow!

    blackView.backgroundColor = UIColor.black
    blackView.alpha = 0.4
    window.addSubview(blackView)
    blackView.frame = window.frame

    blackView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTapRecognizer)))

    window.addSubview(cardViewController.view)

    card.translatesAutoresizingMaskIntoConstraints = false
    cardHeightConstraint = card.bottomAnchor.constraint(equalTo: window.bottomAnchor, constant: containerView.bounds.height - (self.headerHeight ?? 0))
    card.leadingAnchor.constraint(equalTo: window.leadingAnchor).isActive = true
    card.trailingAnchor.constraint(equalTo: window.trailingAnchor).isActive = true
    card.heightAnchor.constraint(equalToConstant: containerView.bounds.height).isActive = true
    cardHeightConstraint.isActive = true

The above code is called in the initialization of the class. This is what I see:

在此处输入图像描述

If I don't add the blackView, and don't do anything related to UIWindow and just simply add the cardViewController as a child to parentViewController, all the animations and card gestures work as it should. Only when the card gets added to a UIWindow is when the animations and swipe gesture on the card view doesn't work.

What should I do about it? Help will be appreciated. (Using Xcode 11, iOS 13+)

You should not use UIWindow for anything other than setting an initial RootViewController on it unless you are creating an app that has some external projection to a different screen.

You should only set UIWindows if you are trying to use the Programmatic approach either in your AppDelegate.swift pre XCode 11

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    window = UIWindow(frame: UIScreen.main.bounds)
    window?.makeKeyAndVisible()
    window?.rootViewController = TabBarViewController()

    return true
}

It is not quite clear what you are trying to achieve with your CardViewController. But if you have created a custom View then you should subclass UIView instead of UIViewController then use view.addSubview(yourCustomUIView) in a corresponding viewController.

If you could edit your question what exactly you are trying to achieve and show more of your code. We could help better. Is the code you included from your AppDelegate??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM