ios – 使用自动布局的基于百分比的保证金

ios – 使用自动布局的基于百分比的保证金,第1张

概述我正在尝试根据父视图的宽度将子视图放在左边距.这听起来很简单但我无法弄清楚如何使用autolayout来做到这一点. 从逻辑上讲,我只需要将左边距值设置为父宽度的某个百分比值,但此刻,我无法将该逻辑转换为自动布局. 这是我目前的代码: var view = UIView();view.backgroundColor = UIColor.redColor();view.frame = CGRec 我正在尝试根据父视图的宽度将子视图放在左边距.这听起来很简单但我无法弄清楚如何使用autolayout来做到这一点.

从逻辑上讲,我只需要将左边距值设置为父宽度的某个百分比值,但此刻,我无法将该逻辑转换为自动布局.

这是我目前的代码:

var vIEw = UIVIEw();vIEw.backgroundcolor = UIcolor.redcolor();vIEw.frame = CGRectMake(0,320,400);var svIEw = UIVIEw();svIEw.setTranslatesautoresizingMaskIntoConstraints(false);svIEw.backgroundcolor = UIcolor.yellowcolor();//svIEw.frame = CGRectMake(0,50,50);vIEw.addSubvIEw(svIEw);var dict = Dictionary<String,UIVIEw>()dict["Box"] = svIEw;var con1 = NSLayoutConstraint(item: svIEw,attribute: NSLayoutAttribute.top,relatedBy: NSLayoutRelation.Equal,toItem: vIEw,multiplIEr: 1.0,constant: 20.0);vIEw.addConstraint(con1);var con2 = NSLayoutConstraint(item: svIEw,attribute: NSLayoutAttribute.left,attribute: NSLayoutAttribute.WIDth,multiplIEr: 0.75,constant: 0.0);vIEw.addConstraint(con2);var con3 = NSLayoutConstraint(item: svIEw,constant: 0.0);vIEw.addConstraint(con3);var con4 = NSLayoutConstraint(item: svIEw,attribute: NSLayoutAttribute.Height,constant: 0.0);vIEw.addConstraint(con4);

这是代码返回错误的位置:

var con2 = NSLayoutConstraint(item: svIEw,constant: 0.0);vIEw.addConstraint(con2);

错误:

* Terminating app due to uncaught exception ‘NSinvalidargumentexception’,reason: ‘* +[NSLayoutConstraint
constraintWithItem:attribute:relatedBy:toItem:attribute:multiplIEr:constant:]:
InvalID pairing of layout attributes’

有没有人知道如何实现这一目标?我只想让左边距为父视图宽度的0.75%.

谢谢.

解决方法 你想要的是svIEw的左边是在视图左边的某个点,你写的是你希望svIEw的左边是视图宽度的某个点,这不是一个正确的布局属性配对作为你的错误说.

这是你需要做的:

NSLayoutConstraint(item: svIEw,multiplIEr: 1,constant: (CGRectGetWIDth(vIEw.bounds) * 0.75));

希望能帮助到你!

编辑

我发现了一篇关于基于Percented的边距的精彩文章:http://simblestudios.com/blog/development/percentage-width-in-autolayout.html
甚至更简单:
http://simblestudios.com/blog/development/easier-percentage-width-in-autolayout.html

总结

以上是内存溢出为你收集整理的ios – 使用自动布局的基于百分比的保证金全部内容,希望文章能够帮你解决ios – 使用自动布局的基于百分比的保证金所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存