UIKeyboardWillShowNotification并在显示键盘时向上滑动

时间:2012-04-11 15:30:33

标签: ios keyboard notifications

这是我的代码:

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = scroll.frame;
    if (movedUp)
    {
        rect.size.height += kOFFSET_FOR_KEYBOARD;
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
        rect.origin.y += kOFFSET_FOR_KEYBOARD;


    }
    scroll.frame = rect; 
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMovedUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMovedUp:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

问题是,我怎么能说这只能用于TextView3,而不是TextView2或TextView1。 BackGround是,TextView1和TextView2不需要因为我的视图之上有。 只有TextView3是不可读的TextView,因为键盘被覆盖,必须更正视图才能读取我写的内容。

1 个答案:

答案 0 :(得分:2)

为textView3指定一个特殊标记实现TextView委托mothode - (void)textViewDidBeginEditing:(UITextView *)textView;,如果它等于textView 3标记,则会检查textView标记,调用你的函数-(void)setViewMovedUp:(BOOL)movedUp

更多代码:

好的,您需要将TextViewDelegate导入到您的viewController中,如下所示:在.h viewController文件中 - &gt; viewController : UIViewController<UITextVIewDelegate>

然后在viewDidload中为ex指定如下标记:textView3.tag = 222;

然后把我在上面编写的委托方法“textViewDidBeginEdit”和内部制作一个if语句来检查textView标签,如果等于222则调用你的函数。

这是我现在可以描述的最好的因为我从iPhone写这个...祝你好运。