【问题标题】:Coordinates for Center of Screen for iPhone 6S [closed]iPhone 6S屏幕中心坐标[关闭]
【发布时间】:2016-02-17 19:02:03
【问题描述】:

我想在UIViewController 的中心放置一个对象。我曾尝试使用frame.width/2frame.height/2,例如UIView

let newView = UIView(frame: CGRect(x: frame.width/2, y: frame.height/2, width: 100, height: 100))

该方法没有像我预期的那样将对象定位在视图的中心。如何使用 iPhone 6s 的 x-y 坐标将对象定位到视图的中心?

【问题讨论】:

    标签: ios swift uiview coordinates


    【解决方案1】:

    要获得窗口的实际中心坐标,我会使用:

    let centerX = UIScreen.mainScreen().bounds.maxX / 2
    let centerY = UIScreen.mainScreen().bounds.maxY / 2
    

    但除此之外,无论您在哪个 ViewController 中工作,视图的中心都是 CGPoint,其位置可以通过以下方式找到:

    view.center
    

    如果您只想要xy 坐标,您可以通过在view.center 之后添加.x.y 来获得它们。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      相关资源
      最近更新 更多