![](/img/trans.png)
[英]Xcode 6 Auto Layout - Simulator has correct display, Storyboard far off
[英]Xcode storyboard screen type affecting layout within simulator
在Xcode上開發應用程序時,我遇到的問題是,當故事板中的“查看方式:”選項與同一設備類型匹配時,某些標簽/圖像視圖的對齊僅在模擬器上正確。
我正在創建一個應用程序,其中包含靠近屏幕中間的正方形(圖像視圖),在其中,一組較小的圖像視圖排列成一個圓圈。
如果我轉到故事板並將屏幕視為特定設備(即iPhone Xs),當我在模擬器(也是iPhone Xs)上運行相同的設備時,所有設備都完全對齊。 如果我然后回到故事板查看屏幕作為備用設備(即iPhone 8),並重新運行模擬器(仍然是iPhone Xs),則對齊關閉。
廣場(在故事板中使用自動布局完成)看起來很好,而在動態代碼中創建的較小圖像視圖是錯誤的。 它們的位置基於正方形的中心,其計算方式為self.squareView.center.x和self.squareView.center.y。 這些計算似乎是基於故事板中的屏幕,而不是模擬器中的屏幕。
有沒有人有類似的問題或知道如何解決這個問題?
代碼如下:
let radius = 1.4 // these will be arranged in a circle within the square view.
let angleStep = Float(2.0 * Double.pi) / Double(arrayOfImages.count) // arrayOfImages.count will be a number between 5 and 7.
let yPos: Float = cosf(angleStep * radius)
let xPos: Float = sinf(angleStep * radius)
// The below is to determine the midpoint of the view that I am having the issue with:
let setCenter = CGPoint(x: self.squareView.center.x + CGFloat(xPos), y: self.squareView.center.y + CGFloat(yPos)
// Then the code to arrange the image view:
arrayOfImages[i].frame = CGRect(x:CGFloat(xPos), y:CGFloat(yPos), width: 30, height: 30)
arrayOfImages[i].center = setCenter
我不太確定底部兩行代碼是否正確。
我一直遇到類似的問題,模擬器需要匹配故事板設備顯示才能正常工作。
我想我已經通過在視圖控制器中的viewDidLayoutSubviews()
方法中獲取視圖的邊界來解決我的問題。
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let setCenter = CGPoint(x: self.squareView.center.x + CGFloat(xPos), y:
self.squareView.center.y + CGFloat(yPos)
arrayOfImages[i].frame = CGRect(x:CGFloat(xPos), y:CGFloat(yPos), width: 30, height: 30)
arrayOfImages[i].center = setCenter
}
看起來你引用的squareView的高度和寬度值可能在生命周期的這一部分之前是不准確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.