Ios 如何以编程方式创建、添加和定位UIScrollView,使其充满屏幕?

Ios 如何以编程方式创建、添加和定位UIScrollView,使其充满屏幕?,ios,iphone,swift,cocoa-touch,uiscrollview,Ios,Iphone,Swift,Cocoa Touch,Uiscrollview,我想在一个Swift项目中创建一个简单的视图层次结构,该项目有一个UIScrollView,它应该充当子视图的可扩展容器,这些子视图是:UILabel、UITextView和一个按钮 这里的问题是,我使用可视化格式语言以编程方式完成了所有工作,但无法在Interface Builder中完成。下面的“我的代码”显示一个滚动视图,但是它无法向下滚动以显示它下面的视图,并且视图本身的大小不正确。它是静态的。此外,子视图不会展开以填满屏幕 我想显示一个全屏大小的scrollview,并且它的子视图根据

我想在一个Swift项目中创建一个简单的视图层次结构,该项目有一个UIScrollView,它应该充当子视图的可扩展容器,这些子视图是:UILabel、UITextView和一个按钮

这里的问题是,我使用可视化格式语言以编程方式完成了所有工作,但无法在Interface Builder中完成。下面的“我的代码”显示一个滚动视图,但是它无法向下滚动以显示它下面的视图,并且视图本身的大小不正确。它是静态的。此外,子视图不会展开以填满屏幕

我想显示一个全屏大小的scrollview,并且它的子视图根据我设置的大小以不同的高度水平填充屏幕。它们目前只有约200磅宽,这是不寻常的

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.
}

这不是一件很难做到的事情。如果你仔细研究,你可以在这里找到你所有疑问的答案

无论如何,我为您提供了您想要实现的事情的详细代码。希望这对你有帮助

只需在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)
    }
}
下面是一个(相当)简单的示例,向滚动视图添加3个视图,并使用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)
}

尝试指定scrollView的内容大小Hey Darsha,如果您的意思是添加类似scrollView.contentSize=CGSize(宽度:view.frame.width,高度:view.frame.height)的内容,那么实际上它不起作用。它只是将scrollview设置为屏幕的大小,但对于显示内容下方的其他视图,我无法滚动超过scrollview的大小。它仍然是静态的。谢谢,当你给出静态高度时,它能工作吗?通过设置上面的内容大小,我相信它会有一个静态的高度和宽度?这确实会使scrollview成为视图的完整大小,但是scrollview不会滚动,并且子视图的宽度错误(不能跨越整个屏幕),最好使用自动布局和约束,而不是框架和内容大小。这里我举了一个例子,使用了几种不同的约束方法,包括VFL:非常感谢您提供了我想要的具体实现,您给了我很棒的评论,并且从您使用的可视化格式语言中学到了一两件事。祝福你的朋友!祝你有美好的一天。