iOS开发-UIWindow的用法-创建悬浮按钮


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

www.MyException.Cn  网友分享于:2015-05-25  浏览:0次
iOS开发-UIWindow的用法-创建悬浮按钮

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值