我在UITableView (或UICollextionView )的底部添加一个视图,但不是作为单元格或页脚,而只是将其添加到表视图本身(因此,添加到UIScrollView),然后使用滚动的contentInset。这样做的目的是将视图放在表内容的底部,而不是内容的末尾。这样,如果内容比表格的边界短,则视图是可见的(左图),但如果内容较高,则视图出现在内容的末尾(右图)。

这是代码的摘录:
let yCoord = collectionView.contentSize.height >= view.frame.height - viewHeight
? collectionView.contentSize.height
: view.frame.height - viewHeight
let bottomView = UIView(frame: CGRect(x: 0, y: yCoord, width: collectionView.contentSize.width, height: viewHeight))
collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: viewHeight, right: 0)
collectionView.addSubview(bottomView)我想知道这种方法是否会以任何方式被认为是有害的,例如,AL问题,旋转,表的重新加载……我不确定有什么,事实上,这是一种与iOS 6之前的拉刷新功能非常相似的方法,但在将其推向生产之前,我更愿意确定这一点。
如果这可以被认为是一个问题,除了添加一个新的单元之外,您还提出了什么解决方案?
发布于 2019-01-22 18:35:12
如果你想要上面的功能,你必须在scrollView中使用tableView和底部视图,并使用内容观察器根据内容为tableView赋予动态高度,并将底部视图保持在滚动视图的底部,并将tableView作为从上到下的视图,并将greaterThan或equal作为属性。
希望它能起作用。
发布于 2019-01-22 19:25:29
在下面的代码中尝试一下。使您的页脚视图颜色清晰,并将另一个视图放入带有底部、左侧、右侧和高度约束的页脚视图中
@IBOutlet weak var footerView: UIView!
//MARK:- View Controller life cycle method
override func viewDidLoad() {
super.viewDidLoad()
yourTableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
yourTableView.removeObserver(self, forKeyPath: "contentSize")
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "contentSize"){
let contentHeight: CGFloat = yourTableView.contentSize.height
let tableHeight = yourTableView.frame.size.height
if contentHeight < tableHeight {
footerView.frame = CGRect(x: footerView.frame.origin.x, y: footerView.frame.origin.y, width: footerView.frame.size.width, height: CGFloat(tableHeight - contentHeight + 44.0))
// 44.0 is your initial footer height which you put in xib
} else {
footerView.frame = CGRect(x: footerView.frame.origin.x, y: footerView.frame.origin.y, width: footerView.frame.size.width, height: 44.0)
// 44.0 is your initial footer height which you put in xib
}
}
}发布于 2019-01-23 03:10:12
我更喜欢创建一个UIView子类,它包装表视图和浮动脚注视图。我避免修改完全由框架管理的类的视图层次结构,使用子类可以很容易地在其他地方使用这种布局。使用滚动视图委托scrollViewDidScroll事件,它将在用户每次滚动时更新页脚视图框。顺便说一句,我当然会使用约束来放置我的页脚视图,而不是使用可能发生变化的帧。
在你的实现中有什么有害的东西吗?
你没有提到你在哪里添加了代码。如果它是viewDidLoad,那么如果您使用自动布局,并且视图的框架因旋转或特征集合更改而更改,那么您将拥有trubles。此外,如果您的内容不是静态的,调用reload data将使表视图的内容大小失效,这也将创建一个错位的页脚视图。
我们应该把这些代码行放在哪里?
我们是否应该观察表视图框架及其内容,以便在每次更改页脚视图框架时更新它们?
当然不是!苹果已经提供了我们需要的所有活动。至少,将这些行放在viewDidLayoutLayouts中,并确保不会多次添加页脚视图。每次布局控制器的视图时,都会调用viewDidLayoutLayouts。
https://stackoverflow.com/questions/54303801
复制相似问题