在UIWebView中调整iOS键盘高度的对比度

时间:2015-08-05 22:21:35

标签: ios objective-c uiwebview autolayout

我有一个简单的浏览器视图控制器,用作故事板的一部分。它开始看起来很棒。我的UIToolBar通过垂直空间约束为0锚定在UIView的底部。

enter image description here

当您点击打开键盘的网页上的某些内容时。隐藏了UIToolBar。所以我为键盘可见性更改添加了一个监听器,并根据键盘的高度调整了约束。这似乎运作良好。

enter image description here

然而,如果用户然后按下键盘上的最小化按钮,则键盘不会完全消失。允许在输入字段之间进行选项卡的顶部箭头键(我不知道该怎么称呼它)将保持可见。所以我不能将我的约束设置回0,我必须根据可见键盘的高度再次设置它(我认为这将包括顶部栏)。

然而,当我的UIKeyboardDidHideNotification开火时,键盘高度仍然相同,所以我最终会这样。

enter image description here

我移动约束的逻辑基于以这种方式获取键盘高度:

   // get height of visible keyboard
    NSDictionary* keyboardInfo = [aNotification userInfo];
    NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
    CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

    _toolbarBottomVerticalSpaceConstraint.constant = -1 * keyboardFrameBeginRect.size.height;

UIKeyboardFrameBeginUserInfoKey不是在隐藏键盘的情况下使用的基础值吗?

此视图控制器的整个源实际上目前非常简单,因此我将包含所有内容,以防有人稍后要求它。

    #import "LEPopupBrowserViewController.h"

    @interface LEPopupBrowserViewController ()
    @property (weak, nonatomic) IBOutlet UIWebView *webView;
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *toolbarBottomVerticalSpaceConstraint;

    @end

    @implementation LEPopupBrowserViewController

    - (void)viewDidLoad {
        [super viewDidLoad];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];

        if (_url != nil) {
            NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:_url]];
            [_webView loadRequest:request];
        }
    }

- (void) viewDidUnload {
    [[NSNotificationCenter defaultCenter]  removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]  removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

    // Called when the UIKeyboardDidShowNotification is sent.
    - (void)keyboardWillShow:(NSNotification*)aNotification
    {
        // get height of visible keyboard
        NSDictionary* keyboardInfo = [aNotification userInfo];
        NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

        _toolbarBottomVerticalSpaceConstraint.constant = -1 * keyboardFrameBeginRect.size.height;
    }

    // Called when the UIKeyboardWillHideNotification is sent
    - (void)keyboardDidHide:(NSNotification*)aNotification
    {
        // get height of visible keyboard
        NSDictionary* keyboardInfo = [aNotification userInfo];
        NSValue* keyboardFrameBegin = [keyboardInfo valueForKey:UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardFrameBeginRect = [keyboardFrameBegin CGRectValue];

        _toolbarBottomVerticalSpaceConstraint.constant = -1 * keyboardFrameBeginRect.size.height;
    }


    /*
    #pragma mark - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    - (IBAction)doneButtonPressed:(id)sender {
        // close keyboard if present
        [self.view endEditing:YES];

        // dismiss ourselves
        [self dismissViewControllerAnimated:YES completion:nil];
    }

    @end

更新

我通过一些额外的research找到了这个额外的视图,称为accessoryView。我看到很多人试图remove it,但没有发现任何你可以轻易找到它的高度的帖子。关于将它删除给我的麻烦部分似乎是苹果可能拒绝你的应用程序。

0 个答案:

没有答案