UIScreen.main 、 super.view 和其他视图的中心不在屏幕中间
Center for UIScreen.main , super.view and other views are not centered in middle of screen
我的目标是在 iPhone 屏幕的中央放置一个按钮,使按钮与屏幕边缘的距离相等。我尝试以两种方式添加此按钮,方法是为框架指定一个以父视图 super.view 或 UIScreen.main 为中心的原点。我也尝试过使用约束将按钮的中心重新设置为父视图的中心。在这两种情况下,按钮看起来都靠近屏幕中心,但不是直接居中。需要注意的是,我使用的是 ARSCNView,其次我没有使用 .xib 或故事板来配置 UI。您可以查看相机按钮的屏幕截图(红色):
camera button not centered.
您可以在此处查看视图控制器中我当前代码的样子:(我已经注释掉了定义屏幕中心的各种方式)
let sceneView = ARSCNView()
func addCameraButton(){
//let cameraCenter = CGPoint(x:super.view.center.x, y: super.view.center.y)
//let cameraCenter = CGPoint(x:UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
let cameraCenter = CGPoint(x:sceneView.center.x, y:sceneView.center.y)
let cameraSize = CGSize(width: 100, height: 100)
let cameraRect = CGRect(origin: cameraCenter, size: cameraSize)
cameraButton = KYShutterButton(frame:cameraRect, shutterType: .normal, buttonColor: UIColor.gray)
cameraButton?.addTarget(self, action: #selector(cameraSnap), for: .touchUpInside)
self.view.addSubview(cameraButton!)
let verticalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let horizontalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([verticalCenterCamera, horizontalCenterCamera])
cameraButton?.layoutIfNeeded()
使用自动布局时,您需要在以编程方式设置约束时将 translateAutoresizingMaskIntoconstarints
设置为 false
cameraButton?.translateAutoresizingMaskIntoconstarints = false
let verticalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let horizontalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([verticalCenterCamera, horizontalCenterCamera])
self.view.layoutIfNeeded()
//
有框架布局
self.view.addSubview(cameraButton!)
self.cameraButton?.center = self.sceneView.center
我的目标是在 iPhone 屏幕的中央放置一个按钮,使按钮与屏幕边缘的距离相等。我尝试以两种方式添加此按钮,方法是为框架指定一个以父视图 super.view 或 UIScreen.main 为中心的原点。我也尝试过使用约束将按钮的中心重新设置为父视图的中心。在这两种情况下,按钮看起来都靠近屏幕中心,但不是直接居中。需要注意的是,我使用的是 ARSCNView,其次我没有使用 .xib 或故事板来配置 UI。您可以查看相机按钮的屏幕截图(红色): camera button not centered.
您可以在此处查看视图控制器中我当前代码的样子:(我已经注释掉了定义屏幕中心的各种方式)
let sceneView = ARSCNView()
func addCameraButton(){
//let cameraCenter = CGPoint(x:super.view.center.x, y: super.view.center.y)
//let cameraCenter = CGPoint(x:UIScreen.main.bounds.midX, y: UIScreen.main.bounds.midY)
let cameraCenter = CGPoint(x:sceneView.center.x, y:sceneView.center.y)
let cameraSize = CGSize(width: 100, height: 100)
let cameraRect = CGRect(origin: cameraCenter, size: cameraSize)
cameraButton = KYShutterButton(frame:cameraRect, shutterType: .normal, buttonColor: UIColor.gray)
cameraButton?.addTarget(self, action: #selector(cameraSnap), for: .touchUpInside)
self.view.addSubview(cameraButton!)
let verticalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let horizontalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([verticalCenterCamera, horizontalCenterCamera])
cameraButton?.layoutIfNeeded()
使用自动布局时,您需要在以编程方式设置约束时将 translateAutoresizingMaskIntoconstarints
设置为 false
cameraButton?.translateAutoresizingMaskIntoconstarints = false
let verticalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1.0, constant: 0.0)
let horizontalCenterCamera = NSLayoutConstraint(item: cameraButton, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1.0, constant: 0.0)
NSLayoutConstraint.activate([verticalCenterCamera, horizontalCenterCamera])
self.view.layoutIfNeeded()
//
有框架布局
self.view.addSubview(cameraButton!)
self.cameraButton?.center = self.sceneView.center