如果直接在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上添加悬浮按钮的更多相关文章

  1. 在TableView上添加悬浮按钮

    如果直接在TableVIewController上贴Button的话会导致这个会随之滚动,下面解决在TableView上实现位置固定悬浮按钮的两种方法: 1.在view上贴tableView,然后将悬 ...

  2. 如何在MyEclipse上添加更换JRE

    如何在myeclipse上添加更换JRE 由于兼容性的问题,有些WEB项目会依赖jdk的版本.如果需要更换jdk,那么,知道如何更换JRE的方法很有必要. 一种在myeclipse上添加和更换JRE的 ...

  3. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

    转载请注明出处. 今天在调试代码的时候,在tableviewcell上添加button,发现button快速点击的话,是看不出点击效果的,查找资料发现, ios7上UITableViewCell子层容 ...

  4. 如何在UILable上添加点击事件?

    最近开始学习iOS开发,今天上来写第一个iOS笔记 昨天碰到一个需求,在UILable上添加点击事件,网上找了写资料,有人建议用透明的UIButton覆盖,有人建议写一个集成自UILable的类,扩展 ...

  5. 如何在UITableViewController上添加一个固定的视图

    最近在使用UITableViewController,想在上面添加一个固定的视图,不随ScrollView滑动而移动.最后找到2种解决办法,一种是计算TableView的偏移,调整视图的位置,不断更新 ...

  6. 如何在IDEA上 添加GIT和maven、mybatis插件

    IDEA工具上,添加GIT和maven.mybatis插件,相对比较简单: 首先下载GIT.maven.mybatis. 先添加GIT插件: 首先在IDEA找到file中找到setting,然后搜索g ...

  7. 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

    那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...

  8. sencha panel的头header上添加刷新按钮

    var plet3=Ext.create('portaltest3.view.Portlet',                   { title: '提醒',                   ...

  9. 如何为 Drupal 7 网站添加悬浮的反馈按钮?

    最近有客户咨询我们要怎么为 Drupal 网站添加悬浮按钮,方便访客能够链接到反馈表单页面.很幸运,使用 Feedback Simple 模块可以很容易实现. 在这篇短教程中,我将和大家分享如何添加链 ...

随机推荐

  1. 关于jqueryUI里的拖拽排序

    主要参考http://jsfiddle.net/KyleMit/Geupm/2/  (这个站需要FQ才能看到效果) 其实是jqueryUI官方购物车拖拽添加例子的增强版,就是在拖拽的时候增加了排序 这 ...

  2. linux中grep使用方法具体解释

    查找特定字符串并颜色显示 [root@fwq test]# grep -n 'the' regular_express.txt --color=auto 8:I can't finish the te ...

  3. Django学习笔记(三)—— 型号 model

    疯狂暑期学习 Django学习笔记(三)-- 型号 model 參考:<The Django Book> 第5章 1.setting.py 配置 DATABASES = { 'defaul ...

  4. PAT A 1065. A+B and C (64bit) (20)

    题目 Given three integers A, B and C in [-263, 263], you are supposed to tell whether A+B > C. Inpu ...

  5. win7启动后报丢失nscmk.dll解决解决方式

    1.根据当前计算机选择下载64位或者32位nscmk.dll 2.拷贝nscmk.dll到相路径(32位:%windir%\system32\:64位:%windir%\SysWOW64\nscmk. ...

  6. DevExpress.GridControl.gridView的一些注意

    1.DevExpress控件组中的GridControl控件不能使横向滚动条有效.现象:控件中的好多列都挤在一起,列宽都变的很小,根本无法正常浏览控件单元格中的内容. 解决: gridView1.Op ...

  7. Erp第三章:管理问题与MRP、MRP2、ERP

    1企业管理者经常头疼问题:一.“产供销”严重脱节 .       二.财务数据和业务数据对不上号 2.MRP(物料需求计划,Material Requiremengts Planning),MRP2( ...

  8. js 去重 字符串 [123123,123123,345435,33467,45645,343467,879,45645]

    function unique(dislodgeArr) { var ret = [] var hash = {} var datasource = new Array(); var array= d ...

  9. Fiddler使用教程(收藏)

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...

  10. UVA 1660 Cable TV Network

    题意: 求一个无向图的点连通度. 分析: 把一个点拆成一个入点和一个出点,之间连一条容量为1的有向边,表示能被用一次.最大流求最小割即可.套模板就好 代码; #include <iostream ...