uitextfield 또는 uitextview에 키보드가 나타나면 인터페이스 조정

StackOverflow https://stackoverflow.com/questions/402658

  •  03-07-2019
  •  | 
  •  

문제

각 셀에 레이블과 텍스트 필드가 포함 된 테이블이 있습니다. 문제는 마지막 행을 편집 할 때 키보드가 테이블의 아래쪽 부분을 숨기고 타이핑되는 것을 볼 수 없다는 것입니다. 키보드 위로 인터페이스를 이동하여 타이핑중인 내용을 확인하려면 어떻게해야합니까?

고마워, 무스타파

도움이 되었습니까?

해결책

ViewController를 등록하고 싶을 것입니다 UIKeyboardDidShowNotification 그리고 UIKeyboardWillHideNotification 이벤트. 이것을 얻으면 테이블의 한계를 조정해야합니다. 키보드는 170 픽셀 높이이므로 테이블을 적절하게 축소하거나 키우면 키보드에 올바르게 조정해야합니다.

다른 팁

이 문제는 UI 시나리오에 따라 복잡합니다. 여기서는 uitextfield 또는 uitextview가 uitableviewcell에있는 시나리오에 대해 설명합니다.

  1. uikeyboarddidshownotification 이벤트를 감지하려면 nsnotificationCenter를 사용해야합니다. 보다 http://iosdevelopertips.com/user-interface/adjust-textfield-hidden-by-keyboard.html. 키보드로 덮인 화면 영역 만 차지하도록 UitableView 프레임 크기를 축소해야합니다.

    1. UitableViewCell을 탭하면 OS는 셀을 자동으로 UitableView의 시청 영역 내에 배치합니다. 그러나 UitableViewCell에 있는데도 UitextView 또는 UitableViewCell을 탭할 때는 발생하지 않습니다.

전화해야합니다

[myTableView selectRowAtIndexPath:self.indexPath animated:YES scrollPosition:UITableViewScrollPositionBottom];` 

프로그래밍 방식으로 셀을 "탭"합니다.

두 지점을 모두 구현하면 키보드 바로 위의 UitextView/필드 위치가 표시됩니다. UitableView/Field가있는 UitableViewCell은 "커버되지 않은"지역보다 키가 크지 않다는 것을 명심하십시오. 이것이 당신의 경우가 아니라면, 그것에 대한 다른 접근법이 있지만 나는 여기서 논의하지 않을 것입니다.

그 아래에 충분한 스크롤 공간이 있는지 확인하십시오. 내 지식에 따라 iPhone은 키보드가 나타날 때 초점을 맞춘 텍스트 상자를 자동으로 조정하고 표시합니다.

직장의 hight가 수정되고있는 줄을 제거/댓글을 달면 문제가 해결되는 것 같습니다. 감사.

수정 된 코드 :

# define kOFFSET_FOR_KEYBOARD 150.0     // keyboard is 150 pixels height

// Animate the entire view up or down, to prevent the keyboard from covering the author field.
- (void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];

// Make changes to the view's frame inside the animation block. They will be animated instead
// of taking place immediately.
CGRect rect = self.view.frame;
CGRect textViewRect = self.textViewBeingEdited.frame;
CGRect headerViewRect = self.headerView.frame;

if (movedUp) {
    // If moving up, not only decrease the origin but increase the height so the view 
    // covers the entire screen behind the keyboard.
    rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    // rect.size.height += kOFFSET_FOR_KEYBOARD;
} else {
    // If moving down, not only increase the origin but decrease the height.
    rect.origin.y += kOFFSET_FOR_KEYBOARD;
    // rect.size.height -= kOFFSET_FOR_KEYBOARD;
}

self.view.frame = rect;
[UIView commitAnimations];

}

     [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardDidShowNotification
                                                   object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasHidden:)
                                                     name:UIKeyboardDidHideNotification
                                                   object:nil];
        keyboardVisible = NO;

- (void)keyboardWasShown:(NSNotification *)aNotification {
    if ( keyboardVisible )
        return;

    if( activeTextField != MoneyCollected)
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y -= keyboardSize.height-300;
        frame.size.height += keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = YES;
    }

    keyboardVisible = YES;
}

- (void)keyboardWasHidden:(NSNotification *)aNotification {
    if ( viewMoved ) 
    {
        NSDictionary *info = [aNotification userInfo];
        NSValue *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
        CGSize keyboardSize = [aValue CGRectValue].size;

        NSTimeInterval animationDuration = 0.300000011920929;
        CGRect frame = self.view.frame;
        frame.origin.y += keyboardSize.height-300;
        frame.size.height -= keyboardSize.height-50;
        [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
        [UIView setAnimationDuration:animationDuration];
        self.view.frame = frame;
        [UIView commitAnimations];

        viewMoved = NO;
    }

    keyboardVisible = NO;
}
tabelview.contentInset =  UIEdgeInsetsMake(0, 0, 210, 0);
[tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:your_indexnumber inSection:Your_section]
                 atScrollPosition:UITableViewScrollPositionMiddle animated:NO];

이것을 시도해보십시오. 이것은 당신에게 도움이 될 것입니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top