簡體   English   中英

如何以縱向方向移動鍵盤視圖

[英]How to move view for keyboard in portrait orientations

我有一個僅在portrait和portraitUpsideDown中運行的應用程序。 我需要在鍵盤出現時將視圖向上推,在鍵盤消失時將其拉回。 如果設備保持縱向,下面的代碼可以完美地工作,但是如果設備處於portraitUpsideDown,則視圖將向錯誤的方向移動(-260而不是260),並且如果在顯示鍵盤時方向發生了變化,則無法處理...。 keyboardWillHide方法在兩個方向上都可以正常工作。 有沒有一種方法可以將相對視圖移至鍵盤或狀態欄,因此設備所處的方向無關緊要?

- (void) keyboardWillShow:(NSNotification *) notification
{
    NSLog(@"Keyboard Will Show");
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + -260);
    }completion:^(BOOL finished){
    }];
}

- (void) keyboardWillHide:(NSNotification *) notification
{
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSLog(@"Keyboard Will Hide");

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        [self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
     }completion:^(BOOL finished){
    }];
}

我通過以下方式解決了它(不太優雅):

注意:您應該執行的操作,最終將更改此代碼以執行以下操作:創建一個屬性以保存鍵盤動畫的持續時間,以便可以在鍵盤委托方法之外使用它,並類似地為offset創建一個屬性並確定它使用userInfo表示鍵盤的高度。

- (void) keyboardWillShow:(NSNotification *) notification
{
    NSLog(@"Keyboard Will Show");
    if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
        offset = -260;
    else 
        offset = 260;

    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y + offset);
    }completion:^(BOOL finished){
    }];
}

- (void) keyboardWillHide:(NSNotification *) notification
{
    double animationDuration;
    animationDuration = [[[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    NSLog(@"Keyboard Will Hide");

    [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationCurveEaseIn animations:^{
        self.view.center = CGPointMake(self.view.center.x, self.view.center.y - offset);
     }completion:^(BOOL finished){
    }];
}

- (void) keyboardDidShow
{
    NSLog(@"Keyboard Did Show");
    keyboardIsShowing = YES;
}

- (void) keyboardDidHide
{
    NSLog(@"Keyboard Did Hide");
    keyboardIsShowing = NO;
}


-(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{    
    if (keyboardIsShowing && UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
    {
        if (toInterfaceOrientation == UIInterfaceOrientationPortrait && offset == 260)
            offset = -260;
        else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown && offset == -260)
            offset = 260;
        else 
            return;
        [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationCurveEaseIn animations:^{
            self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 2* offset);
        }completion:^(BOOL finished){
        }];
    }
}

不幸的是,您將不得不自己進行管理。 您可以詢問當前方向並適當設置y軸調整。

int movementDistance = -260;
if (UIInterfaceOrientationPortraitUpsideDown == [self interfaceOrientation]) movementDistance = -movementDistance;

您需要設置框架高度,或為視圖而不是中心設置contentInset。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM