2016-11-11 2 views
0

Ich versuche, eine Ansicht in der übergeordneten Ansicht zu zentrieren, aber sie zentriert nicht wie erwartet. Hier ist mein Code ein Screenshot des Ergebnisses.Swift: UIView in Superview kann nicht mit NSLayoutConstraint zentriert werden

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, mit dem die App abstürzt: *** Beenden der App aufgrund der nicht abgefangenen Ausnahme 'NSInternalInconsistencyException', Grund: 'Das Layout kann nicht eingerichtet werden, wenn die Ansichtshierarchie nicht auf Einschränkungen beschränkt ist.' –

+0

Wo schreiben Sie diesen Code zum Festlegen der Einschränkung? Diese Ausnahme bedeutet, dass die Ansicht kein Layout ist und keine Steckdosen vorhanden sind. Daher können Sie keine Einschränkungen auf diese anwenden. – PGDev

Antwort

2

Hier ist der Code,

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() 
    } 
} 

Wenn Sie Etikett oder Taste oder eine andere Ansicht verwenden, die innere Größe, Höhen- und Breitenbeschränkungen sind nicht erforderlich

Viewcontroller-Schnittstelle:

enter image description here

Ausgang:

enter image description here

0

diesen Code Versuchen

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]) 
Verwandte Themen