Hagrid 1.0.2

Hagrid 1.0.2

Ondrej Rafaj维护。



Hagrid 1.0.2

  • rafiki270

Hagrid: Grid layout library for iOS, tvOS and macOS

Platform Cocoapods Compatible Carthage compatible

为苹果平台带来正确的栅格布局!


使用gridView.config.displayGrid = true启用网格布局,如截图所示

可用的组件

  • GridView - 基础网格视图
  • GridScrollView - 拥有所有UIScrollView属性的网格视图 (在macOS上不可用)
  • GridViewController - 基于UIViewController的基础网格视图 (在macOS上不可用)
  • GridScrollViewController - 允许滚动的网格视图视图控制器 (在macOS上不可用)

用法

// Add an album cover from the first column to 5
// (please note we are using just a plain Int instead of the Position object below)
// To make the album square we have used a custom closure with SnapKit's own `ConstraintMaker` (make) at the end of the method
gridView.add(subview: albumCover, space: 5) { make in
    make.height.equalTo(self.albumCover.snp.width)
}

// Add album title label, stretching (12px padding) from the album cover image to the end of the grid view
gridView.add(subview: albumTitleLabel, from: .relation(albumCover, margin: 12), space: .last)

// Reusable position for the remaining two album labels. iPhones in portrait will take the rest of the screen,
// on bigger devices or in landscape mode these labels finish two columns from the end of the grid view (.reversed(2)). 
let subtitlesLast: Position = .custom { traitCollection in
    return self.gridView.bounds.size.width <= 414 ? .last : .reversed(2)
}

// Place artist label below the album title with margin of 2px, match the left position of the album title label
// and the previously calculated `subtitlesLast` for end position
gridView.add(subview: artistLabel, .below(albumTitleLabel, margin: 2), from: .match(albumTitleLabel), space: subtitlesLast)

// Place year released label under the artist name label, rest is same as above
gridView.add(subview: yearLabel, .below(artistLabel, margin: 2), from: .match(artistLabel), space: subtitlesLast)

// Place a purchase button next to the artist and year label on bigger screens and under on smaller ones (iPhone in portrait etc)
// For the start position you either start 12px from the album on smaller screens or second column from the right on bigger ones
// (note you can also use `UITraitCollection` in both cases if needed)
gridView.add(subview: purchaseButton, .custom({ traitCollection in
    if self.gridView.bounds.size.width <= 414 {
        return .below(self.yearLabel, margin: 3)
    } else {
        return .match(self.artistLabel)
    }
}), from: .custom({ _ in
    if self.gridView.bounds.size.width <= 414 {
        return .relation(self.albumCover, margin: 12)
    } else {
        return .relation(self.artistLabel, margin: 6)
    }
}), space: .last) { make in
    make.height.equalTo(28)
}

// Place the star rating label above the separator. The initial position is not set and will be dynamic, the whole thing will stretch to the end
gridView.add(subview: ratingLabel, .above(separator, margin: 12), from: .dynamic, space: .last) { make in
    make.height.equalTo(28)
}

// Add a 1px separator below any of the components that could reach it and space it 12px from the lowest one
gridView.add(subview: separator, .below([albumCover, yearLabel, purchaseButton], margin: 12)) { make in
    make.height.equalTo(1)
}

// Add the long description copy which takes the full width and is placed below the separator
gridView.add(subview: aboutLabel, .below(separator, margin: 12))

自定义

有几种方法可以自定义网格视图

列数

每个网格视图都可以有任意数量的列,具体取决于开发者决定在哪个视图中使用多少列。默认值是12。

// Set number of columns
gridView.config.numberOfColumns = 24

网格视图外部填充

您可以为整个网格视图设置topleftright填充,如下所示

gridView.config.padding = .full(top: 6, left: 12, right: 12)

动态大小

通过设置以下选项可以启用网格视图的动态大小

gridView.config.automaticVerticalSizing = true

定位

填充

可用的填充选项有

/// None
.none

/// Top
.top(CGFloat)

/// Left
.left(CGFloat)

/// Right
.right(CGFloat)

/// Sides (left, right)
.horizontal(left: CGFloat, right: CGFloat)

/// Full (top, left, right, bottom)
.full(top: CGFloat, left: CGFloat, right: CGFloat)

垂直定位

对于垂直定位,您可以直接设置像素值(IntFloat,稍后将转换为.exactly(fromTop: CGFloat))或使用以下值之一

/// Top of the grid
.toTop

/// Exact value from the top
.exactly(fromTop: CGFloat)

/// Match top of another view
.match(UIView, margin: CGFloat = 0)

/// Maintains a position under a set of elements
.below([UIView], margin: CGFloat = 0)

/// Below another view (view, margin)
.below(UIView, margin: CGFloat = 0)

/// Above another view (view, margin)
.above(UIView, margin: CGFloat = 0)

/// Custom vertical position for a given size class (trait collection, not available on macOS)
.custom(((_ traitCollection: UITraitCollection) -> Vertical))

水平定位

对于水平定位,您可以直接使用整数(Int)来指定确切的列或使用以下方法之一

/// First column
.first

/// Specific column on the grid
.col(Int)

/// Last column on a grid view
.last

/// N-th column from the end
.reversed(Int)

/// Up to another element
.relation(UIView, margin: CGFloat = 0)

/// Match position of another view
.match(UIView, margin: CGFloat = 0)

/// Dynamic position
.dynamic

/// Custom position for a given size class (trait collection, not available on macOS)
.custom(((_ traitCollection: UITraitCollection) -> Position))

视图控制器

可供子类化和方便使用的是 GridViewControllerGridScrollViewController,其中 gridView 属性替代了标准的 view

class MyViewController: GridViewController { }

调试

要启用背景网格,执行以下操作:

// Display grid
gridView.config.displayGrid = true

Boost - 一个开源企业应用商店

Boost 的核心包,一个完全开源的企业级应用商店,使用 Swift 编写!

代码贡献

我们喜欢拉取请求,从不嫌多......所以如果您有一个有趣的改进、bug修复或一个新特性,请务必联系我们。在开始开发之前,如果您对某些事情不确定,您始终可以通过我们的 Slack 联系我们的开发团队和产品团队。

致谢

作者

Ondrej Rafaj (@rafiki270 on GitHub, Twitter, LiveUI Slack and Vapor Slack))

鸣谢

感谢SnapKit团队开发如此出色的工具!

许可证

MIT协议;更多信息请参阅LICENSE文件。