iOS-悬浮按钮
2016-07-11 11:55
615 查看
在项目中可能会有这种需求,即在一个界面最顶层需要一个按钮,这个按钮可能是发布信息功能,也可能是回到顶部.这样我们可以使用UIwindow这个神奇的控件实现,很简单.
完整项目源码:
https://github.com/qxuewei/XWSuspendBtn
最终实现效果如下:
![](https://img-blog.csdn.net/20160711114815767)
实现逻辑:
1.在需要出现悬浮按钮的类中声明按钮UIButton属性和UIWindow属性
2.创建UIWindow以及悬浮按钮方法
3.初始化视图时创建悬浮按钮
项目github地址:
https://github.com/qxuewei/XWSuspendBtn
完整项目源码:
https://github.com/qxuewei/XWSuspendBtn
最终实现效果如下:
实现逻辑:
1.在需要出现悬浮按钮的类中声明按钮UIButton属性和UIWindow属性
/** window */ @property (nonatomic, strong) UIWindow *window; /** 悬浮按钮 */ @property (nonatomic, strong) UIButton *button;
2.创建UIWindow以及悬浮按钮方法
-(void)creatSuspendBtn{ //悬浮按钮 _button = [UIButton buttonWithType:UIButtonTypeCustom]; [_button setImage:[UIImage imageNamed:@"plus"] forState:UIControlStateNormal]; CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width; CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height; _button.frame = CGRectMake(0,0, 64, 64); [_button addTarget:self action:@selector(suspendBtnClick) forControlEvents:UIControlEventTouchUpInside]; //添加悬浮按钮方式一(直接添加): // _button.frame = CGRectMake(screenWidth*0.5-28, screenHeight-74, 56, 56); // [_button addTarget:self action:@selector(suspendBtnClick) forControlEvents:UIControlEventTouchUpInside]; //[self.view addSubview:_button]; //[self.view bringSubviewToFront:_button]; //添加悬浮按钮方式二:(利用UIWindow) _button.frame = CGRectMake(0,0, 56, 56); [_button addTarget:self action:@selector(suspendBtnClick) forControlEvents:UIControlEventTouchUpInside]; //悬浮按钮所处的顶端UIWindow _window = [[UIWindow alloc] initWithFrame:CGRectMake(screenWidth*0.5-28, screenHeight-54, 56, 56)]; //使得新建window在最顶端 _window.windowLevel = UIWindowLevelAlert + 1; _window.backgroundColor = [UIColor clearColor]; [_window addSubview:_button]; //显示window [_window makeKeyAndVisible]; }
3.初始化视图时创建悬浮按钮
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self.mainTableView setDelegate:self]; [self.mainTableView setDataSource:self]; //延时加载window,注意我们需要在rootWindow创建完成之后再创建这个悬浮的按钮 [self performSelector:@selector(creatSuspendBtn) withObject:nil afterDelay:0.2]; }
项目github地址:
https://github.com/qxuewei/XWSuspendBtn
相关文章推荐
- IOS中导入OpenCV
- iOS开发之获取实时海拔高度
- NSUserDefaults 简介,使用 NSUserDefaults 存储自定义对象
- IOS博客项目搭建-03-代码重构
- Ios 24小时制与12小时制
- iOS—最全的真机测试教程
- iOS Block 和 Delegate 的使用比较
- 在导航栏上添加搜索框的问题
- iOS: xcode打包上传iTunes失败,iTunes Store operation failed,this action can not complete .try again
- ios-IM
- iOS CoreTelephony 实现监听通话状态
- AFNetworking 源码解析之“AFURLSessionManager”
- iOS XMPP协议的服务端Tigase
- IOS 开发大牛首选之路
- iOS--检测野指针
- iOS设计模式(九) 模板方法模式
- iOS中 如何将自己的框架更新到cocopods上
- iOS AFNetWorking源码详解(一)
- ios截屏 裁剪区域
- iOS新的视频开发框架AVPlayerViewContoller与画中画技术