Skip to content

Instantly share code, notes, and snippets.

@devshok
Last active November 30, 2023 09:22
Show Gist options
  • Save devshok/42feb0dd51dd596207e03d1085dc6eaf to your computer and use it in GitHub Desktop.
Save devshok/42feb0dd51dd596207e03d1085dc6eaf to your computer and use it in GitHub Desktop.
import UIKit.UIScrollView
// MARK: - CONTENT CENTRALIZATION
extension UIScrollView {
/// Call this method on your `UIScrollView` instance
/// when you need to center its content
/// relative to the width and height of the `UIScrollView`.
///
/// The function first attempts to centralize along the Y-axis
/// if the content height is less than the height of your instance.
///
/// Then, it attempts to centralize along the X-axis
/// if the content width is less than the width of your instance.
///
/// - Warning: The only content of your `UIScrollView`
/// should be a `contentView` (`UIView`), inside which
/// you place all the content.
func centralizeContentIfAvailableSpaceExists() {
self.centralizeContentByYIfAvailableSpaceExists()
self.centralizeContentByXIfAvailableSpaceExists()
}
/// Call this method on your `UIScrollView` instance
/// when you need to center the content
/// along the Y-axis (vertically) relative to the height of `UIScrollView`.
///
/// This function is appropriate when three conditions are met:
///
/// **1.** The content height of your instance is less
/// than the height of your `UIScrollView`.
///
/// **2.** You need to place the content inside your
/// `UIScrollView` centered along the Y-axis relative to `UIScrollView`.
///
/// **3.** The only content of your `UIScrollView`
/// is a certain `contentView` (`UIView`).
func centralizeContentByYIfAvailableSpaceExists() {
guard let subview = self.subviews.first else {
return
}
let subviewHeight = subview.bounds.size.height
let scrollViewHeight = self.bounds.size.height
let availableYSpaceForSubview = scrollViewHeight - subviewHeight
guard availableYSpaceForSubview > .zero else {
return
}
let availableSpaceByTopForSubview = availableYSpaceForSubview / 2
let offsetByY = scrollViewHeight / 2
let updatedY = offsetByY + availableSpaceByTopForSubview
let oldX = self.center.x
let updatedCenter = CGPoint(x: oldX, y: updatedY)
self.center = updatedCenter
}
/// Call this method on your `UIScrollView` instance
/// when you need to center the content
/// along the X-axis (horizontally) relative to the width of `UIScrollView`.
///
/// This function is appropriate when three conditions are met:
///
/// **1.** The content width of your instance is less
/// than the width of your `UIScrollView`.
///
/// **2.** You need to place the content inside your
/// `UIScrollView` centered along the X-axis relative to `UIScrollView`.
///
/// **3.** The only content of your `UIScrollView`
/// is a certain `contentView` (`UIView`).
func centralizeContentByXIfAvailableSpaceExists() {
guard let subview = self.subviews.first else {
return
}
let subviewWidth = subview.bounds.size.width
let scrollViewWidth = self.bounds.size.width
let availableXSpaceForSubview = scrollViewWidth - subviewWidth
guard availableXSpaceForSubview > .zero else {
return
}
let availableSpaceByLeftForSubview = availableXSpaceForSubview / 2
let offsetByX = scrollViewWidth / 2
let updatedX = offsetByX + availableSpaceByLeftForSubview
let oldY = self.center.y
let updatedCenter = CGPoint(x: updatedX, y: oldY)
self.center = updatedCenter
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment