2016-11-11 73 views
0

我試圖在父視圖中心的視圖,但預期其不居中不能居中的UIView。這是我的代碼,結果的截圖。斯威夫特:在上海華與NSLayoutConstraint

emptyView.translatesAutoresizingMaskIntoConstraints = false 
self.view.addSubview(emptyView) 
let centerXConstraint = NSLayoutConstraint(item: emptyView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0) 
let centerYConstraint = NSLayoutConstraint(item: emptyView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0) 
self.view.addConstraints([centerXConstraint]) 
self.view.addConstraints([centerYConstraint]) 

Preview

+0

'view.addConstraints'? – sasquatch

+0

Sesquatch應用程序崩潰:***終止應用程序,由於未捕獲的異常'NSInternalInconsistencyException',原因:'無法使用未準備好約束的視圖層次結構設置佈局。 –

+0

你在哪裏寫設置約束這個代碼?這個異常意味着視圖不是完全佈局的,而且沒有設置網點,所以你不能對它們應用約束。 – PGDev

回答

2

下面是代碼,

import UIKit 

class ViewController: UIViewController 
{ 
    @IBOutlet weak var emptyView: UIView! 

    override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     self.emptyView.translatesAutoresizingMaskIntoConstraints = false 
     let centerXConstraint = NSLayoutConstraint(item: emptyView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0) 
     let centerYConstraint = NSLayoutConstraint(item: emptyView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0) 
     let widthConstraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100) 
     let heightConstraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 100) 

     self.view.addConstraints([centerXConstraint, centerYConstraint, widthConstraint, heightConstraint]) 
     self.view.layoutIfNeeded() 
    } 
} 

,沒有必要對高度和寬度約束

視圖控制器接口:

enter image description here

輸出:

enter image description here

0

如果您使用的標籤按鈕或具有固有大小的任何其他視圖試試這個代碼

emptyView.translatesAutoresizingMaskIntoConstraints = false 
let widthConstraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: emptyView.frame.size.width) 
let heightConstraint = NSLayoutConstraint(item: emptyView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: emptyView.frame.size.height) 
var constraints = NSLayoutConstraint.constraints(
     withVisualFormat: "V:[superview]-(<=1)-[label]", 
     options: NSLayoutFormatOptions.alignAllCenterX, 
     metrics: nil, 
     views: ["superview":view, "label": emptyView]) 

view.addConstraints(constraints) 

// Center vertically 
constraints = NSLayoutConstraint.constraints(
     withVisualFormat: "H:[superview]-(<=1)-[label]", 
     options: NSLayoutFormatOptions.alignAllCenterY, 
     metrics: nil, 
     views: ["superview":view, "label": emptyView]) 

view.addConstraints(constraints) 
view.addConstraints([ widthConstraint, heightConstraint])