zoukankan      html  css  js  c++  java
  • 键盘弹出后上提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);
                
            }
        }
        
        
    }

  • 相关阅读:
    优秀案例:18个美丽的图片在网页设计中的使用
    推荐几款很棒的 JavaScript 表单美化和验证插件
    25套用于 Web UI 设计的免费 PSD 网页元素模板
    字体排布艺术:30例可圈可点的版式设计欣赏
    Twitter Bootstrap 3.0 正式发布,更好地支持移动端开发
    引领网页设计潮流的优秀网页作品赏析《第三季》
    Codrops 优秀教程:实现效果精美的多层推拉菜单
    字体大宝库:设计师必备的精美免费英文字体
    优秀前端开发教程:超炫的 Mobile App 3D 演示
    推荐35个新鲜出炉的响应式 Web 设计实例
  • 原文地址:https://www.cnblogs.com/lisa090818/p/3230494.html
Copyright © 2011-2022 走看看