如果直接在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. iOS 在tableView上添加button导致按钮没有点击效果和不能滑动的 zhuang

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

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

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

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

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

  5. iOS实现类似QQ的好友列表,自由展开折叠(在原来TableView的基础上添加一个字典,一个Button)

    //直接代码 只包含 折叠展开字典的处理搭建#import "CFViewController.h" @interface CFViewController ()<UITab ...

  6. 微信小程序添加悬浮在线客服会话按钮

    微信为小程序提供客服消息能力,小程序用户可以方便快捷地与小程序服务提供方进行沟通,并且已经做成了组件的形式,直接就可以调用.客服会话按钮,用于在页面上显示一个客服会话按钮,用户点击该按钮后会进入客服会 ...

  7. 127使用 TableView 自带的单元格样式实现好友列表,另外在单元格中添加辅助按钮

    类似的做法如之前这篇随笔:114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果) 相比之下:自定义 UITableViewCell 的内容灵活,可根据需求 ...

  8. Android UI ActionBar功能-在 Action Bar 上添加按钮

    在ActionBar上添加按钮实现某些功能最常见的Application的功能如:在ActionBar上添加一个搜索按钮: 首先官方文档说明:http://wear.techbrood.com/tra ...

  9. Android标题栏上添加多个Menu按钮

    最近项目中碰到要在Android Menu旁边再添加一个按钮,而不是点击menu按钮然后在弹出一些选项. MainActivity代码: public class MainActivity exten ...

随机推荐

  1. zyUpload+struct2完成文件上传

    前言: 最近在写自己的博客网站,算是强化一下自己对s2sh框架的理解.期间遇到了很多问题,这些问题在写之前都考虑过,感觉也就是那样吧.但正真遇到了,也挺让人难受的.就利用zyUpload这个js插件实 ...

  2. JQuery EasyUI datagrid 复杂表头处理

    下面是我项目中和网上收集的EasyUI的表头处理的,合适的拿去用吧. 例子1: $('#day_health').datagrid({                 url: "sqb_b ...

  3. Objective-C中的继承和多态

    面向对象编程之所以成为主流的编程思想和他的继承和多态是分不开的,只要是面向对象语言都支持继承和多态,当然不同的OOP语言之间都有其特点.OC中和Java类似,不支持多重继承,但OOP语言C++就支持多 ...

  4. 【记录】vmware fusion 7 windows 10 unidentified network

    今天在 vmware fusion 7 中,使用 windows 10 时,突然报出一个错误(忘记截图了),当时就要求强制重启系统,也没怎么在意,但是重启之后,发现 windows 10 居然不能联网 ...

  5. beego上传文件

    html代码: <form id="fform" method="POST" enctype="multipart/form-data" ...

  6. Java 线程池框架核心代码分析--转

    原文地址:http://www.codeceo.com/article/java-thread-pool-kernal.html 前言 多线程编程中,为每个任务分配一个线程是不现实的,线程创建的开销和 ...

  7. PowerPoint基础

    一.基础 默认后缀ppt,pptx office2003和以后的版本只支持ppt, 可以将pptx另存为ppt97-2003 二.修改PPT尺寸 三.新建幻灯片 四.字体与段落设置 五.主题与字体 六 ...

  8. 1Z0-053 争议题目解析304

    1Z0-053 争议题目解析304 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 304.What privileges must be granted to allow an a ...

  9. jquery.mobile手机网页简要

    先上最终效果: 最近做了一个用手机浏览器访问的web应用,采用较流行的HTML5,为了提高开发效率节省时间决定采用现有开源框架,免去了自己做设计与兼容性. 一些比较优秀的框架:10大优秀的移动Web应 ...

  10. Visual Studio Code 1.0发布,支持中文在内9种语言

    Visual Studio Code 1.0发布,支持中文在内的9种语言:Simplified Chinese, Traditional Chinese, French, German, Italia ...