IOS UI layout-back to top

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.