tableView的高度宽度动态布局

一://根据滑动慢慢改变frame

func scrollViewDidScroll(_ scrollView: UIScrollView){

        guard isCommPush == true else {

            return

        }

        let contentOffsetY = scrollView.contentSize.height

        print("contentOffsetY:\(contentOffsetY)")

        //如果已经显示完毕了,就不再执行下面的方法,否则的话滑动的时候页面会一直上下抖动(改变再弹回来)

        guard (selectCommView?.bigViewHeight.constant)! < 44+contentOffsetY else{

            return

        }

        

        // 向下滑动时<0,向上滑动时>0

        let offsetY = scrollView.contentOffset.y

        print("offsetY:\(offsetY)")

        if offsetY > 0 {

            let changeHeight = 400+offsetY

            //70是可以显示的最上面的间距(就是选择小区框的下面)

            if changeHeight > ((selectCommView?.myObserVer?.view.frame.size.height)!-70) {

                selectCommView?.bigViewHeight.constant = (selectCommView?.myObserVer?.view.frame.size.height)!-70

                //小于默认高度,显示默认高度

            }else

                if changeHeight < 400 {

                    selectCommView?.bigViewHeight.constant = 400+44

                }else{

                    selectCommView?.bigViewHeight.constant = 44+changeHeight

            }

        }

        

    }

二:contenSize等于所有cell(内容)的size

1、在viewDidLoad中添加观察者

  1. [self.tableView addObserver:self forKeyPath:@"contentSize" options:0 context:NULL];  

2、重写observeValueForKeyPath方法,一旦UITableView的contentSize发生改变,就会调用这个方法

  1. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {  
  2.     CGRect frame = self.tableView.frame;  
  3.     frame.size = self.tableView.contentSize;  
  4.     self.tableView.frame = frame;  
  5. }  

猜你喜欢

转载自blog.csdn.net/weixin_39872861/article/details/81179204