![](/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.