在segue中,我有一个UIVIew
,它具有一些约束,使其布局与顶部,左侧和右侧的屏幕以及底部的按钮相同:
然后从ViewController
中以编程方式添加UIButtons
,如下所示:
override func viewDidLoad() {
super.viewDidLoad()
drawCards()
}
private func drawCards(){
let deck = Deck()
var rowIndex = 0
var columnIndex = 0
let cardWidth = Int(Float((deckView.bounds.width) / 7))
let cardHeight = Int(Float(cardWidth) * 1.5)
for card in deck.cards{
let x = (cardWidth / 2) * columnIndex
let y = cardHeight * rowIndex
let button = CardButton()
button.delegate = self
button.setCard(card: card, x: x, y: y, width: cardWidth, height: cardHeight)
deckView.addSubview(button)
columnIndex += 1
if(columnIndex == 13){
columnIndex = 0
rowIndex += 1
}
}
}
预期的行为是此函数应采用UIView
的大小(称为deckView
),插入UIButtons
并相互堆叠,并且在设备之间始终具有相同的布局。但是,在UIButtons
超出UIView
的范围内,它在iPhone上的显示效果与在iPad上的预期效果相同:
据我所知,这是因为X
和Y
值计算不正确,因为UIView
(deckView
)的宽度未正确检索(或按预期方式) 。
为什么UIView
的宽度没有按预期方式获得?(即,为什么它在iPhone上能按预期工作,但在iPad上却不能按预期工作)
将drawCards()
从viewDidLoad
移动到viewDidLayoutSuviews