Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS : UIView with subview Button click event not firing when zPosition of view changes

Tags:

ios

swift

I am using parent view controller and child view controller in my app. In which parent view controller contains subview as button with zPosition value of 2.

Now, i am adding child view controller to parent as below,

    func addChildViewController(){

        let storyboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
        let childVC = storyboard.instantiateViewController(withIdentifier: "childVC") as! ChildViewController        
        addChildViewController(childVC)
        self.view.addSubview(childVC.view)
        childVC.didMove(toParentViewController: self)
     }

Button subview is visible at top of child view controller, but click event is not firing.

Note : I am not adding button as subview on child view controller, only at parent view controller.

like image 716
Raju Avatar asked Oct 29 '22 11:10

Raju


1 Answers

Is the childVC covering the button? - You need to make sure that the subview with the button in it is moved to the front after you add the childVC.

func addChildViewController(){
    let storyboard = UIStoryboard(name: "myStoryBoard", bundle: nil)
    let childVC = storyboard.instantiateViewController(withIdentifier: "childVC") as! ChildViewController        
    addChildViewController(childVC)
    self.view.addSubview(childVC.view)
    childVC.didMove(toParentViewController: self)

    // Bring button subview to front
    self.view.bringSubviewToFront(SubViewWithButtonIn)

}
like image 191
Wez Avatar answered Nov 15 '22 05:11

Wez