Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a more elegant solution using uiview category</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface UIView(AnimationUtils) -(void)scrollControlToCenter:(UIView *)view; -(void)scrollViewToOriginalPosition; @end #import "UIView+AnimationUtils.h" @implementation UIView(AnimationUtils) #pragma mark ScrollView Methods //Method Called whenever keyboard appears - (void)scrollControlToCenter:(UIView *)view { if([view isKindOfClass:[UITextField class]]){ CGRect viewFrame = [view frame]; float verticalDistance = 216.0f - viewFrame.origin.y - (2*viewFrame.size.height); if(viewFrame.size.height &gt;= (460 - 216)/2){ verticalDistance = 0; } [UIView beginAnimations:@"ScrollToCenter" context:nil]; [UIView setAnimationDuration:0.5]; [self setFrame:CGRectMake(0, verticalDistance, self.frame.size.width, self.frame.size.height)]; [UIView commitAnimations]; }else if([view isKindOfClass:[UITextView class]]){ [UIView beginAnimations:@"ScrollToTop" context:nil]; [UIView setAnimationDuration:0.5]; UIView *viewBG = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; [viewBG setTag:5]; [viewBG setBackgroundColor:[UIColor blackColor]]; [viewBG setAlpha:0.75]; [self addSubview:viewBG]; [view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height)]; [self setFrame:CGRectMake(0, -view.frame.origin.y , self.frame.size.width, self.frame.size.height)]; [self insertSubview:view atIndex:[self.subviews count] + 1]; [UIView commitAnimations]; } } -(void)scrollViewToOriginalPosition{ [UIView beginAnimations:@"ScrollToOriginal" context:nil]; [UIView setAnimationDuration:0.5]; for(UIView *view in self.subviews){ if(view.tag == 5){ [view removeFromSuperview]; } } [self setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; [UIView commitAnimations]; } #pragma mark - @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload