iOS开发-UIWindow的用法-创建悬浮按钮
来源:互联网 发布:德军总部剧情知乎 编辑:程序博客网 时间:2024/06/05 08:23
UIWindow如何使用?开发中有何用?
我们在开发中可能经常会遇到需要在TableView上使用悬浮按钮的情况,这时 如果直接在TableVIewController上贴Button的话会导致这个会随之滚动
那么我么在TableView上实现悬浮按钮的基本方法有两种:
1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层(我已经写过该文章)
2.使用window
今天我们来使用第2种方法实现悬浮按钮以及介绍UIWindow的详细用法
首先看一下我们要实现的效果:
在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动;
点击该按钮关闭该悬浮窗
实现代码:
(1)创建一个window,button属性避免window被释放
#import "TableViewController.h"@interface TableViewController ()@property(strong,nonatomic)UIWindow *window;@property(strong,nonatomic)UIButton *button;@end
(2)创建window和button
默认的情况下系统只有一个window这时我们需要设置windowLevel
window不用添加在任何视图上
- (void)createButton{ _button = [UIButton buttonWithType:UIButtonTypeCustom]; [_button setTitle:@"悬浮按钮" forState:UIControlStateNormal]; _button.frame = CGRectMake(0, 0, 80, 80); [_button addTarget:self action:@selector(resignWindow) forControlEvents:UIControlEventTouchUpInside]; _window = [[UIWindow alloc]initWithFrame:CGRectMake(100, 200, 80, 80)]; _window.windowLevel = UIWindowLevelAlert+1; _window.backgroundColor = [UIColor redColor]; _window.layer.cornerRadius = 40; _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)关闭window
释放keyWindow即可关闭window
/** * 关闭悬浮的window */- (void)resignWindow{ [_window resignKeyWindow]; _window = nil; }
完整代码如下:
//// TableViewController.m// //// Created by MRBean on 15/5/23.////#import "TableViewController.h"@interface TableViewController ()@property(strong,nonatomic)UIWindow *window;@property(strong,nonatomic)UIButton *button;@end@implementation TableViewController- (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(createButton) withObject:nil afterDelay:1]; }- (void)createButton{ _button = [UIButton buttonWithType:UIButtonTypeCustom]; [_button setTitle:@"悬浮按钮" forState:UIControlStateNormal]; _button.frame = CGRectMake(0, 0, 80, 80); [_button addTarget:self action:@selector(resignWindow) forControlEvents:UIControlEventTouchUpInside]; _window = [[UIWindow alloc]initWithFrame:CGRectMake(100, 200, 80, 80)]; _window.windowLevel = UIWindowLevelAlert+1; _window.backgroundColor = [UIColor redColor]; _window.layer.cornerRadius = 40; _window.layer.masksToBounds = YES; [_window addSubview:_button]; [_window makeKeyAndVisible];//关键语句,显示window}/** * 关闭悬浮的window */- (void)resignWindow{ [_window resignKeyWindow]; _window = nil; }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath]; return cell;}@end
可以看到,有了window任何 神马的 悬浮按钮悬浮窗都是 浮云啦....
希望本文能对你有所帮助,更多文章: http://blog.csdn.net/yangbingbinga
UIWindow如何使用?开发中有何用?
我们在开发中可能经常会遇到需要在TableView上使用悬浮按钮的情况,这时 如果直接在TableVIewController上贴Button的话会导致这个会随之滚动
那么我么在TableView上实现悬浮按钮的基本方法有两种:
1.在view上贴tableView,然后将悬浮按钮贴在view的最顶层(我已经写过该文章)
2.使用window
今天我们来使用第2种方法实现悬浮按钮以及介绍UIWindow的详细用法
首先看一下我们要实现的效果:
在tableViewController上添加一个悬浮按钮,该按钮不能随着视图的滚动而滚动;
点击该按钮关闭该悬浮窗
实现代码:
(1)创建一个window,button属性避免window被释放
#import "TableViewController.h"@interface TableViewController ()@property(strong,nonatomic)UIWindow *window;@property(strong,nonatomic)UIButton *button;@end
(2)创建window和button
默认的情况下系统只有一个window这时我们需要设置windowLevel
window不用添加在任何视图上
- (void)createButton{ _button = [UIButton buttonWithType:UIButtonTypeCustom]; [_button setTitle:@"悬浮按钮" forState:UIControlStateNormal]; _button.frame = CGRectMake(0, 0, 80, 80); [_button addTarget:self action:@selector(resignWindow) forControlEvents:UIControlEventTouchUpInside]; _window = [[UIWindow alloc]initWithFrame:CGRectMake(100, 200, 80, 80)]; _window.windowLevel = UIWindowLevelAlert+1; _window.backgroundColor = [UIColor redColor]; _window.layer.cornerRadius = 40; _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)关闭window
释放keyWindow即可关闭window
/** * 关闭悬浮的window */- (void)resignWindow{ [_window resignKeyWindow]; _window = nil; }
完整代码如下:
//// TableViewController.m// //// Created by MRBean on 15/5/23.////#import "TableViewController.h"@interface TableViewController ()@property(strong,nonatomic)UIWindow *window;@property(strong,nonatomic)UIButton *button;@end@implementation TableViewController- (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(createButton) withObject:nil afterDelay:1]; }- (void)createButton{ _button = [UIButton buttonWithType:UIButtonTypeCustom]; [_button setTitle:@"悬浮按钮" forState:UIControlStateNormal]; _button.frame = CGRectMake(0, 0, 80, 80); [_button addTarget:self action:@selector(resignWindow) forControlEvents:UIControlEventTouchUpInside]; _window = [[UIWindow alloc]initWithFrame:CGRectMake(100, 200, 80, 80)]; _window.windowLevel = UIWindowLevelAlert+1; _window.backgroundColor = [UIColor redColor]; _window.layer.cornerRadius = 40; _window.layer.masksToBounds = YES; [_window addSubview:_button]; [_window makeKeyAndVisible];//关键语句,显示window}/** * 关闭悬浮的window */- (void)resignWindow{ [_window resignKeyWindow]; _window = nil; }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 20;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath]; return cell;}@end
可以看到,有了window任何 神马的 悬浮按钮悬浮窗都是 浮云啦....
希望本文能对你有所帮助,更多文章: http://blog.csdn.net/yangbingbinga
- iOS开发-UIWindow的用法-创建悬浮按钮
- iOS开发-UIWindow的用法-创建悬浮按钮
- 【iOS开发】UIWindow创建悬浮按钮
- 利用UIWindow创建悬浮按钮
- iOS---悬浮按钮的创建和使用
- ios uiwindow 的用法
- (iOS开发) 自定义悬浮按钮
- 创建简单的悬浮按钮
- iOS 用UIWindow实现一个悬浮按钮或者全屏覆盖效果
- 仿ios touch按钮创建可随意拖动的悬浮按钮
- iOS开发-tableView上显示悬浮按钮悬浮窗
- iOS中UIWindow的创建和删除
- iOS中UIWindow的用法(一)
- iOS开发中的UIWindow
- iOS 开发 之UIWindow
- iOS开发之UIWindow
- iOS开发中UIWindow
- iOS 开发 UIWindow 说明
- Linux系统下配置Java环境
- Android .9图片
- Oracle 触发器更新本表数据
- Android Loader 异步加载详解一:基础概念
- Linux环境下配置虚拟主机域名
- iOS开发-UIWindow的用法-创建悬浮按钮
- 快速阅读——套路
- 数据结构之并查集
- C++ STL中容器的使用全面总结
- CSS3 background-size图片自适应
- Phoenix 配置
- Linux常用命令
- TextView跑马灯与EditText共存失效
- kafka+ss create stream