Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIScrollView大小的问题_Ios_Iphone_Swift_Swift3_Uiscrollview - Fatal编程技术网

Ios UIScrollView大小的问题

Ios UIScrollView大小的问题,ios,iphone,swift,swift3,uiscrollview,Ios,Iphone,Swift,Swift3,Uiscrollview,我现在尝试动态调整UIScrollView的大小,就像Android中使用wrap_content作为属性一样 数据来自一个SQL查询,我从override func viewDidLoad() 我尝试了自由尺寸变体和带有扩展的版本 extension UIScrollView { func resizeScrollViewContentSize() { var contentRect = CGRect.zero for view in self.sub

我现在尝试动态调整UIScrollView的大小,就像Android中使用wrap_content作为属性一样

数据来自一个SQL查询,我从
override func viewDidLoad()

我尝试了自由尺寸变体和带有扩展的版本

extension UIScrollView {

    func resizeScrollViewContentSize() {
        var contentRect = CGRect.zero
        for view in self.subviews {
            contentRect = contentRect.union(view.frame)
        }
        self.contentSize = contentRect.size
    }
}
但没有任何帮助。有人有主意吗

编辑:

@Stephen J我不明白你的答案,对不起:(

我也有一个子视图,但是我怎么能说Scrollview与他的大小相等呢?我试着在Scrollview上按住CTRL键并拖动鼠标,在另一个方向也一样

所以它看我的故事板


这个主意还可以,但是

您的扩展正在滚动视图的子视图中循环,但是您的滚动视图只包含一个子视图-您的
ContentView
…并且该视图包含一组子视图(实际上,滚动视图的子视图属性也包含滚动指示条,但这并不是真正的因素)

您要做的是循环浏览
ContentView
的子视图,然后更改其大小,然后根据
ContentView
设置滚动视图的
.contentSize

所以

就我个人而言,我发现使用自动布局和约束使使用滚动视图变得非常非常简单-比这种方法更简单,因为您不必不断重新计算。但是,如果这对您有效,那就太好了:)

编辑:这里是一个完整的工作示例。它:

  • 创建大小为完整视图的滚动视图
  • 添加不带大小的ContentView
  • 垂直添加30个标签,间距为16分
  • 然后根据ContentView的子视图计算并设置ContentView的大小和滚动视图的.contentSize


  • 这是以前在没有SQL的情况下提出的问题。去年我自己在制作日历时处理过这个问题。。。基本上,您需要调整内部内容视图的大小(您已经调整了),但是您还必须添加约束,例如宽度和高度。我忘了还有什么。。。我记得使用了子视图,然后添加了宽度和高度约束。不过,有些事情毫无意义,例如,您可能必须从子视图而不是内容本身将约束挂接到uiscrollview。它很凌乱,但你在尝试不同的设置后会得到它。刚刚注意到你在示例图像中键入了“ziemlich schwer”,哈哈。这是,因为我花了大约2个小时,很尴尬,因为我已经做了这么长时间的图形,但约束是没有用的,在所有。写代数框架逻辑比处理它们要快。。。如果他们太累了,请随时关掉annoying@DonMog感谢您的回答,但这不起作用。他正在将每个子视图合并,但高度与以前相同:(@SebastianR.-内容高度不正确吗?您是否设置了滚动视图的框架,使其延伸到主视图框架下方?@DonMog yep从我这里看起来像上面的图片不,我不认为滚动视图延伸到主视图框架下方mainview@SebastianR.是“ziemlich schwer…”在ContentView中标记底部标签?如果是,标签的边框是什么…ContentView的边框是什么…滚动视图的边框是什么…滚动视图的边框是什么。contentSize?@DonMog nope在“ziemlich schwer…”之后还有两个其他标签标签地图上所有标签的框架是ContentView。ContentView的框架是ScrollView,extends函数中显示的contentSize是683.0,真正的框架是自由形式的1000
        // init empty rect
        var contentRect = CGRect.zero
    
        // for each subview in the ** Content ** view - NOT in the Scroll view
        for view in theContentView.subviews {
            // union the view's rect with the "total" rect
            contentRect = contentRect.union(view.frame)
        }
    
        // set the frame size of the Content view to the "total" rect size
        theContentView.frame.size = contentRect.size
    
        // increast the size of the "total" rect by the Content view's x,y offsets
        contentRect.size.width += theContentView.frame.origin.x
        contentRect.size.height += theContentView.frame.origin.y
    
        // NOW we can set the contentSize of the Scroll view
        theScrollView.contentSize = contentRect.size
    
    //
    //  ViewController.swift
    //  SWScrollNoAutoLayout
    //
    //  Created by Don Mag on 6/14/17.
    //  Copyright © 2017 DonMag. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let theScrollView = UIScrollView()
    
            let theContentView = UIView()
    
            theScrollView.frame = self.view.bounds
            theScrollView.backgroundColor = .orange
    
            theContentView.backgroundColor = .yellow
    
            self.view.addSubview(theScrollView)
    
            theScrollView.addSubview(theContentView)
    
            var yOffset: CGFloat = 0.0
    
            for i in 1...30 {
    
                let lbl = UILabel()
    
                lbl.text = "Label: \(i)"
                lbl.backgroundColor = .cyan
                lbl.sizeToFit()
                lbl.frame.origin.y = yOffset
    
                theContentView.addSubview(lbl)
    
                yOffset += lbl.frame.size.height + 16
    
            }
    
    
            // init empty rect
            var contentRect = CGRect.zero
    
            // for each subview in the ** Content ** view - NOT in the Scroll view
            for view in theContentView.subviews {
                // union the view's rect with the "total" rect
                contentRect = contentRect.union(view.frame)
            }
    
            // set the frame size of the Content view to the "total" rect size
            theContentView.frame.size = contentRect.size
    
            // increast the size of the "total" rect by the Content view's x,y offsets
            contentRect.size.width += theContentView.frame.origin.x
            contentRect.size.height += theContentView.frame.origin.y
    
            // NOW we can set the contentSize of the Scroll view
            theScrollView.contentSize = contentRect.size
    
    
        }
    
    }