He hecho dos UIView con Auto Layout usando este código :

class ViewController: UIViewController {
var view_constraint_V:NSArray = [NSLayoutConstraint]();
var originalValue:CGFloat = 0.0;
var offsetValue:CGFloat = -100;
override func viewDidLoad() {
super.viewDidLoad()
//Do any additional setup after loading the view, typically from a nib.
        var topView = UIView()
topView.setTranslatesAutoresizingMaskIntoConstraints(false)
topView.backgroundColor = UIColor.blackColor()
var bottomView = UIView()
bottomView.setTranslatesAutoresizingMaskIntoConstraints(false)
bottomView.backgroundColor = UIColor.lightGrayColor()
let button   = UIButton()
button.backgroundColor = UIColor.darkGrayColor()
button.setTitle("Click me!", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
button.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(topView);
self.view.addSubview(bottomView);
bottomView.addSubview(button)
//constraints
        let viewsDictionary = ["top":topView,"bottom":bottomView, "button":button]
//position constraints
        let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[top]-10-|",options: NSLayoutFormatOptions(0),metrics: nil, views: viewsDictionary)
let view_constraint_H2:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-10-[bottom]-10-|",options: NSLayoutFormatOptions(0),metrics: nil, views: viewsDictionary)
view_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[top(bottom)]-[bottom]-10-|", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary)
view.addConstraints(view_constraint_H)
view.addConstraints(view_constraint_H2)
view.addConstraints(view_constraint_V)
originalValue = (view_constraint_V[1] as NSLayoutConstraint).constant;
//println((view_constraint_V[1] as NSLayoutConstraint))
        (view_constraint_V[1] as NSLayoutConstraint).constant = offsetValue
//Position button
        let control_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-30--30-|", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
let control_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-30-", options: NSLayoutFormatOptions(0), metrics: nil, views: viewsDictionary)
bottomView.addConstraints(control_constraint_H)
bottomView.addConstraints(control_constraint_V)
}
/**
Example of dynamically changing the constraints when clicking on a button.
*/
func buttonAction(sender:UIButton!)
{
if (view_constraint_V[1].constant == offsetValue) {
(view_constraint_V[1] as NSLayoutConstraint).constant = originalValue
} else {
(view_constraint_V[1] as NSLayoutConstraint).constant = offsetValue
}
}
}

Ahora, quiero fijar la vista superior de tamaño igual a 1/3 de la parte inferior de la vista de tamaño mediante programación, pero no sé cómo hacerlo.

Una sugerencia para reducir el autodiseño de código repetitivo es el uso de PureLayout.
mi amigo PureLayout son para swift?
Todos Objective-C las bibliotecas pueden ser utilizados con swift.
utilizar el constructor de interfaces en lugar de código, de creación de la UIs a través de código es una de las más incómodas las cosas ahí fuera.

OriginalEl autor black tiger | 2015-05-24

1 Comentario

  1. 3

    Esto va a funcionar:

    let heightConstraint = NSLayoutConstraint(item: topView, 
    attribute: NSLayoutAttribute.Height,
    relatedBy: NSLayoutRelation.Equal,
    toItem: bottomView,
    attribute: NSLayoutAttribute.Height,
    multiplier: 0.33, constant: 0)
    self.view.addConstraint(heightConstraint)
    let widthConstraint = NSLayoutConstraint(item: topView, 
    attribute: NSLayoutAttribute.Width,
    relatedBy: NSLayoutRelation.Equal,
    toItem: bottomView,
    attribute: NSLayoutAttribute.Width,
    multiplier: 0.33, constant: 0)
    self.view.addConstraint(widthConstraint)

    OriginalEl autor PrabhDev

Dejar respuesta

Please enter your comment!
Please enter your name here