【发布时间】: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