根据UIView和UITextView的整体大小动态调整UIScrollView contentSize

问题描述 投票:0回答:1

我有一个项目,其中有一个viewController,并且在其中有一个Scroll视图,该视图具有UILabel和UITextView作为子视图。每次打开应用程序时,我都会从Google Firestore数据库检索数据,因此将UILabel更新为Title,将UITextView更新为文本主体。我已经设定了约束,并且一切运行正常。但是,如果textView的大小超出UIViewController视图的边界,我不知道如何计算UIScrollView的contentView高度,以便您可以向下滚动以读取其余文本。我在底部也有一个tabBar,这意味着滚动视图应在其上方停止。

这是我的代码。

    import UIKit

class StoryViewController: UIViewController {

    var storyTitle = String()
    var storyBody = ""


    let titleLabel: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.numberOfLines = 0
        label.font = UIFont(name: "Arial-BoldMT", size: 28)
        label.textColor = UIColor.black
        label.translatesAutoresizingMaskIntoConstraints = false

        return label
    }()

    let textView: UITextView = {
        let textView = UITextView()
        textView.textAlignment = .left
        textView.isEditable = false
        textView.isSelectable = false
        textView.font = UIFont(name: "ArialMT", size: 19)
        textView.isScrollEnabled = false
        textView.translatesAutoresizingMaskIntoConstraints = false

        return textView
    }()

    let scrollView: UIScrollView = {
        let scrollingView = UIScrollView()
        scrollingView.translatesAutoresizingMaskIntoConstraints = false
        return scrollingView
    }()


    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.view.backgroundColor = UIColor.white


        titleLabel.text = storyTitle
        textView.text = storyBody.replacingOccurrences(of: "NL", with: "\n\n")
        textView.sizeToFit()

        scrollView.contentSize = CGSize(width: self.view.frame.width, height: textView.contentSize.height)

        view.addSubview(scrollView)

        scrollView.addSubview(titleLabel)
        scrollView.addSubview(textView)

        NSLayoutConstraint.activate([

        scrollView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),


        titleLabel.leadingAnchor.constraint(equalTo: scrollView.readableContentGuide.leadingAnchor),
        titleLabel.trailingAnchor.constraint(equalTo: scrollView.readableContentGuide.trailingAnchor),
        titleLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16),
        titleLabel.bottomAnchor.constraint(equalTo: textView.topAnchor, constant: -16),
        titleLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),


        textView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16),
        textView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
        textView.leadingAnchor.constraint(equalTo: scrollView.readableContentGuide.leadingAnchor),
        textView.trailingAnchor.constraint(equalTo: scrollView.readableContentGuide.trailingAnchor),


        ])

我尝试了很多事情,但似乎无济于事。

感谢大家的时间和事先的答复。

ios swift uiscrollview uitextview
1个回答
0
投票

您知道scrollView的尺寸是在您的子视图上计算的。

所以:

if scrollView.frame.size.height < textView.height + titleLabel.height + margins {
    scrollView.contentSize = CGSize(width: self.view.frame.width, height: textView.contentSize.height + titleLabel.height) + margins
}
© www.soinside.com 2019 - 2024. All rights reserved.