簡體   English   中英

在iOS中全屏設置UITextView

[英]Set UITextView on full screen in ios

我已經在設備的全屏上設置了UITextView,就像在iPhone-5s中一樣,textView的大小為(0,44,320,524)。 當出現鍵盤時,用戶無法在視圖中顯示插入文本。如何管理UITextView,可以使用UIScrollView還是其他方式?

編輯完成時,View將向上移動,完成編輯后將向下移動...

(void)textViewDidBeginEditing:(UITextView *)textView {
    [self animateTextView: YES];
}

(void)textViewDidEndEditing:(UITextView *)textView  {
    [self animateTextView:NO];
}

(void) animateTextView:(BOOL) up {
    const int movementDistance =heightKeyboard; // tweak as needed
    const float movementDuration = 0.3f; // tweak as needed
    int movement= movement = (up ? -movementDistance : movementDistance);
    NSLog(@"%d",movement);
    [UIView beginAnimations: @"anim" context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    self.view.frame = CGRectOffset(self.inputView.frame, 0, movement);
    [UIView commitAnimations];
}

希望它對您有用。

我已經通過使用此代碼解決了

.h文件定義一個值

 CGRect originalTextViewFrame;

.m文件

  - (void)viewWillAppear:(BOOL)animated
  {
  // Register notifications for when the keyboard appears
     [[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 
   {
   [[NSNotificationCenter defaultCenter] removeObserver:self];
   }

  - (void)keyboardWillShow:(NSNotification*)notification 
   {
   [self moveTextViewForKeyboard:notification up:YES];
   }

  - (void)keyboardWillHide:(NSNotification*)notification 
   {
   [self moveTextViewForKeyboard:notification up:NO];
   }
  - (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up
  {
  NSDictionary *userInfo = [notification userInfo];
  NSTimeInterval animationDuration;
  UIViewAnimationCurve animationCurve;
  CGRect keyboardRect;
 [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
 animationDuration = [[userInfo      objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
 keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]   CGRectValue];
 keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
 [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
 [UIView setAnimationDuration:animationDuration];
 [UIView setAnimationCurve:animationCurve];

 if (up == YES) {
 CGFloat keyboardTop = keyboardRect.origin.y;
 CGRect newTextViewFrame = textView.frame;
 originalTextViewFrame = textView.frame;
 newTextViewFrame.size.height = keyboardTop - textView.frame.origin.y - 10;
 textView.frame = newTextViewFrame;
 }     
 else
 {
 // Keyboard is going away (down) - restore original frame
  textView.frame = originalTextViewFrame;
  }

 [UIView commitAnimations];
 }

 -(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
   {
  // Any new character added is passed in as the "text" parameter
  if ([text isEqualToString:@"\n"])
   {
    // Be sure to test for equality using the "isEqualToString" message
    [textView resignFirstResponder];
    //[scrlView setContentOffset:CGPointMake(0,0) animated:YES];

    // Return FALSE so that the final '\n' character doesn't get added
    return FALSE;
   }
  // For any other character return TRUE so that the text gets added to the view
  return TRUE;
  }

使用滾動視圖,然后嘗試后

UITextViewDelegate 

方法

- (void)textViewDidBeginEditing:(UITextView *)textView
{
  [scrollView setContentOffset:CGPointMake(0,yourtextview.center.y-200) animated:YES];
}

暫無
暫無

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

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