Skip to content

Commit

Permalink
Merge pull request #502 from square/gil/gravity-frame-fix
Browse files Browse the repository at this point in the history
CONV-1435: Gravity layout frame change fix
  • Loading branch information
gilgtm authored Aug 17, 2023
2 parents 6e5f113 + 1c78bca commit e47336c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixed

- The `verticalLayoutGravity` behavior now takes into account `frame` changes so that the scroll position relative to the bottom remains unchanged when the `frame` changes.

### Added

### Removed
Expand Down
17 changes: 16 additions & 1 deletion Demo/Sources/Demos/Demo Screens/ChatDemoViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class ChatDemoViewController : UIViewController {

self.navigationItem.rightBarButtonItems = [
UIBarButtonItem(title: "New message", style: .plain, target: self, action: #selector(addAtBottom)),
UIBarButtonItem(title: "Grow Frame", style: .plain, target: self, action: #selector(growFrame)),
UIBarButtonItem(title: "Shrink Frame", style: .plain, target: self, action: #selector(shrinkFrame)),
]

self.view.addSubview(listView)
Expand All @@ -29,7 +31,6 @@ final class ChatDemoViewController : UIViewController {
listView.customScrollViewInsets = { [weak self] in
guard let self = self else { return .init() }
let inset = max(self.footerHeight, self.keyboardHeight - self.view.safeAreaInsets.bottom)
print("new bottom inset:", inset)
let insets = UIEdgeInsets(
top: 0,
left: 0,
Expand Down Expand Up @@ -88,6 +89,20 @@ final class ChatDemoViewController : UIViewController {
self.reload()
}

@objc private func shrinkFrame() {
view.frame.size.height -= 22
reload()
}

@objc private func growFrame() {
guard view.frame.size.height < view.superview!.frame.size
.height else {
return
}
view.frame.size.height += 22
reload()
}

private var footerHeight: CGFloat = 80
private var keyboardHeight: CGFloat = 0
private let listView = ListView()
Expand Down
20 changes: 20 additions & 0 deletions ListableUI/Sources/ListView/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1716,4 +1716,24 @@ final class CollectionView : ListView.IOS16_4_First_Responder_Bug_CollectionView

}
}

override var frame: CGRect {
get {
super.frame
}
set {
// With bottom gravity enabled keep the scroll distance to the bottom unchanged
if layoutDirection == .vertical && verticalLayoutGravity == .bottom {
guard newValue != super.frame else {
return
}
let oldValue = super.frame
let offsetY = contentOffset.y
super.frame = newValue
contentOffset.y = offsetY - (newValue.height - oldValue.height)
} else {
super.frame = newValue
}
}
}
}

0 comments on commit e47336c

Please sign in to comment.