Floating heading below UINavigationBar as a sub-heading - getting the touches?

I have a floating title UIViewbelow UINavigationBar. I inserted it as a preview in index 0, so I can animate it and according to the contentOffset from UITableView.

However, since it goes beyond UINavigationBar, I cannot receive touch events in this view after adding to it UITapGestureRecognizer. All touch events go to UITableViewbelow.

Any ideas if it is possible to have a preview outside the borders of the navigation bar and receive touch events for it?

I did this because adding it as a subview in UITableView, I would need to set the beginning of Y based on the contentOffset while scrolling, and this also makes the animation very complicated, since the original Y changes while scrolling, so I cannot know where revive him.

It looks like the title of a Facebook app with the Status, Photos, and Check buttons.

thank

+4
source share
4 answers

You need to create a custom UINavigationBar and redefine pointInside:withEvent:it to expand the visibility of the navigation bar (add a viewport):

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect extendedRect = self.frame;
    extendedRect.size.height += MENU_HEADER_HEIGHT - self.frame.origin.y;
    BOOL pointInside = NO;
    if (CGRectContainsPoint(extendedRect, point)) {
        pointInside = YES;
    }
    return pointInside;
}
+2
source

Adding to the answer above. Here is what I did.

class CustomNavigationBar: UINavigationBar {

    override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        if(self.pointInside(point, withEvent: event)){
            self.userInteractionEnabled = true
        }else{
            self.userInteractionEnabled = false
        }

        return super.hitTest(point, withEvent: event)
    }

    override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool {
        var extendedRect:CGRect = self.frame
        extendedRect.size.height += 100.0 - self.frame.origin.y
        var pointInside:Bool = false

        if(CGRectContainsPoint(extendedRect, point)){
            pointInside = true
        }
        return pointInside
    }
}
+1

, . , :

-, UITableView, , Y- contentview contentOffset. , , y, - :

[UIView animateWithDuration:0.4
                 animations:^{
                     _myHeaderView.frame = CGRectOffset(_myHeaderView.frame, 0.0, -_myHeaderView.frame.size.height);
                 }];

, , , UINavigationBar. , , UINavigationBar ( UINavigationBar) , .

0

Swift 4:

class TappableNavigationBar: UINavigationBar {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        isUserInteractionEnabled = self.point(inside: point, with: event)
        return super.hitTest(point, with: event)
    }

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        var extendedRect = frame
        extendedRect.size.height += 300.0 - frame.origin.y
        return extendedRect.contains(point)
    }
}
0
source

All Articles