【问题标题】:Programmatic auto layout程序化自动布局
【发布时间】:2018-09-04 04:43:19
【问题描述】:

所以我有一个 UIImageViews 的二维数组,它显示一个游戏屏幕(就像一个棋盘,但在不同的行/列中)

目前我正在使用以下自动布局代码:

func setConstraints() {
    for i in 0...rows-1 {
        for j in 0...cols-1 {
                //First image
            if (i == 0 && j == 0) {
                floorImageView[i][j].anchor(top: self.view.safeAreaLayoutGuide.topAnchor, leading: self.view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 180, left: 10, bottom: 0, right: 0), size: .init(width: 40, height: 40))
                cellImageView[i][j].anchor(top: self.view.safeAreaLayoutGuide.topAnchor, leading: self.view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 180, left: 10, bottom: 0, right: 0), size: .init(width: 40, height: 40))
            }
                //Middle rows
            else if (i == 0 && j > 0) {
                floorImageView[i][j].anchor(top: self.view.safeAreaLayoutGuide.topAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: nil, padding: .init(top: 180, left: 0, bottom: 0, right: 0), size: .init(width: 40, height: 40))
                cellImageView[i][j].anchor(top: self.view.safeAreaLayoutGuide.topAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: nil, padding: .init(top: 180, left: 0, bottom: 0, right: 0), size: .init(width: 40, height: 40))
            }
                //First image of every row
            else if (i > 0 && j == 0) {
                floorImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: self.view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 10, bottom: 0, right: 0), size: .init(width: 40, height: 40))
                cellImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: self.view.safeAreaLayoutGuide.leadingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 10, bottom: 0, right: 0), size: .init(width: 40, height: 40))
            }
                //Middle js
            else if (i > 0 && j > 0) {
                floorImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: 40, height: 40))
                cellImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: nil, padding: .init(top: 0, left: 0, bottom: 0, right: 0), size: .init(width: 40, height: 40))
            }
                //Last row
            else if (i > 0 && j == cols-1) {
                floorImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: self.view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 200, right: 10), size: .init(width: 40, height: 40))
                cellImageView[i][j].anchor(top: floorImageView[i-1][j].bottomAnchor, leading: floorImageView[i][j-1].trailingAnchor, bottom: nil, trailing: self.view.safeAreaLayoutGuide.trailingAnchor, padding: .init(top: 0, left: 0, bottom: 200, right: 10), size: .init(width: 40, height: 40))
            }
        }
    }
}

结果是这样的:

但我真正想要的是,无论关卡的大小如何,它始终保持在显示屏的中心 (iPhone X),而不会在侧面切掉部分关卡或留下大的间隙。

任何帮助将不胜感激!

【问题讨论】:

  • 请注意,view.anchor() 方法是 LetsBuildThatApp 的扩展,该方法本身很容易解释
  • 你真的可以使用UICollectionView

标签: ios swift user-interface autolayout swift4


【解决方案1】:

我通常不倾向于使用大量 pod,但Snapkit 确实会通过更简洁和描述性的代码帮助您解决问题...

【讨论】:

    猜你喜欢
    • 2017-12-05
    • 2018-06-21
    • 2021-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多