我有覆盖整个屏幕的 UIScrollView,在其中我有 3 个并排的 UIView 对象,通过分页进行管理。我想做出正确的约束,以便它也适合 iPhone 6。
拖动时是这样的:
UIScrollView 的约束运行良好,但是如何在 UIScrollView 中安排 View 的约束?
提前致谢!!
最佳答案
您的 ScrollView 的 subview 需要两组约束。
第一组是规定 ScrollView 的滚动行为(即 ScrollView 的 contentSize 是什么)。我们使用 ScrollView 的 contentLayoutGuide为了这。请注意,与大多数约束不同,这并不规定 subview 的大小,而只是规定这些 subview 与 ScrollView 的 contentSize 之间的关系。
第二组是 subview 大小的约束。为此,我们使用 ScrollView 的 frameLayoutGuide .
因此,假设您有三个 subview ,分别是红色、绿色和蓝色,您可以这样做:
// Set horizontal constraints relative to scroll view contentLayoutGuide
NSLayoutConstraint.activate([
redView.leadingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.leadingAnchor),
greenView.leadingAnchor.constraint(equalTo: redView.trailingAnchor),
blueView.leadingAnchor.constraint(equalTo: greenView.trailingAnchor),
blueView.trailingAnchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor),
])
// set constraints that are common to all three subviews
for subview in [blueView, greenView, redView] {
NSLayoutConstraint.activate([
// Set vertical constraints to scroll view contentLayoutGuide
subview.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
subview.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
// Set width and height of subviews relative to scroll view's frame
subview.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
subview.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),
])
}
上面假设你已经关闭了这三个 subview 的 translatesAuthresizingMaskIntoConstraints。但希望以上说明了这个想法。
顺便说一下,您也可以在 IB 中完成所有这些操作(引用“内容布局指南”和“框架布局指南”,就像上面一样)。我只是在此处以编程方式执行此操作,因此您可以确切地看到发生了什么。
在 iOS 11 之前,我们没有 contentLayoutGuide 和 frameLayoutGuide。因此,当您在 ScrollView 及其 subview 之间设置约束时,它的作用类似于 contentLayoutGuide(即仅影响 contentSize 和 subview 的关系)并设置实际大小在 subview 中,您必须到达 ScrollView 的父 View :
// Set horizontal constraints relative to scroll view contentSize
NSLayoutConstraint.activate([
redView.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor),
greenView.leadingAnchor.constraint(equalTo: redView.trailingAnchor),
blueView.leadingAnchor.constraint(equalTo: greenView.trailingAnchor),
blueView.trailingAnchor.constraint(equalTo: scrollView.trailingAnchor),
])
// set constraints that are common to all three subviews
for subview in [blueView, greenView, redView] {
NSLayoutConstraint.activate([
// Set vertical constraints to scroll view contentSize
subview.topAnchor.constraint(equalTo: scrollView.topAnchor),
subview.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
// Set width and height of subviews relative to superview
subview.widthAnchor.constraint(equalTo: view.widthAnchor),
subView.heightAnchor.constraint(equalTo: view.heightAnchor),
])
}
顺便说一下,Apple 在 Technical Note TN 2154 中讨论了这种行为.
但如果针对 iOS 11 及更高版本,请使用更直观的 contentLayoutGuide 和 frameLayoutGuide。
关于ios - 对带有分页的 UIScrollView 的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27877857/
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
使用rspec-rails3.0+,测试设置分为spec_helper和rails_helper我注意到生成的spec_helper不需要'rspec/rails'。这会导致zeus崩溃:spec_helper.rb:5:in`':undefinedmethod`configure'forRSpec:Module(NoMethodError)对thisissue最常见的回应是需要'rspec/rails'。但这是否会破坏仅使用spec_helper拆分rails规范和PORO规范的全部目的?或者这无关紧要,因为Zeus无论如何都会预加载Rails?我应该在我的spec_helper中做
假设我有一个类A,里面有一些方法。假设stringmethodName是这些方法之一,我已经知道我想给它什么参数。它们在散列中{'param1'=>value1,'param2'=>value2}所以我有:params={'param1'=>value1,'param2'=>value2}a=A.new()a.send(methodName,value1,value2)#callmethodnamewithbothparams我希望能够通过传递我的哈希以某种方式调用该方法。这可能吗? 最佳答案 确保methodName是一个符号,而
当我进入Rails控制台时,我已将pry设置为加载代替irb。我找不到该页面或不记得如何将其恢复为默认行为,因为它似乎干扰了我的Rubymine调试器。有什么建议吗? 最佳答案 我刚发现问题,pry-railsgem。忘记了它的目的是让“railsconsole”打开pry。 关于ruby-on-rails-带有Pry的Rails控制台,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/question
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我了解instance_eval和class_eval之间的基本区别。我在玩弄时发现的是一些涉及attr_accessor的奇怪东西。这是一个例子:A=Class.newA.class_eval{attr_accessor:x}a=A.newa.x="x"a.x=>"x"#...expectedA.instance_eval{attr_accessor:y}A.y="y"=>NoMethodError:undefinedmethod`y='forA:Classa.y="y"=>"y"#WHATTT?这是怎么回事:instance_eval没有访问我们的A类(对象)然后它实际上将它添加到
据我们所知,Jekyll默认分页仅支持index.html,我想创建blog.html并在那里包含分页。有什么解决办法吗? 最佳答案 如果您创建一个名为/blog的目录并在其中放置一个index.html文件,那么您可以向_config.yml表示paginate_path:"blog/page:num"。不是使用根文件夹中的默认index.html作为分页器模板,而是使用/blog/index.html。分页器将根据需要生成类似/blog/page2/和/blog/page3/的页面。这将使您到达yourwebsite.com/b
我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'