2016-10-17 5 views
0

키보드가 나타나면 키보드가 텍스트 필드를 차단하지 못하도록 다음 방법을 사용하여보기를 위로 이동합니다. 기본적으로 스크롤바에 텍스트 필드를 포함하고 키보드가 나타날 때 스크롤을 위쪽으로 스크롤합니다. 텍스트 필드이 화면 상단에 이미 그것은 점프 할 필요가없는 경우IOS : 키보드가 나타나면 TextField 위로 이동

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    _scrollView.contentInset = contentInsets; 
    _scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your app might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, _activeField.frame.origin)) { 
     [_scrollView scrollRectToVisible:_activeField.frame animated:YES]; 
    } 
} 

그러나, 위의 코드는보기에서 올라 점프를 발생합니다.

어딘가에 나는 그것을 막기 위해 추가 할 수있는 선이 있지만 기억이 나지 않습니다. 기본적으로, 텍스트 필드가 이미 키보드가 그것을 커버하지 않을 정도로 높으면 뷰를 이동시킬 필요가 없다는 것을 테스트 할 것입니다.

위 코드의 if 문은 텍스트 필드가 숨겨져 있지 않은 경우를 선별하지 않는 것으로 보입니다.

누구든지이 방법을 제안 할 수 있습니까?

감사합니다.

+0

, 그것은 훨씬 쉬울 것 같습니다 정상적인보기, 당신은 단지 바닥 제약을 조정할 수 다음 layoutIfNeeded 호출, 하위보기는보기 밖으로 뛰지 않을 것입니다 – Tj3n

+0

당신은이 일을 automanages IQkeyboard 라이브러리를 사용할 수 있습니다. 라이브러리를 프로젝트에 넣으면 아무것도 구현하지 않아도됩니다. 자동으로 작업을 시작합니다. –

답변

0

autolayout을 사용하는 경우 제약 조건을 사용하여 프레임 대신보기를 조작해야합니다. 필요에 따라 textField의 아래쪽 간격을 조정하거나 수퍼 뷰를보고 layoutIfNeeded 호출을 애니메이션 블록 안에 넣을 수 있습니다. 또한 하단 구속 조건의 값이 이미 특정 값인지 확인하거나 부울을 UIKeyboardWillHideNotificationUIKeyboardWillShowNotification에 전달할 수 있는지 확인할 수 있습니다. 그렇다면 구속 조건의 값이 변경되어서는 안됩니다. 뭔가 같이 :

-(void)showViewAnimatedly:(BOOL)show{ 

    if(show){ 
     [bottomConstraint setConstant:0]; 
    }else{ 
     [bottomConstraint setConstant:-160]; 
    } 

    [UIView animateWithDuration:0.3f animations:^{ 

     [self.view layoutIfNeeded]; 

    }]; 
} 

또한 전환이 키보드가 나타나는와 조정에 더 보인다 UIKeyboardWillShowNotification를 사용하는 대신 UIKeyboardDidShowNotification 추천 할 것입니다. UIKeyboardDidShowNotification이 전환을 시작합니다. AFTER 키보드가 나타납니다.

0

'IQKeyboardManager'를 확인하십시오. 그것은 귀하의 요구 사항에 대한 훌륭한 컨트롤입니다. 그리고 그것은 한 줄의 코드 만 필요합니다. 그것은 UITextFields 및 UITextViews 모두 작동합니다. 이전 단추와 다음 단추에 대한 옵션도 있습니다. 당신은 cocoapod

https://www.cocoacontrols.com/controls/iqkeyboardmanager

0

의 간단한 키보드가 필요한 경우 애니메이션으로 텍스트 뷰 또는 텍스트 필드의 y 축 좌표를 변경 나타나는 프로젝트에 설치할 수 있습니다. 당신이 참조 할 수있는 코드는 ----> 당신이있는 ScrollView를 사용하여 다음이 서브 뷰 위치를 제어하기 위해 꽤 어려운 경우 (아마도 서브 뷰의 크기와 약간의 제약을 조정해야)

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 

    CGRect rect = self.view.frame; 
    if (movedUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

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

    [[NSNotificationCenter defaultCenter] removeObserver:self 
              name:UIKeyboardWillHideNotification 
              object:nil]; 
} 
+0

여기에있는 뷰는 무엇입니까? scrollview 일 수 있습니까? 한 화면에 너무 많은 내용이 들어 있기 때문에 어떤 경우에도 scrollview를 사용하고 있습니다. – user6631314

+0

예 uiView의 하위 클래스 인 것을 사용할 수 있습니다. 귀하의 경우에는 스크롤보기를 사용하고 키보드의 호출을 위아래로 스크롤하여 나타나고 사라집니다. – vivek

+0

약 12 ​​개 필드와보기가 있습니다. 위의 내용은 mailIf라는 한 텍스트 필드에 따라 달라집니다. 텍스트 또는 텍스트보기에 적용 할 수 있습니까? – user6631314

관련 문제