UIView 不调整大小以匹配设备
UIView not adjusting size to match device
更新:通过将 init(coder) 中的代码移动到 layoutSubviews,我能够解决下面的问题。感谢您的输入!
我有一个观点 class,它基本上构建了一个棋盘,并且限制为 1:1 比率和来自其他 UI 的一定数量的点数。在 init(coder) 中,我正在绘制棋盘空间,然后调用一个函数从我的控制器中绘制棋子。
但是,当我 运行 模拟器时,它不会根据设备加载,只会根据我目前在 Interface Builder 中 select 编辑的内容加载。
例如,我在 IB 中有 iPhone 7 Plus selected,但 运行 iPhone 7 上的模拟器导致电路板离开屏幕。如果我在 IB 和模拟器中 select iPhone 7,它工作正常。
我一直难以找到这个问题的答案。任何帮助将不胜感激。
Board too big on iPhone 7
Because Storyboard set to iPhone 7 Plus
app首先在init(coder)中绘制空格:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let spaces: CGFloat = CGFloat(Constants.numberOfSpaces)
spaceMargin = 16
spaceWidth = (bounds.width - (spaces - 1) * spaceMargin) / spaces
spaceHeight = spaceWidth
pawnTop = bounds.height * (-3/304)
pawnMargin = bounds.width * (1/304)
pawnHeight = bounds.height * (48/304)
pawnWidth = bounds.width * (44/304)
pawnDict = [:]
spaceDict = [:]
for column in 0..<Constants.numberOfSpaces {
for row in 0..<Constants.numberOfSpaces {
let coordinate = try! Coordinate(column: column, row: row)
drawSpace(at: coordinate)
}
}
}
然后这是我的控制器调用的:
func updateBoard() {
guard let board = dataSource?.currentBoard() else {
return
}
// Remove previous pawns
for subview in subviews {
if let pawnView = subview as? PawnView {
pawnView.removeFromSuperview()
}
}
pawnDict = [:]
// Draw new pawns based on placement in board model
for column in 0..<Constants.numberOfSpaces {
for row in 0..<Constants.numberOfSpaces {
let coordinate = try! Coordinate(column: column, row: row)
let space = board.getSpace(coordinate)
if space.hasPawn {
drawPawn(at: coordinate)
}
}
}
}
我认为您的问题是由于 UIView
不知道它在 init
中的界限。查看视图生命周期 here。要解决您的问题,您有两种选择:使用自动布局来绘制您的电路板 - 或者 - 等到视图生命周期的后期绘制它。
更多信息
更新:通过将 init(coder) 中的代码移动到 layoutSubviews,我能够解决下面的问题。感谢您的输入!
我有一个观点 class,它基本上构建了一个棋盘,并且限制为 1:1 比率和来自其他 UI 的一定数量的点数。在 init(coder) 中,我正在绘制棋盘空间,然后调用一个函数从我的控制器中绘制棋子。
但是,当我 运行 模拟器时,它不会根据设备加载,只会根据我目前在 Interface Builder 中 select 编辑的内容加载。
例如,我在 IB 中有 iPhone 7 Plus selected,但 运行 iPhone 7 上的模拟器导致电路板离开屏幕。如果我在 IB 和模拟器中 select iPhone 7,它工作正常。
我一直难以找到这个问题的答案。任何帮助将不胜感激。
Board too big on iPhone 7
Because Storyboard set to iPhone 7 Plus
app首先在init(coder)中绘制空格:
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
let spaces: CGFloat = CGFloat(Constants.numberOfSpaces)
spaceMargin = 16
spaceWidth = (bounds.width - (spaces - 1) * spaceMargin) / spaces
spaceHeight = spaceWidth
pawnTop = bounds.height * (-3/304)
pawnMargin = bounds.width * (1/304)
pawnHeight = bounds.height * (48/304)
pawnWidth = bounds.width * (44/304)
pawnDict = [:]
spaceDict = [:]
for column in 0..<Constants.numberOfSpaces {
for row in 0..<Constants.numberOfSpaces {
let coordinate = try! Coordinate(column: column, row: row)
drawSpace(at: coordinate)
}
}
}
然后这是我的控制器调用的:
func updateBoard() {
guard let board = dataSource?.currentBoard() else {
return
}
// Remove previous pawns
for subview in subviews {
if let pawnView = subview as? PawnView {
pawnView.removeFromSuperview()
}
}
pawnDict = [:]
// Draw new pawns based on placement in board model
for column in 0..<Constants.numberOfSpaces {
for row in 0..<Constants.numberOfSpaces {
let coordinate = try! Coordinate(column: column, row: row)
let space = board.getSpace(coordinate)
if space.hasPawn {
drawPawn(at: coordinate)
}
}
}
}
我认为您的问题是由于 UIView
不知道它在 init
中的界限。查看视图生命周期 here。要解决您的问题,您有两种选择:使用自动布局来绘制您的电路板 - 或者 - 等到视图生命周期的后期绘制它。
更多信息