我对 UIStackView 内的 UIView 的自动布局约束有问题。
我的目标是创建一个 View :
private static func makeSpace(with value: CGFloat) -> UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = true
let heightConstraint = view.heightAnchor.constraint(lessThanOrEqualToConstant: value)
heightConstraint.priority = UILayoutPriorityDefaultHigh
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalToConstant: 295),
heightConstraint
])
return view
}
我想将其作为排列的 subview 添加到 UIStackView 中,作为其他 UIStackView 排列的 subview 之间的空间。
当我使用视觉检查器检查这些 View 时,我的空间 View 的高度为 0。虽然,当我将约束更改为 equalToConstant 时,高度计算正确。
我想使用 lessThanOrEqualToConstant 来缩小这些空间,以防屏幕布局太小而无法以适当的尺寸容纳它们。
有没有人遇到过这个问题?
最佳答案
代码有效,因为 0 实际上小于或等于 295。如果您希望它小于或等于 295,具体取决于堆栈中的其他 View ,您需要另一个约束,告诉它在可能的情况下扩展到 295:
private static func makeSpace(with value: CGFloat) -> UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = true
// this keeps it under the value, we want this to be enforced, so priority stays `required`
let heightConstraint1 = view.heightAnchor.constraint(lessThanOrEqualToConstant: value)
// this constraint will drive the size (the other one is just correcting it)
let heightConstraint2 = view.heightAnchor.constraint(equalToConstant: value)
// modify this priority according to compression resistance of the other views
heightConstraint2.priority = UILayoutPriority(rawValue: 240)
NSLayoutConstraint.activate([
view.widthAnchor.constraint(equalToConstant: 295),
heightConstraint1,
heightConstraint2
])
return view
}
第一个约束使 value 下的空间保持不变(例如 295)。此约束必须具有 required 优先级,因为您不想以任何方式扩展此高度。
第二个约束告诉它保持高度等于 value。但它的优先级较低。所以其他约束可以覆盖它。如果发生这种情况,自动布局系统将尝试尽可能接近实现它 - 所以如果它不能是 295,但它可以是 230,那将会发生。
旁注:
如果您不关心将其扩展到比 value 更大的值(例如 295),那么您根本不需要第一个约束。我把它放在那里是因为我认为这是必须保留的空间的限制。
另一个旁注:
CompressionResistancePriority 和 ContentHuggingPriority 适用于具有固有大小的 View ,不适用于 UIView。
关于iOS AutoLayout 约束 `lessThanOrEqualToConstant` 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47790098/
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw
你能解释一下吗?我想评估来自两个不同来源的值和计算。一个消息来源为我提供了以下信息(以编程方式):'a=2'第二个来源给了我这个表达式来评估:'a+3'这个有效:a=2eval'a+3'这也有效:eval'a=2;a+3'但我真正需要的是这个,但它不起作用:eval'a=2'eval'a+3'我想了解其中的区别,以及如何使最后一个选项起作用。感谢您的帮助。 最佳答案 您可以创建一个Binding,并将相同的绑定(bind)与每个eval相关联调用:1.9.3p194:008>b=binding=>#1.9.3p194:009>eva
我无法运行Spring。这是错误日志。myid-no-MacBook-Pro:myid$spring/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/lib/spring/sid.rb:17:in`fiddle_func':uninitializedconstantSpring::SID::DL(NameError)from/Users/myid/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/spring-0.0.10/li
我在RoR应用程序中有一个提交表单,是使用simple_form构建的。当字段为空白时,应用程序仍会继续下一步,而不会提示错误或警告。默认情况下,这些字段应该是required:true;但即使手动编写也行不通。该应用有3个步骤:NewPost(新View)->Preview(创建View)->Post。我的Controller和View的摘录会更清楚:defnew@post=Post.newenddefcreate@post=Post.new(params.require(:post).permit(:title,:category_id))ifparams[:previewButt
我一直在Heroku上尝试不同的缓存策略,并添加了他们的memcached附加组件,目的是为我的应用程序添加Action缓存。但是,当我在我当前的应用程序上查看Rails.cache.stats时(安装了memcached并使用dalligem),在执行应该缓存的操作后,我得到current和total_items为0。在Controller的顶部,我想缓存我有的Action:caches_action:show此外,我修改了我的环境配置(对于在Heroku上运行的配置)config.cache_store=:dalli_store我是否可以查看其他一些统计数据,看看它是否有效或我做错
我在我的机器上安装了ruby版本1.9.3,并且正在为我的个人网站开发一个octopress项目。我为我的gems使用了rvm,并遵循了octopress.org记录的所有步骤。但是我在我的rake服务器中发现了一些错误。这是我的命令日志。Tin-Aung-Linn:octopresstal$ruby--versionruby1.9.3p448(2013-06-27revision41675)[x86_64-darwin12.4.0]Tin-Aung-Linn:octopresstal$rakegenerate##GeneratingSitewithJekyllidenticals
我是RubyonRails的新手,我正在尝试编写一个morethan表达式:5%>大于号不断抛出异常捕获错误。我不确定如何解决这个问题?编辑:这不是rails,也不是View,它是一个Ruby构造 最佳答案 使用5%>错误来自photo_limit而不是从Integer延伸类(猜测它真的是一个字符串),因此没有混合比较方法/s有关更多信息,请参阅:http://www.skorks.com/2009/09/ruby-equality-and-object-comparison/特别是你必须混入Comparable并定义方法。虽然在这
在回答另一个问题时,我意识到下面的程序并没有完全按照我的想法去做。puts"test"self.puts"test"#=>privatemethod`puts'calledformain:Object(NoMethodError)异常让我感到惊讶,因为我一直认为顶级方法调用将由main对象实例解决,但事实似乎并非如此。谁是第一个电话的实际接收者?如何解决?这是仅适用于顶级范围内的方法调用的特殊规则吗? 最佳答案 这是一个gooddiscussion说的就是这个问题。顶级方法,由Kernel提供,自动包含在Object类中。这意味着内