IOS如何制作 View 覆盖键盘

标签 ios uiview uiviewcontroller keyboard

我遇到了一个问题:

我有一个像这样的 View Controller 。 enter image description here

为了在键盘出现时使工具栏向上,我将 self.view 向上移动。

[self.view setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y + keyboardFrame.size.height * (up ? -1 : 1), self.view.frame.size.width, self.view.frame.size.height)];

现在我想点击工具栏中的左侧按钮,出现与该键盘框架相同的 View 。

但是我可以在哪里添加这个 View 呢? 如果将 subview 添加到self.view中,它将与键盘顶部的self.view一起向上移动,而不是覆盖。 我是IOS的初学者,我对此一无所知,并进行了搜索,但对此一无所知。

还有一个问题,如果当底部的工具栏时,我也想点击它的左键来显示 View (动画和框架都与键盘相同),我该怎么办?

你能帮我吗? 谢谢

最佳答案

我说得对吗,你想弹出键盘上方与键盘上下相连的栏吗?

然后,您必须注册键盘显示和隐藏通知,并根据通知和键盘显示和隐藏速度对 View 进行上下动画。

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

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification 
                                           object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(keyboardWasShown:)
                                             name:UIKeyboardDidShowNotification object:nil];

现在您必须实现所有选择器方法:

//Code from Brett Schumann
-(void) keyboardWillShow:(NSNotification *)note{
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    // get a rect for the textView frame
    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    containerView.frame = containerFrame;

    // commit animations
    [UIView commitAnimations];

    [self showSendButton];
}

-(void) keyboardWillHide:(NSNotification *)note{
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // get a rect for the textView frame
    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - containerFrame.size.height;

    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    containerView.frame = containerFrame;

    // commit animations
    [UIView commitAnimations];

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0, 0, containerView.frame.size.height-46.0f+20.0f, 0);
    _table.contentInset = contentInsets;
    _table.scrollIndicatorInsets = contentInsets;

    //display button based on facebook login status
    if ([facebookService.facebook isSessionValid]) {
        [commentButton makeButtonLogoutButton];
        textView.editable = YES;
    } else {
        [commentButton makeButtonLoginButton];
        textView.editable = NO;
    }
}

- (void)keyboardWasShown:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height+20.0f, 0.0);
    _table.contentInset = contentInsets;
    _table.scrollIndicatorInsets = contentInsets;
}

请务必用您自己的 View 替换键盘顶部的给定 View 。该 View 应该是包含所有按钮和内容的单个 View 。

问候

关于IOS如何制作 View 覆盖键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12200537/

相关文章:

ios - 如果用户安装了您的主应用程序,是否会自动安装 iOS 共享扩展?

ios - 如何设置自定义 UIView 属性

ios - UIViewController 初始化与 initWithNibName :bundle:

登录注册首页的IOS最佳实践

ios - Cocoapod pod.lock 文件在 pod 安装时通过在 repo url 末尾添加 .git 进行更改

ios - 使用 Xcode 出现错误 "No such module",但框架存在

ios - 直通触摸事件

iOS:如何将 ViewController 推送到当前上下文?

ios - Swift - 使 UIViewController 可见,同时从 TOP 添加动画的新 ViewController(通过 self.present)

android - 使 iOS/Android HTTP 请求看起来像是来自 PC 浏览器?