wolfcolony / MISFloatingBall

an easy way add a floating ball
42 stars 10 forks source link

floatingBall怎么显示在键盘上面 #2

Open hejunbinlan opened 7 years ago

hejunbinlan commented 7 years ago

floatingBall怎么显示在键盘上面

wolfcolony commented 7 years ago

HI,刚才工作之余突然一个想法一闪而过,然后试了一下,系统可以注册通知,当一个窗口显示的时候,UIWindowDidBecomeVisibleNotification,然后在接受通知里判断,如果当前窗口是键盘的时候,直接改变我floatingBall的层级

@interface ViewController ()

@property (nonatomic, strong) UIWindow *demoWindow;

@end

@implementation ViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowBecomeVisible:) name:UIWindowDidBecomeVisibleNotification object:nil];

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    textView.backgroundColor = [UIColor redColor];
    [textView becomeFirstResponder];
    [self.view addSubview:textView];

    self.demoWindow = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 100)];
    self.demoWindow.backgroundColor = [UIColor yellowColor];
    self.demoWindow.rootViewController =[UIViewController new];

    [self.demoWindow makeKeyAndVisible];
}

- (void)windowBecomeVisible:(NSNotification*)noti {
    UIWindow *window = noti.object;

    if (window.windowLevel == 10000000) {

        self.demoWindow.windowLevel = window.windowLevel * 2;
    }
}
@end

但是奇怪的是直接设置窗口的层级为 10000000 * 2 是没有用的。个人猜测可能是系统键盘还没有弹出,当前键盘窗口不可见,设置level无效。但是如果说这个和顺序有关如果一个窗口没有弹出,设置另一个窗口层级高于这个窗口的话,这也是错误的,因为直接拿两个自定义窗口做了一个测试,发现和顺序是无关的,所以猜测可能是键盘的特殊性,如果后面有发现再来回复你