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)总结
以上是内存溢出为你收集整理的ios – 如何以编程方式快速设置自动布局中的UIView大小?全部内容,希望文章能够帮你解决ios – 如何以编程方式快速设置自动布局中的UIView大小?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)