gpt4 book ai didi

ios - UIScrollView 大小问题

转载 作者:行者123 更新时间:2023-11-30 12:23:09 26 4
gpt4 key购买 nike

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

数据来 self 在override func viewDidLoad()

中启动的SQL查询

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

extension UIScrollView {

func resizeScrollViewContentSize() {
var contentRect = CGRect.zero
for view in self.subviews {
contentRect = contentRect.union(view.frame)
}
self.contentSize = contentRect.size
}
}

但没有任何帮助。有人有想法吗?

UIScrollView did not show everything

编辑:

@Stephen J 我不明白你的回答,抱歉:(

我也有一个 subview ,但是我怎么能说 ScrollView 等于他的大小呢?我尝试使用 CTRL + 在 ScrollView 上拖动鼠标,并在另一个方向上进行相同的操作。

所以它会查看我的 Storyboard

Storyboard

最佳答案

这个想法不错,但是……

您的扩展正在循环浏览 ScrollView 的 subview ,但您的 ScrollView 仅包含一个 subview - 您的ContentView...以及那个 View 包含一堆 subview 。 (实际上, ScrollView 的 subview 属性包含scrollIndicator栏,但这并不真正在这里考虑)。

您想要做的是循环遍历 ContentView 的 subview ,然后更改其大小,然后设置 .contentSize基于 ContentView 的 ScrollView 。

所以...

    // 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

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

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

  1. 创建一个大小适合整个 View 的 ScrollView
  2. 添加一个 ContentView 没有大小
  3. 垂直添加 30 个标签,间距为 16 点
  4. 然后根据ContentView的 subview 计算并设置ContentView的大小和Scroll View 的.contentSize
<小时/>
//
// 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


}

}

关于ios - UIScrollView 大小问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44544665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com