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