0

我遇到一个问题:

我有一个像这样的视图控制器。 在此处输入图像描述

为了在键盘出现时使工具栏向上,我将 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)];

现在我想单击工具栏中的左键,并出现框架与此键盘相同的视图。

但是我在哪里可以添加这个视图?如果将子视图添加到 self.view,它将与键盘顶部的 self.view 一起向上移动,而不是覆盖。我是IOS的初学者,我对此一无所知,并且已经搜索过,但对此一无所知。

另一个问题,如果底部的工具栏,我也想点击它的左键来显示视图(动画和框架都和键盘一样),我该怎么办?

你能帮助我吗?谢谢

4

2 回答 2

1

我是对的,你想用键盘上下连接的键盘上方弹出栏吗?

然后你必须注册键盘显示和隐藏通知,并根据通知和键盘显示和隐藏速度上下动画视图。

[[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;
}

用您自己的视图直接替换键盘顶部的给定视图。这个视图应该是一个包含所有按钮和东西的单一视图。

问候

于 2012-08-30T16:02:11.710 回答
0

首先,为什么要覆盖键盘?这样做似乎不是一个好主意。而且我认为这根本不可能。对于第二个问题的答案,您应该查看UIView文档:

[UIView animateWithDuration:0.3 animations:^{
    // place your view wherever you want
    CGRect frame = myView.frame;
    frame.y = self.view.frame.size.height - frame.size.height;
    myView.frame = frame;
}];
于 2012-08-30T15:50:39.337 回答