Back to the top, is a more commonly used effect
Core code
In Viewdidload, add back to the top button
Calculates the offset and displays the back to top button if the currently displayed content is all up hidden
////VIEWCONTROLLER.M//back to top////Created by Apple on 15/11/4.//copyright©2015 year Apple. All rights reserved.//#import "ViewController.h"@interfaceViewcontroller () <UITableViewDelegate,UITableViewDataSource>@property (nonatomic, strong) UITableView*TableView, @property (nonatomic, strong) UIButton*topbtn; @property (nonatomic, assign) cgfloat Lastcontentoffset;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Self.tableview = [[UITableView alloc] Initwithframe:cgrectmake (0,0, Self.view.frame.size.width, Self.view.frame.size.height)]; Self.tableview.Delegate=Self ; Self.tableView.dataSource=Self ; [Self.view Addsubview:_tableview]; //add back to top button_TOPBTN =[UIButton Buttonwithtype:uibuttontypecustom]; _topbtn.frame= CGRectMake (self.view.frame.size.width- -, self.view.frame.size.height- -, +, +); [_topbtn setbackgroundimage:[uiimage imagenamed:@"nearby_return_top_btn"] Forstate:uicontrolstatenormal]; [_topbtn addtarget:self Action: @selector (Backtotopbutton) forcontrolevents:uicontroleventtouchupinside]; _topbtn.clipstobounds=YES; _topbtn.hidden=YES; [Self.view addsubview:_topbtn];}-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{return -;}-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{StaticNSString *cmaincell =@"Cmaincell"; UITableViewCell*cell =[TableView Dequeuereusablecellwithidentifier:cmaincell]; if(Cell = =Nil) {Cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstylesubtitle Reuseidentifier:cmaincell]; } Cell.textLabel.text=[nsstring stringWithFormat:@"test wide Ben%u", Indexpath.row]; returncell;}//MARK: Calculate offset- (void) Scrollviewdidscroll: (Uiscrollview *) scrollview{//MARK: List slip offset calculationCgpoint point =[Self.tableview Contentoffset]; if(Point.y >=self.tableView.frame.size.height) {Self.topBtn.hidden=NO; [Self.view BringSubviewToFront:self.topBtn]; } Else{Self.topBtn.hidden=YES; }}//MARK: Click to move to the top- (void) backtotopbutton{[Self.tableview setcontentoffset:cgpointmake (0,0) Animated:yes];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end
Effect:
Source code Download Link: Http://pan.baidu.com/s/1c0c7t3E
IOS UI layout-back to top