在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上却不能按预期工作)