dvshrwt
dvshrwt

Reputation: 23

How to hide custom view on tapping outside it

I have made a custom view and is hidden by default. By pressing a button I am unhiding it. Now I want to again hide it by adding outside the custom view. I have used touchesbegan with event but that is not serving the purpose. This is the function.

override func touchesBegan(touches: Set, withEvent event: UIEvent?) {

   let touch = UITouch()

    if touch.view?.tag == 1 {          // 1 is the tag value for custom view

        self.view1.hidden = false     // view1 is the outlet for custom view
    }
    else{
        self.view1.hidden = true
    }
}

Upvotes: 0

Views: 712

Answers (1)

Ankita Shah
Ankita Shah

Reputation: 2248

Add touch to superview of you'r custom view. On it's function call use this

func superView_Touch(){
    let childView = superView.viewWithTag(1)
    if(!childView.hidden){
        childView.hidden = true
    }
}

Upvotes: 0

Related Questions