Custom Subview added on TabBarController is not tappable

271 Views Asked by At

I have a custom view that I'm trying to add as a footer to my TabBarController, meaning resting right on top of the TabBar. I'm able to make the custom view show up, however, nothing on the custom view is tappable. It's like the custom view has isUserInteractionEnabled set to false but I know from Storyboard and debugging my code that the view and all of it's subviews have isUserInteractionEnabled set to true. The reason I say that it acts like isUserInteractionEnabled is set to false because the buttons underneath the custom view from the TabBarController's root view controllers are being tapped instead of the custom view that is on top as far as view hierarchy. I've tried solutions such as making the isUserInteractionEnabled set to false before I add the subview to the TabBarController. I've tried to check if the subview is truly on top and within the bounds of the TabBarController and it is. I'm at a stopping point right now and I would really appreciate some help. The only clues I have that might be making it not tappable is the way I'm initializing my custom view which is

static func nibInstance() -> CustomView {
    let nibView = Bundle.main.loadNibNamed(String(describing:(self)), owner: nil, options: nil)?.first as! CustomView
    return nibView
}

Or that I initialize it in the TabBarController as a global variable then add it in the ViewDidAppear

import UIKit

class CustomTabBarController: UITabBarController {

    let footer = CustomView.nibInstance()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        if !self.footer.isDescendant(of: self.view) {
            self.view.superview!.addSubview(self.footer)
            self.footer.translatesAutoresizingMaskIntoConstraints = false
            self.footer.frame.size.width = self.view.frame.width
            self.footer.leadingAnchor.constraint(equalTo: self.footer.superview!.leadingAnchor, constant: 0)
            NSLayoutConstraint.activate([self.footer.leadingAnchor.constraint(equalTo: self.footer.superview!.leadingAnchor, constant: 0), self.footer.trailingAnchor.constraint(equalTo: self.footer.superview!.trailingAnchor, constant: 0)])
            NSLayoutConstraint.activate([self.footer.bottomAnchor.constraint(equalTo: self.tabBar.topAnchor, constant: 0)])
        }
    }

}

EDIT: So even if I add the custom view to a single view controller, the custom view is still not tappable either.

Here's the link to the Github repo for my project: https://github.com/ovais-panjwani/TapCustomView and I've attached screenshots of the storyboard, customView, and iPhone SE simulator screenshot so it's easier to understand. If you have any other questions feel free to reach out. Storyboard

CustomView

iPhoneSEScreenshot

0

There are 0 best solutions below