如果直接在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. SVM算法

    本文主要介绍支持向量机理论推导及其工程应用. 1 基本介绍 支持向量机算法是一个有效的分类算法,可用于分类.回归等任务,在传统的机器学习任务中,通过人工构造.选择特征,然后使用支持向量机作为训练器,可 ...

  2. 利用Shell脚本将MySQL表中的数据转化为json格式

    脚本如下: #!/bin/bash mysql -s -phello test >.log <<EOF desc t1; EOF lines="concat_ws(',', ...

  3. 跌倒了,再爬起来:ASP.NET 5 Identity

    "跌倒了"指的是这一篇博文:爱与恨的抉择:ASP.NET 5+EntityFramework 7 如果想了解 ASP.NET Identity 的"历史"及&q ...

  4. 1Z0-053 争议题目解析706

    1Z0-053 争议题目解析706 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 706.You execute the following command to set the ...

  5. JavaScriptSerializer 序列化json 时间格式

    利用JavaScriptSerializer 序列化json 时间格式,得到的DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,显然要进行转换 1.利用字符串直 ...

  6. 从零开始编写属于我的CMS:(一)前言

    一,项目背景 记得大学毕业课题,我就是选择做个CMS,不过当时虽然做了个,不过感觉不是很好,所以现在又重做了,顺便发上来供大家讨论的.虽然CMS不是什么特别的项目,但是还是想从一个普通项目学到更多的东 ...

  7. MVC5 DBContext.Database.SqlQuery获取对象集合到ViewModel集合中(可以利用这个方法给作为前台视图页cshtml页面的@model 源)

    首先我们已经有了一个Model类: using System;using System.Data.Entity;using System.ComponentModel.DataAnnotations; ...

  8. Oracle循环中的EXIT、RETURN、CONTINUE解密

    有时候编写Oracle中用游标等信息去循环处理逻辑的时候,对EXIT.RETURN.CONTINUE很容易搞混淆,网上搜了资料也不是很清楚,所以本人自己写了一小段代码测试了这三种用法.案例代码如下: ...

  9. visual studio 2015 开发android

    转载请注明: http://www.cnblogs.com/sunyl/p/5493249.html http://www.cnblogs.com/sunyl/ 最近有不少新闻, 甲骨文向谷歌索赔93 ...

  10. jquery移除属性值

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...