如果直接在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. ASP.NET MVC5 网站开发实践(二) Member区域 - 用户部分(1)用户注册

    上一次把基本框架搭建起来了,这次开始整Web部分,终于可以看到界面了小激动一下.web项目部分从用户功能开始,基本有注册,登录.注销.查找.查看.删除等涉及Member区域和Manage区域. 目录: ...

  2. 关于Android多项目依赖在Eclipse中无法关联源代码的问题解决

    被Eclipse中Android依赖项目无法关联源代码的问题困扰了许久,网上搜索了一下,终于得到解决,大大提高了开发效率. 问题描述: 项目有A,B两个Android Project组成, B是And ...

  3. MySQL学习笔记四:字符集

    1.字符集就是字符和其编码的集合,查看数据库支持的字符集 show character set 2.查看服务端启动时默认的字符集 mysql> show variables like 'char ...

  4. 国内版Office 365和Azure AAD绑定的问题及解决方案

      背景: 1. 开发人员要做的事情,是希望在应用程序里面能调用Office 365 的API(Microsoft Graph)来动态添加Office 365账号 2. 如果需要调用这些API,必须在 ...

  5. Oracle配置数据库诊断

    环境:RHEL 6.4 + Oracle 11.2.0.4 1. 设置ADR 2. 使用Support Workbench 3. 恢复块介质 Reference 1. 设置ADR 1.1 查看v$di ...

  6. 1Z0-053 争议题目解析606

    1Z0-053 争议题目解析606 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 606.Identify the channel settings that can be per ...

  7. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  8. SFC的OAM管理框架

    1.简介SFC Service Function Chain(SFC):一系列服务功能的顺序组合,主要是针对网络中的数据包/帧分类处理,还包括能够动态排序部署的网络功能并且这些网络功能间拓扑结构独立, ...

  9. “全能”选手—Django 1.10文档中文版Part4

    第一部分传送门 第二部分传送门 第三部分传送门 3.2 模型和数据库Models and databases 3.2.2 查询操作making queries 3.3.8 会话sessions 2.1 ...

  10. 【原创】基于日志增量,统计qps,并基于ip排序

    增量统计日志行数(只统计上一秒) dns_qps.py #!/usr/bin/env python #_*_coding:utf-8_*_ import datetime import re impo ...