- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在一个 Swift 项目中创建一个简单的 View 层次结构,它有一个 UIScrollView,它应该充当 subview 的可扩展容器,这些 subview 是:UILabel、UITextView 和一个按钮。
这里的问题是,我使用可视化格式语言以编程方式完成这一切,而不能在 Interface Builder 中完成。我下面的代码显示了一个 ScrollView ,但是它无法向下滚动以显示其下方的 View ,并且 View 本身的大小不正确。它是静态的。此外, subview 不会扩展以填满屏幕。
我想显示一个全屏大小的 ScrollView ,并且它的 subview 水平填充屏幕,高度取决于我设置的大小。它们目前只有大约 200pt 宽,这很不寻常。
viewDidLoad() {
view.addSubview(scrollView)
//This function is a convenience function which applies constraints
view.addConstraints(withFormat: "H:|[v0]|", toViews: scrollView)
view.addConstraints(withFormat: "V:|[v0]|", toViews: scrollView)
//Here I add the 3 subviews mentioned above
scrollView.addSubview(nativeText)
scrollView.addSubview(mnemonicDescription)
scrollView.addSubview(addButton)
//Here I apply constraints using format language
scrollView.addConstraints(withFormat: "H:|[v0]|", toViews: foreignText)
// Note that this should make foreignText expand the full width. It doesn't, its very small
//I continue to add subviews with horizontal and vertical constraints however they do not fill the container view as expected.
}
最佳答案
这是一个(相当)简单的示例,向 ScrollView 添加 3 个 View 并使用 VFL 设置约束。
override func viewDidLoad() {
super.viewDidLoad()
// create a scroll view with gray background (so we can see it)
let theScrollView = UIScrollView()
theScrollView.backgroundColor = .gray
// add it to the view
view.addSubview(theScrollView)
// add constraints so the scroll view fills the view
view.addConstraintsWithFormat("H:|[v0]|", views: theScrollView)
view.addConstraintsWithFormat("V:|[v0]|", views: theScrollView)
// create three UIViews - nativeText (red), mnemonicDescription (green), addButton (blue)
let nativeText = UIView()
nativeText.translatesAutoresizingMaskIntoConstraints = false
nativeText.backgroundColor = .red
let mnemonicDescription = UIView()
mnemonicDescription.translatesAutoresizingMaskIntoConstraints = false
mnemonicDescription.backgroundColor = .green
let addButton = UIView()
addButton.translatesAutoresizingMaskIntoConstraints = false
addButton.backgroundColor = .blue
// add those three views to the scroll view
theScrollView.addSubview(nativeText)
theScrollView.addSubview(mnemonicDescription)
theScrollView.addSubview(addButton)
// set horizontal / width constraints for the three views so they fill the scroll view
// "H:|[v0(==v1)]|" means "make the width of v0 equal to the width of v1, and pin to leading and trailing"
theScrollView.addConstraintsWithFormat("H:|[v0(==v1)]|", views: nativeText, theScrollView)
theScrollView.addConstraintsWithFormat("H:|[v0(==v1)]|", views: mnemonicDescription, theScrollView)
theScrollView.addConstraintsWithFormat("H:|[v0(==v1)]|", views: addButton, theScrollView)
// set the vertical / height constraints of the three views
// (==200) means "set the height to 200"
// "|" means "pin to edge"
// "-40-" means 40 points of space
// so the following 3 lines will put:
// nativeText (Red view) pinned to the top of scrollview, height of 200
theScrollView.addConstraintsWithFormat("V:|[v0(==200)]", views: nativeText)
// mnemonicDescription (Green view) pinned 40 space to Red view, height of 300
theScrollView.addConstraintsWithFormat("V:[v0]-40-[v1(==300)]", views: nativeText, mnemonicDescription)
// addButton (Blue view) pinned 40 space to Green view, height of 250, *and* pinned to bottom of scrollview
theScrollView.addConstraintsWithFormat("V:[v0]-40-[v1(==250)]|", views: mnemonicDescription, addButton)
// it could also be expressed in a single statement
// comment out the above three lines of code, and
// un-comment this line to see the same result
//theScrollView.addConstraintsWithFormat("V:|[v0(==200)]-40-[v1(==300)]-40-[v2(==250)]|", views: nativeText, mnemonicDescription, addButton)
// using those example heights and spacing comes to a total of 830,
// so it will scroll vertically a little bit on a iPhone 7+ (736 pts tall)
}
关于ios - 如何以编程方式创建、添加和定位 UIScrollView 以使其填满屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44284243/
我正在运行 Play! 2.3 使用 Akka 的多线程应用程序。每个线程访问 MySQL 数据库并运行一些计算。每次我运行一组这样的任务时,应用程序的内存占用量都会增加,这主要是由于 JDBC4Pr
我是 JavaScript 新手,正在尝试实现选择排序来亲自接触数组(注意:我知道有一个内置的排序函数)。不过,我遇到了一些奇怪的行为。 这是我的代码: function selectionSort(
我正在使用 unsemantic grid创建我的布局。好吧,我有一个容器,其中有一个标签和一个输入文本。标签的宽度为 20% 然后 margin-rightof 1em 然后我想让容器中的其余空间由
看看这张取自 morsmachine.dk/go-scheduler 的著名图片 灰名单是 P 的本地运行队列。如果此队列变空,它们将被全局运行队列中的 go routines 填充。 问题是,谁来填
(帖子创建于 2016 年 10 月 5 日) 我注意到每次运行图像并删除它时,我的系统都不会恢复到原来的可用空间量。 我应用于容器的生命周期是: > docker build ... > docke
我有一个应用程序,它在启动时运行两个或三个完整的 GC。有很多堆空间,不需要运行任何 GC(minor 或 full)。然而,在 gc 日志中,我可以看到 permgen 被填满,然后立即运行完整
我正在尝试制作可视化 VStack 属性的 VStack 示例应用 但是我的 VStack 无法填满屏幕的宽度 我在互联网上尝试了很多解决方案(frame modifier、HStack with S
[已解决] 所以我在页面顶部有一个高度恒定的 div,我希望在它下面有一个 iFrame 来填充页面的其余部分。 Google Images 和 LinkedIn 做的事情非常相似 参见:http:/
我正在尝试使我的标题框与我的图像大小相同,同时又不失去 flex-slider 的响应能力,有什么建议吗? http://jsfiddle.net/bmBnF/10/ 疯狂的K 最佳答案 我通过将其设
我有一个元素是 的兄弟元素动态填充 hello 我想要 固定在屏幕底部直到动态 s 要求将其向下推到 View 下方。 我尝试了以下但无法获得我想要的结果。 该元素位
我是一名优秀的程序员,十分优秀!