zoukankan      html  css  js  c++  java
  • 键盘遮挡的处理

    .m文件相关方法

    @interface DemoViewController ()<UITextFieldDelegate> {
        UITextField * _activeField;  //作为第一响应者的UITextField
        CGRect        _originFrame;  //scrollView的初始frame
    }
    @end
    
    @implementation DemoViewController
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            [self registerForKeyboardNotifications]; //注册键盘事件
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void) viewDidLayoutSubviews {
        [super viewDidLayoutSubviews];
        self.bgScrollView.backgroundColor = [UIColor lightGrayColor];
    
        _originFrame = self.bgScrollView.frame;  //记录scrollView的初始frame
    }
    
    - (void)registerForKeyboardNotifications {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWasShown:)
                                                     name:UIKeyboardWillShowNotification object:nil];
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillBeHidden:)
                                                     name:UIKeyboardWillHideNotification object:nil];
        
    }
    
    - (void)keyboardWillBeHidden:(NSNotification*)aNotification {
        _bgScrollView.frame = _originFrame;  //键盘隐藏,复原scrollView的frame为初始值
    }
    
    - (void)keyboardWasShown:(NSNotification*)aNotification {
        NSDictionary* info = [aNotification userInfo];
        CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;  //键盘尺寸,这个在输入中英文的时候是不一样的
    
        //先将scrollview的frame复原,避免因为键盘中英文切换而造成frame的两次该改变
        self.bgScrollView.frame = _originFrame;
        CGRect scrollViewFrame = self.bgScrollView.frame;
        scrollViewFrame.size.height -= kbSize.height;
        self.bgScrollView.frame = scrollViewFrame;
        
        self.bgScrollView.contentSize = CGSizeMake(320, self.fTxtField.frame.size.height + self.fTxtField.frame.origin.y);
        [_bgScrollView scrollRectToVisible:_activeField.frame animated:YES];
        
    }
    
    - (void) textFieldDidBeginEditing:(UITextField *)textField {
        _activeField = textField;
    }
    - (void) textFieldDidEndEditing:(UITextField *)textField{} - (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { if ([string isEqualToString:@" "]) { [textField resignFirstResponder]; return NO; } return YES; }

    界面布局如下:

  • 相关阅读:
    codeforces 869E. The Untended Antiquity(二维树状数组,随机化)
    bzoj 3083: 遥远的国度(树上换根操作,树剖+询问整个子树)
    hdu 5534 Partial Tree(dp+降唯,好题)
    AtCoder Regular Contest 075 E
    hihocoder 1387 A Research on "The Hundred Family Surnames"(树,lca,求同一颜色的直径)
    hdu 5458 Stability(生成树,树链剖分,好题)
    推荐一套个人ui组件库
    回望2019,期盼2020
    如何从产品的角度对待自己的博客
    致一名迷茫的我
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3937278.html
Copyright © 2011-2022 走看看