2014-03-13 41 views
5

我一直在读通过以下网址由苹果公司提供的文本编程指南:防止键盘重叠的UIButton /的UITextField

https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/KeyboardManagement/KeyboardManagement.html

我试图阻止按钮和文本框从键盘重叠。

当我粘贴从列表5-1中的代码(下面列出)按照导的指令I接收错误,如:

" Use of unidentified identifier 'scrollView'; Did you mean 'UIScrollView' ? " 

" Property 'contentsInset' not found in 'scrollView' " 



// Call this method somewhere in your view controller setup code. 
- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(keyboardWasShown:) 
      name:UIKeyboardDidShowNotification object:nil]; 

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

} 

// 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)) { 
     [self.scrollView scrollRectToVisible:activeField.frame animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 
+0

你实际使用滚动型? – GenieWanted

+1

是的我已经在scrollView中嵌入了UIViewController的内容(两个textFields和一个按钮)@GenieWanted – arman

+0

你的UIScrollView的名称是什么?您需要指定该位置来代替scrollView。 – GenieWanted

回答

1
尝试

此代码将解决您的问题,通过TextField

-(void)textViewDidBeginEditing:(UITextView *)textView{ 

    [self animateTextFild:textView up:YES]; 
} 

-(void)textViewDidEndEditing:(UITextView *)textView{ 

    [self animateTextFild:textView up:NO]; 
    [textView resignFirstResponder]; 
} 

-(void) animateTextFild:(UITextView *)textField up:(BOOL)up{ 

    int animatedDistance; 

    int moveUpValue = textField.frame.origin.y+ textField.frame.size.height; 

    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 

    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 

     animatedDistance = 216-(730-moveUpValue); 
    } 
    else 
    { 
     animatedDistance = 162-(520-moveUpValue); 
    } 

    if(animatedDistance>0) 
    { 
     const int movementDistance = animatedDistance; 

     const float movementDuration = 0.3f; 

     int movement = (up ? -movementDistance : movementDistance); 

     [UIView beginAnimations: nil context: nil]; 

     [UIView setAnimationBeginsFromCurrentState: YES]; 

     [UIView setAnimationDuration: movementDuration]; 

     myView.frame = CGRectOffset(ViewEdit.frame, 0, movement); 

     [UIView commitAnimations]; 
    } 
} 
+0

这是用于文本。我需要解决按钮问题。 – arman

1

在b代替TextView艾伦方法将帮助你。

在.m文件中声明以下常量。

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION  = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION  = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 

,并添加以下代码

- (void)textFieldDidBeginEditing:(UITextField *)textField 
    { 
     CGRect textFieldRect = 
     [self.view.window convertRect:textField.bounds fromView:textField]; 
     CGRect viewRect = 
     [self.view.window convertRect:self.view.bounds fromView:self.view]; 

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = 
    midline - viewRect.origin.y 
    - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = 
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) 
    * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 

    if (heightFraction < 0.0) 
    { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) 
    { 
     heightFraction = 1.0; 
    } 

    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    } 
    else 
    { 
     animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    } 

    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.origin.y -= animatedDistance; 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.scrollView setFrame:viewFrame]; 

    [UIView commitAnimations]; 
    for (id navigBar in [self.view subviews]) 
    { 
     if ([navigBar isKindOfClass:[UINavigationBar class]]) 
     { 
      [self.view bringSubviewToFront:navigBar]; 
     } 
    } 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.origin.y += animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.scrollView setFrame:viewFrame]; 

    [UIView commitAnimations]; 
    for (id navigBar in [self.view subviews]) { 

     if ([navigBar isKindOfClass:[UINavigationBar class]]) 
     { 
      [self.view bringSubviewToFront:navigBar]; 
     } 
    } 
} 
0

最合适的解决方案,我发现:COCOAWITHLOVE

// Keyboard correction method. 
CGFloat animatedDistance; 
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; 
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2; 
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8; 
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; 
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; 

- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    CGRect textFieldRect = 
    [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = 
    [self.view.window convertRect:self.view.bounds fromView:self.view]; 

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; 
    CGFloat numerator = 
    midline - viewRect.origin.y 
    - MINIMUM_SCROLL_FRACTION * viewRect.size.height; 
    CGFloat denominator = 
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) 
    * viewRect.size.height; 
    CGFloat heightFraction = numerator/denominator; 

    if (heightFraction < 0.0) 
    { 
     heightFraction = 0.0; 
    } 
    else if (heightFraction > 1.0) 
    { 
     heightFraction = 1.0; 
    } 

    UIInterfaceOrientation orientation = 
    [[UIApplication sharedApplication] statusBarOrientation]; 
    if (orientation == UIInterfaceOrientationPortrait || 
     orientation == UIInterfaceOrientationPortraitUpsideDown) 
    { 
     animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); 
    } 
    else 
    { 
     animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); 
    } 

    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y -= animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    CGRect viewFrame = self.view.frame; 
    viewFrame.origin.y += animatedDistance; 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; 

    [self.view setFrame:viewFrame]; 

    [UIView commitAnimations]; 
} 

- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
}