美文网首页
webview上添加自定义view

webview上添加自定义view

作者: 风___________ | 来源:发表于2017-12-22 13:14 被阅读10次

    思路:

    1. 获取到键盘所在的window
    2. 自定义view添加到window上
    获取window的代码如下
    - (UIWindow *)keyboardWindow{
        UIWindow *keyboardWindow = nil;
        for(UIWindow *window in [UIApplication sharedApplication].windows)
        {
            if([window isKindOfClass:NSClassFromString(@"UIRemoteKeyboardWindow")])
            {
                keyboardWindow = window;
            }
        }
        return keyboardWindow;
    }
    

    添加view代码如下

    键盘出现:

    - (void)keybordShowAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
        UIWindow *keyboardWindow = [self keyboardWindow];
        // 添加自定义view
        if (![keyboardWindow.subviews containsObject:self.toolBarView]) {
            [keyboardWindow addSubview:self.toolBarView];
            [keyboardWindow addSubview:self.keyBoardInputView];
        }
        // 键盘工具栏
        [self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(keyboardWindow);
            make.left.right.equalTo(keyboardWindow);
            make.height.mas_equalTo(keyboardHeight);
        }];
        // 键盘覆盖层
        [self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.keyBoardInputView.mas_top);
            make.left.right.equalTo(keyboardWindow);
            make.height.mas_equalTo(TOOL_BAR_HEIGHT);
        }];
        // 更新约束
        [keyboardWindow layoutIfNeeded];
    }
    

    键盘隐藏:

    - (void)keybordHiddenAnimalByKeyboardHeight:(float)keyboardHeight AndAnimalTime:(float)keyboardDuration{
        UIWindow *keyboardWindow = [self keyboardWindow];
        if (![keyboardWindow.subviews containsObject:self.toolBarView]) {
            [keyboardWindow addSubview:self.toolBarView];
            [keyboardWindow addSubview:self.keyBoardInputView];
        }
        // 修改为以前的约束(距下边距0)这么写的目的是为了动画效果
        [self.toolBarView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(keyboardWindow.mas_bottom);
            make.left.right.equalTo(keyboardWindow);
            make.height.mas_equalTo(TOOL_BAR_HEIGHT);
        }];
        [self.keyBoardInputView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.toolBarView.mas_bottom);
            make.left.right.equalTo(keyboardWindow);
            make.height.mas_equalTo(keyboardHeight);
        }];
        // 更新约束
        [keyboardWindow layoutIfNeeded];
    }
    

    相关文章

      网友评论

          本文标题:webview上添加自定义view

          本文链接:https://www.haomeiwen.com/subject/bfitgxtx.html