zoukankan      html  css  js  c++  java
  • iOS开发——使用Autolayout弹出键盘

    参考:

    http://segmentfault.com/q/1010000002420050

    http://blog.csdn.net/qq448631961/article/details/40345653

    思路:

    在整个View下面塞进一个高度为0的视图(使用低优先级约束),当键盘改变时改变该View的高度即可。

    constraint 有一个唯一可以修改的属性 constant,我承认它的名字确实很具有迷惑性。。。
    以题主提到的高度问题为例,可以保存这个高度 constraint 的引用,在键盘出现和收起时

    - (void)keyboardDidShowNotification
    {
        self.viewHeightConstraint.constant = 100.f;
        [self.myView layoutIfNeed];
    }

    - (void)keyboardDidHideNotification
    {
        self.viewHeightConstraint.constant = 300.f;
        [self.myView layoutIfNeed];
    }

    我的代码:

    注册通知:

       1:   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillChangeFrameNotification object:nil];

    约束

    @property (nonatomic,strong) NSLayoutConstraint* keyboardConstraint;

    初始为0

       1:  self.keyboardConstraint = [NSLayoutConstraint constraintWithItem:self.keyboardView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
       1:  - (void)keyboardWillShow:(NSNotification *)notification{
       2:      CGRect kbFrame = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
       3:      self.keyboardConstraint.constant = kbFrame.size.height;
       4:      [self.view addConstraint:self.keyboardConstraint];
       5:      //    [self.inputView needsUpdateConstraints];
       6:      [self.view setNeedsLayout];
       7:      [self.view layoutIfNeeded];
       8:      if (self.messages.count > 0){
       9:      NSIndexPath *idxP = [NSIndexPath indexPathForRow:(self.messages.count - 1) inSection:0];
      10:          [self.tableView scrollToRowAtIndexPath:idxP atScrollPosition:UITableViewScrollPositionBottom animated:YES];}
      11:      
      12:  }
      13:   
      14:  - (void)keyboardWillHidden{
      15:      
      16:      [self.view endEditing:YES];
      17:      [UIView animateWithDuration:0.25 animations:^{
      18:          //        self.keyboardConstraint.constant = 0;
      19:          self.keyboardConstraint.constant = 0;
      20:          //        [self.view removeConstraint:self.keyboardConstraint];
      21:          [self.view setNeedsLayout];
      22:          [self.view layoutIfNeeded];
      23:      }];
      24:      
      25:  }
  • 相关阅读:
    RxJava开发精要3-向响应式世界问好
    RxJava开发精要2-为什么是Observables?
    RxJava开发精要1-从.NET到RxJava
    为你的应用加速
    Android最佳性能实践(二)——分析内存的使用情况
    Android最佳性能实践(一)——合理管理内存
    Android 性能优化之使用MAT分析内存泄露问题
    给 Android 开发者的 RxJava 详解
    优化 Android 线程和后台任务开发
    资深谷歌安卓工程师对安卓应用开发的建议
  • 原文地址:https://www.cnblogs.com/zeyang/p/4457968.html
Copyright © 2011-2022 走看看