键盘弹出后上提view隐藏后下拉view还原并修改scroll过程中旋转屏幕到竖屏view显示错误

1,注册键盘相应事件

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

2,移除相应事件

   - (void)viewDidDisappear:(BOOL)animated{
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
    
}

3,控制view高度

-(void)hidenKeyboard
{
    [self.accountFieldItem resignFirstResponder];
    [self.passwordFieldItem resignFirstResponder];
    
}

-(void)nextOnKeyboard:(UITextField *)sender
{
    
    if (sender == self.accountFieldItem)
    {
        [self.passwordFieldItem becomeFirstResponder];
    }
    else if (sender == self.passwordFieldItem)
    {
        [self hidenKeyboard];
    }
    
}

- (void)keyboardWillShow:(NSNotification *)notification {
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        
        if (self.interfaceOrientation != UIDeviceOrientationPortrait &&
            self.interfaceOrientation != UIDeviceOrientationPortraitUpsideDown){
            
            [self.scrollView setScrollEnabled:YES];
            [self.scrollView setShowsVerticalScrollIndicator:YES];
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.5];
            [self.scrollView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds) + CGRectGetWidth(self.view.bounds)/4 - 20, 0, CGRectGetWidth(self.view.bounds)/2 +40,self.view.frame.size.height)];
            
            [UIView commitAnimations];
            
        }else
        {
            [self.scrollView setScrollEnabled:NO];
            [self.scrollView setShowsVerticalScrollIndicator:NO];
            [self.accountFieldItem becomeFirstResponder];
            self.scrollView.frame = CGRectMake(CGRectGetMinX(self.view.bounds) + 20,CGRectGetMinY(self.view.bounds) + 25,CGRectGetWidth(self.view.bounds) - 40,self.view.frame.size.height);
            
            
        }
    }
    else
    {
        if (self.interfaceOrientation != UIDeviceOrientationPortrait &&
            self.interfaceOrientation != UIDeviceOrientationPortraitUpsideDown){
            
            [self.scrollView setScrollEnabled:YES];
            [self.scrollView setShowsVerticalScrollIndicator:YES];
            
        }
        else
        {
            [self.scrollView setScrollEnabled:NO];
            [self.scrollView setShowsVerticalScrollIndicator:NO];
        }
    }
}

- (void)keyboardWillHide:(NSNotification *)notification {
    
    [self.scrollView setScrollEnabled:NO];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:NO];//将scroll滚动拉动起始位置,然后旋转成竖屏后就不会出现view显示错误,直接从scrollview顶部开始显示,否则又可以只显示了半截或者更少视图
    
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        
        if (self.interfaceOrientation != UIDeviceOrientationPortrait &&
            self.interfaceOrientation != UIDeviceOrientationPortraitUpsideDown){
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDuration:0.5];
            
            [self.scrollView setFrame:CGRectMake(CGRectGetMinX(self.view.bounds) + CGRectGetWidth(self.view.bounds)/4 - 20, CGRectGetMinY(self.view.bounds) + 25, CGRectGetWidth(self.view.bounds)/2 + 40,self.view.frame.size.height)];
            [UIView commitAnimations];
            
        }else
        {
            
            self.scrollView.frame = CGRectMake(CGRectGetMinX(self.view.bounds) + 20,CGRectGetMinY(self.view.bounds) + 25,CGRectGetWidth(self.view.bounds) - 40,self.view.frame.size.height);
            
        }
    }
    
    
}

 

posted @ 2013-08-01 17:12  如来藏  阅读(317)  评论(0编辑  收藏  举报