user5036099
user5036099

Reputation:

Fixed UIImage in UITableView at top

When I add a UIImage at the top of my UITableView with prototype cells, the image won't stay fixed. So when I scroll the UITableView at the top also scrolls with it.

Is there any idea to add the UIImage view as fixed (unmovable)

Thanks!

Upvotes: 4

Views: 743

Answers (4)

Vincent Joy
Vincent Joy

Reputation: 2678

UITableView is a subclass of UIScrollView. So we can use the scrollViewDidScroll method for performing this task.

Fixing imageView at the top is simple. But you need to get the reference of your imageView in scrollViewDidScroll method.

func scrollViewDidScroll(scrollView: UIScrollView) {

    if scrollView==self.tableView {

        var frame = yourImageView.frame

        if scrollView.contentOffset.y > 0 {

            frame.origin.y = scrollView.contentOffset.y
            yourImageView.frame = frame

        } else if scrollView.contentOffset.y == 0 {

            frame.origin.y = 0
            yourImageView.frame = frame
        }
    }
}

Upvotes: 1

Abhinav
Abhinav

Reputation: 38162

You need to follow this:

  1. Keep your baseview as regular UIView.
  2. Add image as subView at say (0,10)
  3. Add a UITableView at (0, 10+margin) position.

Upvotes: 1

Ankita Shah
Ankita Shah

Reputation: 2248

Set image in UIImageView or add as UIView background in UITableView header, by using the following method. Sorry below code is for swift, you will find it for Objective C too.

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

}

Upvotes: 0

sooper
sooper

Reputation: 6039

What you can do is make a UIView subclass and fail the hit-test, thus passing all touch events to the table view underneath which will allow your to use scrolling gestures inside the image view without it moving.

In your implementation:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    UIView *view = [super hitTest:point withEvent:event];

    if (view == self) {

        view = nil;

    }

    return view;
}

Place your UIImageView over the table view i.e. Add it to the table view's superview (make sure user-interaction is enabled and set the class in the storyboard to your subclass.

Upvotes: 0

Related Questions