maxep / MXParallaxHeader

Simple parallax header for UIScrollView
MIT License
1.73k stars 251 forks source link

Button click method not working #36

Closed Gantaios closed 8 years ago

Gantaios commented 8 years ago

Hi thanks for your great control just want to add gestures to the headerviw how can we achieve that please tell me

hongzhou85 commented 8 years ago

Same i cannot detect any gestures or IBAction in the parallax header. How do intercept the touch?

jj-12 commented 8 years ago

Same here. This would be an awesome thing to know. Thanks!

hongzhou85 commented 8 years ago

I have achieved it by storing a reference of the tableViewController or scrollViewController or collectionViewController in the headerView. Then set the buttons' touchGestureRecognizer target to your reference and have the function in there to handle the action. It should works that way.

hongzhou85 commented 8 years ago

For the header:

class HeaderView : UIVIewController {
  @IBOutlet weak var button : UIButton!
  var parent: UIViewController?  // Usually your tableview/scrollview/collectionview

  override viewDidLoad() {
    let tapGestureRecognizer = UITapGestureRecognizer(target:self.parent!, action:#selector(ParentViewCollection.actionGesture(_:)))
        button.userInteractionEnabled = true
        button.addGestureRecognizer(tapGestureRecognizer)
  }
}

For the "parent":

class ParentViewController : UIViewController {
   let header : HeaderView = HeaderView()

   override viewDidLoad() {
        header.parent = self
        self.collectionView!.parallaxHeader.view = header.view
        self.collectionView!.parallaxHeader.height = headerHeight
        self.collectionView!.parallaxHeader.mode = MXParallaxHeaderMode.Fill
        self.collectionView!.parallaxHeader.minimumHeight = 0
   }

   func actionGesture(sender : UIGestureRecognizer) {
        // here you can do what you want with the button
    }
}
maxep commented 8 years ago

This is a good approach @hongzhou85. Instead of setting a parentproperty, you can create a delegate and call that delegate on gesture. Another approach would be to create a floating view in your view controller from storyboard, then to attach this floating view to your parallax header. This would allow you to trigger segues directly from your header view.

jj-12 commented 8 years ago

Great, the floating view seems to work pretty well. Thanks guys!

AmruthaMurthy1512 commented 4 years ago

Great, the floating view seems to work pretty well. Thanks guys!

Any code snippet how you added a floating view. My requirement is that I need to add back button on Top of Parallex header view.

AmruthaMurthy1512 commented 4 years ago

This is a good approach @hongzhou85. Instead of setting a parentproperty, you can create a delegate and call that delegate on gesture. Another approach would be to create a floating view in your view controller from storyboard, then to attach this floating view to your parallax header. This would allow you to trigger segues directly from your header view.

Any code snippet how you added a floating view. My requirement is that I need to add back button on Top of Parallex header view.