如何在TableView上添加悬浮按钮
如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法:
1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层
2.使用window
首先看一下最终的效果,在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动

首先介绍上面的第一种方法:
1)创建tableview和底部按钮的属性
//屏幕宽
#define kScreenW [UIScreen mainScreen].bounds.size.width
//屏幕高
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface broadcastLiveViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic) UITableView *livesListTable;
@property(nonatomic) UIButton *bottomButton;
@end
2)创建属性到最顶部
@implementation broadcastLiveViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect clientRect = [UIScreen mainScreen].bounds;
_livesListTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, clientRect.size.width, clientRect.size.height-65) style:UITableViewStylePlain];
[self.view addSubview:_livesListTable];
_livesListTable.delegate = self;
_livesListTable.dataSource = self;
self.bottomButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.bottomButton.frame = CGRectMake(kScreenW - 80, kScreenH - 140, 60, 60);
[self.bottomButton setBackgroundImage:[UIImage imageNamed:@"recordLive"] forState:UIControlStateNormal];
[self.bottomButton addTarget:self action:@selector(onTapLiveBtn) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.bottomButton];
3)实现按钮事件
- (void)onTapLiveBtn
{
NSLog(@"点击底部按钮");
}
接下来介绍第二种方法:
1)创建一个window,button属性避免window被释放
//屏幕宽
#define kScreenW [UIScreen mainScreen].bounds.size.width
//屏幕高
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface broadcastLiveViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(strong,nonatomic)UIWindow *window;
@property(strong,nonatomic)UIButton *button;
@end
2)创建window和button
默认的情况下系统只有一个window这时我们需要设置windowLevel
window不用添加在任何视图上
- (void)createButton{
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button setBackgroundImage:[UIImage imageNamed:@"recordLive"] forState:UIControlStateNormal];
_button.frame = CGRectMake(0, 0, 60, 60);
[_button addTarget:self action:@selector(onTapLiveBtn) forControlEvents:UIControlEventTouchUpInside];
_window = [[UIWindow alloc]initWithFrame: CGRectMake(kScreenW - 80, kScreenH - 80, 60, 60);];
_window.windowLevel = UIWindowLevelAlert+1;
_window.backgroundColor = [UIColor redColor];
_window.layer.cornerRadius = 30;
_window.layer.masksToBounds = YES;
[_window addSubview:_button];
[_window makeKeyAndVisible];//关键语句,显示window
} 3)延时加载window,注意我们需要在rootWindow创建完成之后再创建这个悬浮的按钮
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:@selector(createButton) withObject:nil afterDelay:1];
}
4)实现按钮事件
- (void)onTapLiveBtn
{
NSLog(@"点击底部按钮");
}
注意::最后再添加一个小功能,使tableview上下滑动的时候,按钮动画效果的出现和消失,在这里是上拉消失,下拽出现
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
if (scrollView.contentOffset.y > self.offsetY && scrollView.contentOffset.y > 0) {//向上滑动
//按钮消失
[UIView transitionWithView:self.bottomButton duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButton.frame = CGRectMake(kScreenW - 80, kScreenH - 65, 60, 60);
} completion:NULL];
}else if (scrollView.contentOffset.y < self.offsetY ){//向下滑动
//按钮出现
[UIView transitionWithView:self.bottomButton duration:0.1 options:UIViewAnimationOptionTransitionNone animations:^{
self.bottomButton.frame = CGRectMake(kScreenW - 80, kScreenH - 140, 60, 60);
} completion:NULL];
}
self.offsetY = scrollView.contentOffset.y;//将当前位移变成缓存位移
}
如何在TableView上添加悬浮按钮的更多相关文章
- 在TableView上添加悬浮按钮
如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...
- 如何在MyEclipse上添加更换JRE
如何在myeclipse上添加更换JRE 由于兼容性的问题,有些WEB项目会依赖jdk的版本.如果需要更换jdk,那么,知道如何更换JRE的方法很有必要. 一种在myeclipse上添加和更换JRE的 ...
- iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang
转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...
- 如何在UILable上添加点击事件?
最近开始学习iOS开发,今天上来写第一个iOS笔记 昨天碰到一个需求,在UILable上添加点击事件,网上找了写资料,有人建议用透明的UIButton覆盖,有人建议写一个集成自UILable的类,扩展 ...
- 如何在UITableViewController上添加一个固定的视图
最近在使用UITableViewController,想在上面添加一个固定的视图,不随ScrollView滑动而移动.最后找到2种解决办法,一种是计算TableView的偏移,调整视图的位置,不断更新 ...
- 如何在IDEA上 添加GIT和maven、mybatis插件
IDEA工具上,添加GIT和maven.mybatis插件,相对比较简单: 首先下载GIT.maven.mybatis. 先添加GIT插件: 首先在IDEA找到file中找到setting,然后搜索g ...
- 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS
那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...
- sencha panel的头header上添加刷新按钮
var plet3=Ext.create('portaltest3.view.Portlet', { title: '提醒', ...
- 如何为 Drupal 7 网站添加悬浮的反馈按钮?
最近有客户咨询我们要怎么为 Drupal 网站添加悬浮按钮,方便访客能够链接到反馈表单页面.很幸运,使用 Feedback Simple 模块可以很容易实现. 在这篇短教程中,我将和大家分享如何添加链 ...
随机推荐
- AprioriTID algorithm
What is AprioriTID? AprioriTID is an algorithm for discovering frequent itemsets (groups of items ap ...
- javascript 事件设计模式
http://plkong.iteye.com/blog/213543 1. 事件设计概述 事件机制可以是程序逻辑更加清晰可见,在JavaScript中很多对象都有自己的事件,如:button有onc ...
- C++ new和delete实现原理——new和delete最终调用malloc和free
new和delete最终调用malloc和free,关于malloc和free实现原理参见这篇文章: http://blog.csdn.net/passion_wu128/article/detail ...
- Linux进程间通信——使用命名管道
在前一篇文章——Linux进程间通信——使用匿名管道中,我们看到了如何使用匿名管道来在进程之间传递数据,同时也看到了这个方式的一个缺陷,就是这些进程都由一个共同的祖先进程启动,这给我们在不相关的的进程 ...
- JQ调用后台方法
首先,先在页面上创建一个asp按钮,添加点击事件,把要在前台调用的后台方法写在这个按钮的点击事件中: <span style="display:none;"><a ...
- css中关于transform、transition、animate的区别
写动画经常会用到这几个属性,他们之间有什么区别呢? 1.transform 每每演示transform属性的,看起来好像都是带动画.这使得小部分直觉化思维的人(包括我)认为transform属性是动画 ...
- jQuery.innerWidth() 函数详解
innerWidth()函数用于设置或返回当前匹配元素的内宽度. 内宽度包括元素的内边距(padding),但不包括外边距(margin).边框(border)等部分的高度.如下图: 如果你要获取 ...
- C#VS面向对象基础(二)
这里我们接着上一篇博客,继续学习用C#实现面向对象中的概念.这里学习下边几个,当然我们还是通过动物比赛的例子. 多态:表示不同的对象可以执行相同的动作,但是通过它们自己的实现代码来执行.这里需要将父类 ...
- poj 3411 Paid Roads
题意:有m条路,n座城市,走这些路是要付费的,每条路由两种付费方案,设一条路两端是a,b,如果走完这条路在b点付费的话,应付r,如果走这条路之前在c点付费的话,应付p,求从1端点走到n端点的最小费用. ...
- php学习网址
后面会陆续维护此页. 1. php编程 此博客是网站www.beilei123.cn镜像,转载请注明出处.