Reputation:
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
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
Reputation: 38162
You need to follow this:
Upvotes: 1
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
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