{"id":150,"date":"2012-07-18T20:48:15","date_gmt":"2012-07-18T18:48:15","guid":{"rendered":"http:\/\/andras.palfi.hu\/?p=150"},"modified":"2012-07-18T20:48:15","modified_gmt":"2012-07-18T18:48:15","slug":"uitableview-scroll-indicator-background","status":"publish","type":"post","link":"http:\/\/andras.palfi.hu\/?p=150","title":{"rendered":"Using the background of a UITableView as a scroll indicator"},"content":{"rendered":"<p>\t\t\t\tThere is a fancy solution to present the scrolling position of a tableview to the user using moving image as a background. The user can better imagine the size of the table view because the background image suggest where he\/she is in the tableview.<\/p>\n\n<h3><\/h3>\n<h2>How-To &#8211; short version<\/h2>\n<ol>\n<li>Add a tableview<\/li>\n<li>create a scrollview with an imageview which size and contentSize is the same as the tableview<\/li>\n<li>add the scrollview as a background view of the tableview<\/li>\n<li>set the tableview to transparent<\/li>\n<li>Good To Know: the TableView is a ScrollView so you can handle all ScrollViewDelegate related methods. Just simply implement the ScrollView delegate methods where you implemented the TableViewDelegate methods: catch the tableview&#8217;s scrolling event and position the scroll position of the background scrollview accordingly<\/li>\n<\/ol>\n<h2>How-To &#8211; detailed version<\/h2>\n<div><\/div>\n<div>The sample source uses ARC.<\/div>\n<p>First, in the <em>ViewDidLoad<\/em> of your <em>ViewController<\/em> load the appropriate image, create an image view which presents it \u00a0and add the ImageView as the subview of your ScrollView:<\/p>\n<pre class=\"lang:objc decode:true\" title=\"Create the scroll view with the image\">- (UIView *)createBackgroundScrollViewWithFrame:(CGRect)tableViewFrame {\n\n    \/\/ set up the image view, select a long image\n    UIImage *img = [UIImage imageNamed:kImageName];\n\n    \/\/ create image view with the image, the size should be long enough for the scroll\n    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];\n    imgView.contentMode = UIViewContentModeScaleToFill;\n    imgView.frame = CGRectMake(0,0, tableViewFrame.size.width, img.size.height);\n\n    \/\/ set up the background scroll view and add the image view\n    UIScrollView *backgroundScrollView = [[UIScrollView alloc] initWithFrame:tableViewFrame];\n    backgroundScrollView.userInteractionEnabled = NO;\n    backgroundScrollView.contentSize = imgView.frame.size;\n    [backgroundScrollView addSubview:imgView];\n\n    return backgroundScrollView;\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Second, set the ScrollView as the backgroundView of your tableView:<\/p>\n<pre class=\"lang:objc decode:true\">    \/\/ set the scroll view as the background view of the tableview\n    myTableView.backgroundView = [self createBackgroundScrollViewWithFrame:theTableView.bounds];<\/pre>\n<p>Third, don&#8217;t forget to set the background color of the cells to clearColor if you use the tableView in grouped mode:<\/p>\n<pre class=\"lang:objc decode:true\">cell.backgroundColor = [UIColor clearColor];<\/pre>\n<p>Fourth implement the appropriate ScrollViewDelegate method of the TableView (scrollViewDidScroll). Notice that I modified the parameter to UITableView instead of UIScrollView. This is not necessary but makes to code more clear and you don&#8217;t have to cast a lot. We should calculate the correct position by the proportion of the contentSize of the TableView and the ScrollView.<\/p>\n<pre class=\"lang:objc decode:true\">\/\/ cheat a bit: modify the parameter to UITableView from UIScrollView\n- (void)scrollViewDidScroll:(UITableView *)aTableview {\n\n    if (aTableview != myTableView) {\n        return;\n    }\n    UIScrollView *backgroundScrollView = (UIScrollView*)aTableview.backgroundView;\n\n    \/\/ calculate the position of the scroll regarding to the position of the tableview\n    float diffTableView = aTableview.contentSize.height - aTableview.frame.size.height;\n    if (diffTableView&gt;0) {\n        float diffScroll = backgroundScrollView.contentSize.height - backgroundScrollView.frame.size.height;\n        float newOffset = aTableview.contentOffset.y * (diffScroll\/diffTableView);\n\n        backgroundScrollView.contentOffset = CGPointMake(backgroundScrollView.contentOffset.x, newOffset);\n    }\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Notes:\u00a0The image should be long enough to see the correct effect.<\/p>\n<p>If you put a frame around your content you should play with margins and spacings: \u00a0you have to set up a header and footer and indent of each cell.<\/p>\n<p><a href=\"http:\/\/andras.palfi.hu\/wp-content\/uploads\/2012\/07\/usingFrame.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-198\" title=\"usingFrame\" src=\"http:\/\/andras.palfi.hu\/wp-content\/uploads\/2012\/07\/usingFrame-200x300.png\" alt=\"\" width=\"200\" height=\"300\" \/><\/a><\/p>\n<p>If you use sections take care of indenting the sections as well. You can achieve by implementing the \u00a0tableView:viewForHeaderInSection and return a UIView which contains a UILabel indenting appropriately.<\/p>\n<p>You can set a semitransparent background color for each cell and\/or you can use a faded image.<\/p>\n<p>Download the sample project:<\/p>\n<p><a href=\"http:\/\/andras.palfi.hu\/wp-content\/uploads\/2012\/07\/TableViewAutoScrollingBackground.zip\">TableViewAutoScrollingBackground.zip<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a fancy solution to present the scrolling position of a tableview to the user using moving image as a background. The user can better imagine the size of the table view because the background image suggest where he\/she is in the tableview. How-To &#8211; short version Add a tableview create a scrollview with [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"hide_page_title":"","footnotes":""},"categories":[3,4],"tags":[],"_links":{"self":[{"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/150"}],"collection":[{"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=150"}],"version-history":[{"count":0,"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=\/wp\/v2\/posts\/150\/revisions"}],"wp:attachment":[{"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=150"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=150"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/andras.palfi.hu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=150"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}