iOS UI布局-回到顶部
回到顶部,是比较常用的一个效果
核心代码
在ViewDidLoad中,添加回到顶部按钮
计算偏移量,如果当前显示的内容全部向上隐藏掉,则显示“回到顶部”按钮
//
// ViewController.m
// 回到顶部
//
// Created by Apple on 15/11/4.
// Copyright © 2015年 Apple. All rights reserved.
// #import "ViewController.h" @interface ViewController () <UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) UIButton * topBtn;
@property (nonatomic, assign) CGFloat lastContentOffset;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:_tableView]; // 添加回到顶部按钮
_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
{
static NSString *CMainCell = @"CMainCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CMainCell];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CMainCell];
} cell.textLabel.text =[NSString stringWithFormat:@"测试广本%u",indexPath.row];
return cell;
} // MARK: 计算偏移量
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//MARK:列表滑动偏移量计算
CGPoint 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: 点击移动到顶部
- (void)backToTopButton{
[self.tableView setContentOffset:CGPointMake(, ) animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
效果:

源代码下载链接:http://pan.baidu.com/s/1c0c7t3E
iOS UI布局-回到顶部的更多相关文章
- iOS UI布局调试工具
查看ios软件的ui布局有三种: 1.DCIntrospect 这种方式是开源的,我从github上clone下来后运行demo,运行遇到了问题:Xcode cannot run using t ...
- Vue+element UI实现“回到顶部”按钮组件
介绍 这是一个可以快速回到页面顶部的组件,当用户浏览到页面底部的时候,通过点击按钮,可快速回到页面顶部. 使用方法 由于该组件是基于element-UI进行二次封装的,所以在使用该组件时请务必安装el ...
- iOS UI布局-定时器
定时器是比较常用的一个UI控件,在付款.抢购时,经常会使用到.提取成一个通用的方法 /** * 倒计时GCD通用方法 * 通常用的计时器都是用NSTimer,但是NSTimer在线程很吃紧的时候效果不 ...
- iOS UI布局总结
布局就是尺寸和位置的设置. 一.基本布局: 1)绝对布局:frame.layoutsubviews. 二.相对布局: autoresizing.autolayout.基于父视图.基于约束. 三.线性布 ...
- iOS UI布局-VFL语言
什么是VFL语言 VFL(Visual Format Language),“可视化格式语言”. VFL是苹果公司为了简化autolayout的编码而推出的抽象语言. 语法说明 H:[cancelBut ...
- iOS开发~UI布局(一)初探Size Class
随着iOS8系统的发布,一个全新的页面UI布局概念出现,这个新特性将颠覆包括iOS7及之前版本的UI布局方式,这个新特性就是Size Class.Size Class配合Auto Layout可以解决 ...
- Android商城开发系列(五)—— 商城首页回到顶部和搜索框布局实现
今天我们来开发商城的首页[输入搜索框]布局和点击右下角图片回到顶部的效果 搜索功能在App中很常见,尤其是在商城类的项目当中,一般都会提供很强大的搜索功能,App的搜索布局一般都是在App的顶部,如下 ...
- iOS 8 UI布局 AutoLayout及SizeClass(二)
一.新特性Size Class介绍 随着iOS8系统的公布,一个全新的页面UI布局概念出现,这个新特性将颠覆包含iOS7及之前版本号的UI布局方式,这个新特性就是Size Class. Size Cl ...
- iOS点击状态栏回到顶部底层实现原理
在iOS开发中,苹果其实已经帮你实现了点击状态栏回到顶部这个功能,但我们在开发中会遇到点击不能回到顶部.其实这都和 ScrollView中的一个属性scrollsToTop有关,我们先看看苹果关于这个 ...
随机推荐
- {python之IO多路复用} IO模型介绍 阻塞IO(blocking IO) 非阻塞IO(non-blocking IO) 多路复用IO(IO multiplexing) 异步IO(Asynchronous I/O) IO模型比较分析 selectors模块
python之IO多路复用 阅读目录 一 IO模型介绍 二 阻塞IO(blocking IO) 三 非阻塞IO(non-blocking IO) 四 多路复用IO(IO multiplexing) 五 ...
- 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)
We define the smallest positive real number as the number which is explicitly greater than zero and ...
- 在计算机通信中,可靠交付应当由谁来负责?是网络还是端系统? 网络层协议 MAC帧、IP数据报、TCP报文 关系 IP地址与硬件地址 链路层与网络层
小结: 1. 网络层两种服务 虚电路服务 virtual circuit 电信网 网络层负责可靠交付 数据报服务 网络层不负责可靠交付 提供灵活的.无连接的.尽最大努力交付的数据报服务 不提供服务 ...
- redis分页获取数据
php代码: 采用哈希类型存储数据,有序集合存储分页数据,进行倒序与正序的排序. $getGoodsInfo = M('goods_test')->select(); for($i=0;$i&l ...
- oracle 监听报错the information provided for this listener is currently in use by other software on this computer
use another port number: the information provided for this listener is currently in use by other sof ...
- tensorflow入门笔记(三) tf.GraphKeys
tf.GraphKeys类存放了图集用到的标准名称. 该标准库使用各种已知的名称收集和检索图中相关的值.例如,tf.Optimizer子类在没有明确指定待优化变量的情况下默认优化被收集到tf.Grap ...
- es中如何定位不合法搜索
GET /test_index/test_type/_validate/query?explain{ "query": { "math": { "te ...
- 【SQL】group by 和order by 的区别。
group by 分组,比如group by name.那么重复name就显示一遍,即同样内容归类显示一遍. group by ……having……——(where 后不可以接聚合函数,而having ...
- (1.14)mysql锁问题之MyIsam
1.mysql锁概述 BDB被InnoDB代替了,MyIsam在8.0也被抛弃了 2.MyIsam表锁(读写是串行的) [2.1]查看表锁争用情况. MyIsam存储引擎只支持表锁. 查看表锁争用情况 ...
- linux 修改文件内容 vi命令
vi编辑器是所有Unix及Linux系统下标准的编辑器,介绍一下它的用法和一小部分指令.由于对Unix及Linux系统的任何版本,vi编辑器是完全相同的,因此您可以在其他任何介绍vi的地方进一步了解它 ...