我想在Swift项目中创建一个简单的视图层次结构,该项目具有UIScrollView,它应该作为子视图的可扩展容器:UILabel,UITextView和Button。
这里的问题是我使用可视化格式语言以编程方式完成所有操作,并且无法在Interface Builder中执行此操作。我的下面的代码显示了一个滚动视图,但它无法向下滚动以显示其下方的视图,并且视图本身的大小不正确。这是静态的。此外,子视图不会扩展以填满屏幕。
我想显示全屏尺寸的滚动视图,并且其子视图根据我设置的尺寸水平填充屏幕,具有不同的高度。它们目前只有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.
}
答案 0 :(得分:1)
这是一个(相当)简单的例子,它将3个视图添加到Scroll View并使用VFL设置约束。
<div *ngIf="selectedElement.hasOwnProperty('type');then
showMyDiv">...</div>
<ng-template #showMyDiv>
my div
</ng-template>
答案 1 :(得分:0)
这不是一件非常难以实现的事情。如果你仔细研究,你可以在这里找到所有问题的答案。
无论如何,我正在为您提供您想要实现的事物的详细代码。希望这能帮助你。
只需在ViewController类下面添加此代码即可。
let scrollView = UIScrollView()
override func viewDidLoad() {
super.viewDidLoad()
// Variables for setting the scrollView and Views inside scrollView
let phoneHeight: CGFloat = self.view.frame.size.height
let phoneWidth: CGFloat = self.view.frame.size.width
var yPosition: CGFloat = 0
var scrollViewHeight: CGFloat = 0
// Setting scrollView attributes
scrollView.frame.size.height = phoneHeight
scrollView.frame.size.width = phoneWidth
scrollView.frame.origin.x = 0
scrollView.frame.origin.y = 0
// Adding scrollView to the mainView
self.view.addSubview(scrollView)
// Creating subViews for the scrollView
let view1 = UIView()
let view2 = UIView()
let view3 = UIView()
let view4 = UIView()
let view5 = UIView()
// Adding the subVies in an array
var subViewArray: [UIView] = []
subViewArray.append(view1)
subViewArray.append(view2)
subViewArray.append(view3)
subViewArray.append(view4)
subViewArray.append(view5)
// Adding the subViews to the scrollView
for subView in subViewArray {
// Setting the atributes for subViews
// Fixed height of 200px and width adjusts according to the phoneSize
subView.frame.size.height = 200
subView.frame.size.width = phoneWidth
subView.frame.origin.x = 0
subView.frame.origin.y = yPosition
// Adding the background color to the subViews
subView.backgroundColor = UIColor.blue
// Changing the yPosition and scrollViewHeight and adding a 20px margin for each subview in scrollView
yPosition += 200 + 20
scrollViewHeight += 200 + 20
// Adding labels to the subviews
let label1 = UILabel()
label1.text = "Label Added"
label1.textAlignment = .center
label1.textColor = UIColor.white
label1.frame.size.height = 30
label1.frame.size.width = phoneWidth
label1.frame.origin.x = 0
label1.frame.origin.y = 10
subView.addSubview(label1)
self.scrollView.addSubview(subView)
scrollView.contentSize = CGSize(width: phoneWidth, height: scrollViewHeight)
}
}