凌驾所有视图之上的SuperView

问题

开发中可能会遇到这样的问题:使一个View在屏幕中不被任何视图遮盖?
如果遇到类似的问题,这篇文章及示例可能对你有所启发。

效果图

superview.gif

实现

UIWindow了解

要想实现这样的效果,首先要明白知道UIWindow以及一个应用如何通过UIWindow显示到屏幕。这里有一篇文章的讲的很详细
关于UIWindow,这里讲两点:

  • UIWindow是一个特殊的UIView,一个app至少要有一个UIWindow,不论是UIView还是UIViewController,最终还是要通过UIWindow将视图绘制到屏幕上,才能显示。

  • UIWindow 有分层级(windowLevel),层级越高,显示在屏幕最上方。层级相同时,后面调用的UIWindow显示在上方。官方提供了三个常用层级:

    • UIWindowLevelNormal ---> 0
    • UIWindowLevelAlert ---> 2000
    • UIWindowLevelStatusBar ---> 1000
      键盘所在的UIRemoteKeyboardWindow 层级是10000000。

SuperView的实现原理

  • 创建一个独立的UIWindow, 尺寸和SuperView一样。调用window 的 makeKeyAndVisible方法。别忘了调整superView在window 的frame。
- (void)show {
    
    UIWindow *currentKeyWindow = [UIApplication sharedApplication].keyWindow;
    
    NSLog(@"super view frame: %@", NSStringFromCGRect(self.frame));
    if (!_superviewWindow) {
        _superviewWindow = [[BQSuperViewWindow alloc] initWithFrame:_currentFrame];
        _superviewWindow.rootViewController = [ BQSuperViewController new];
    } else {
        _superviewWindow.frame = _currentFrame;
    }
    
    [_superviewWindow makeKeyAndVisible];
    
    self.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    
    self.layer.cornerRadius = self.frame.size.width <= self.frame.size.height ? self.frame.size.width / 2.0 : self.frame.size.height / 2.0;
    
    [_superviewWindow addSubview:self];
    
    // keep the original keyWindow to avoid some unpredictable problems
    [currentKeyWindow makeKeyWindow];

}

要显示在屏幕最上方,窗口的windowLevel要够大。

  • 添加拖动手势,根据拖动位置更新window的frame。
    至于多动的边界控制其实挺简单,详细代码后面有链接。
[UIView animateWithDuration:.25 animations:^{
            _superviewWindow.center = newCenter;
            //
            if (_superviewWindow.hidden == YES) {
                self.center = newCenter;
            }
            
            // record frame for superview back to superviewWindow
            _currentFrame = _superviewWindow.frame;
        }];

  • 添加键盘弹出和隐藏的通知,
    键盘显示时,隐藏独立的window。然后将superView在屏幕中的frame换算到UIRemoteKeyboardWindow中显示。
    键盘隐藏时,重新显示独立的window,根据superView(frame可能变化)的最新frame设置window的位置并显示。
- (void)keyboardIsShown:(NSNotification *)note {
    
    [self convertSuperViewToKeyboardWindow];
}
- (void)keyboardDidHide {
    
    [self show];
}

- (void)convertSuperViewToKeyboardWindow {
    //hide the superViewWindow
    [self hide];
    
    // add superView in keyboardWindow
    self.frame = _currentFrame;
    
    [[self keyboardWindow] addSubview:self];
}

最后的最后

1.完整的实现代码在这里!
2.如果有更好的实现,可以给在评论区留言。或者在github上提出,我会及时跟进。
3.别忘了点赞哈!别忘了点赞哈!别忘了点赞哈!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.尺寸适配1.原因 iOS7中所有导航栏都为半透明,导航栏(height=44)和状态栏(height=20)不...
    LZM轮回阅读 11,381评论 1 4
  • 概述 UIView或者它的子类知道怎样将自己绘制在一个矩形区域中。我们app所有可视的的界面来自于视图。创建和配置...
    smalldu阅读 4,444评论 0 6
  • 简单的悬浮小球demo下载地址 1.UIWindow简介 一个UIWindow对象为应用程序的用户界面提供了背景以...
    雷鸣1010阅读 13,543评论 2 7
  • 一、问题背景 最近需求量放缓,想起了以前曾经later的小需求,也就是弹出来的AlertView中间的文本框输入一...
    唐笛_Dylan阅读 18,669评论 6 29
  • 一直以来人都很单纯,从小就被教育要努力,努力考高的分数上好的学校,找好的工作赚更高的薪水,这么一个程序逻辑上并没有...
    国玺同学阅读 4,140评论 0 1

友情链接更多精彩内容