move up UIToolbar

前端 未结 5 1366
余生分开走
余生分开走 2020-12-08 12:16

I have a UIToolbar at bottom of my view, and I have a UITextField in this toolbar. When I begin editing this field, it is hidden behind the keyboard. To see what I\'ve typed

5条回答
  •  孤城傲影
    2020-12-08 12:40

    Here is a more elegant solution using uiview category

    #import 
    
    
    @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 >= (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
    

提交回复
热议问题