generated from BB9z/iOS-Project-Template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add control over list scrolling position. Improve test stability (#7)
- Loading branch information
Showing
8 changed files
with
348 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// BottomAlignTableView.swift | ||
// B9ChatAI | ||
// | ||
// Copyright © 2023 B9Software. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class BottomAlignTableView: UITableView { | ||
|
||
override var contentSize: CGSize { | ||
didSet { | ||
if oldValue == contentSize { | ||
return | ||
} | ||
debug("contentSize: \(oldValue) => \(contentSize)") | ||
adjustContentInset() | ||
|
||
let yDiff = contentSize.height - oldValue.height | ||
var offset = contentOffset | ||
offset.y += yDiff | ||
if offset.y < -safeAreaInsets.top { | ||
debug("reach top, skip") | ||
return | ||
} | ||
debug("will adjust offset: \(offset)") | ||
contentOffset = offset | ||
ignoralNextOffsetUpdate = CFAbsoluteTimeGetCurrent() | ||
} | ||
} | ||
|
||
private var ignoralNextOffsetUpdate: CFAbsoluteTime = 0 | ||
override var contentOffset: CGPoint { | ||
get { | ||
super.contentOffset | ||
} | ||
set { | ||
if ignoralNextOffsetUpdate > 0 { | ||
if CFAbsoluteTimeGetCurrent() - ignoralNextOffsetUpdate < 1e-3 { | ||
debug("contentOffset: ignoral system adjust") | ||
return | ||
} | ||
ignoralNextOffsetUpdate = 0 | ||
} | ||
let oldValue = super.contentOffset | ||
super.contentOffset = newValue | ||
if oldValue.y != newValue.y { | ||
debug("contentOffset: \(oldValue.y) => \(newValue.y)") | ||
} | ||
} | ||
} | ||
|
||
override func safeAreaInsetsDidChange() { | ||
super.safeAreaInsetsDidChange() | ||
adjustContentInset() | ||
} | ||
|
||
private func adjustContentInset() { | ||
let oldValue = contentInset.top | ||
let newValue = max(bounds.height - safeAreaInsets.top - safeAreaInsets.bottom - contentSize.height, 0) | ||
if abs(newValue - oldValue) < 1 { | ||
return | ||
} | ||
var inset = contentInset | ||
inset.top = newValue | ||
contentInset = inset | ||
debug("adjust inset: \(inset)") | ||
} | ||
|
||
private func debug(_ value: @autoclosure () -> CustomStringConvertible) { | ||
// print(value().description) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.