如何根据swift中UITableView的高度增加父视图的高度? [英] How to increase the height of parent view according to height of UITableView in swift?

查看:482
本文介绍了如何根据swift中UITableView的高度增加父视图的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的屏幕层次低于此。

I have below hierarchy of my screen.

-ScrollView 
 -ContentView
   -View1
   -View2
   -View3
   -Collectioview
     -CollectioviewCell 
       -Tableview 
         -Tableviewcell 
            -Content

现在这里我的tableview内容是动态的,因此tableview单元格的高度不能是静态的。桌面视图的高度将根据单元格的数量而增加。每个单元格的高度。

Now here my content of tableview is dynamic so height of tableview cell can't be static.So height of tableview will increase according to number of cell & height of each cell.

根据Tableview高度,集合视图的高度应该增加,这会导致滚动视图内容视图的高度增加。

Based on Tableview height, Height of collection view should increase which results in increase in height of scroll view content view.

但我无法理解如何用更少的代码实现这一目标?
或者可以以编程方式完成吗?

But I am not able to understand how should achieve this with less code? or can it be done programatically ?

推荐答案

最通用的方法是观察你的scroll- / table- / collectionView的 contentSize 属性并相应地改变大小。

The most universal way is to observe contentSize property of your scroll-/table-/collectionView and to change size accordingly.

要实现这一点,您应该:

To achieve this you should:


注意 :示例将用于UIScrollView,但它可以应用于任何collectionView

Note: examples will be for UIScrollView, but it can be applied for any collectionView




  • 添加观察者到您的视图

    • add observer to your view

      scrollViewToObserveSizeOf.addObserver(observerObject, forKeyPath: "contentSize", options: [.old, .new], context: nil)
      


    • 覆盖 observeValue 方法

      override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
          if let scrollView = object as? UIScrollView,
              scrollView == scrollViewToObserveSizeOf,
              keyPath == "contentSize" {
              // Do whatever you want with size
              // You can get new size from scrollView.contentSize
              // or change?[NSKeyValueChangeKey.newKey] as? CGSize for new value
              // and change?[NSKeyValueChangeKey.oldKey] as? CGSize for old value
              //
              // ! Do not force unwrap this values, just never use force unwrap, 
              // everything could happen
          } else {
              // ! Do not forget to call super.observeValue(forKeyPath:of:change:context) 
              // if it is not object you added observer to
              super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
          }
      }
      


    • 例如,要将它应用到UITableView,只需检查 observeValue 方法中的对象是否为 UITableView type。

      To apply it, for example, to UITableView just check if object in observeValue method is of UITableView type.

      只要更改了集合视图的内容大小,并且您不需要了解视图是否已完成加载或其他任何内容,就会调用此方法。

      This method is called whenever content size of your collection view is changed and you do not need to know anything about if your view has finished loading or whatever.

      这篇关于如何根据swift中UITableView的高度增加父视图的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆