我使用以下代码制作了两个带有自动布局的UIView:
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-|",views: viewsDictionary) view_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[top(bottom)]-[bottom]-10-|",options: NSLayoutFormatOptions.AlignAllLeading,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-[button]-30-|",views: viewsDictionary) let control_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-30-[button(50)]",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 } } }
现在,我想以编程方式将顶视图大小设置为底视图大小的1/3,但我不知道如何执行此操作.
解决方法
这将有效:
let heightConstraint = NSLayoutConstraint(item: topView,attribute: NSLayoutAttribute.Height,relatedBy: NSLayoutRelation.Equal,toItem: bottomView,multiplier: 0.33,constant: 0) self.view.addConstraint(heightConstraint) let widthConstraint = NSLayoutConstraint(item: topView,attribute: NSLayoutAttribute.Width,constant: 0) self.view.addConstraint(widthConstraint)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。