我有一个项目,它有一个viewController,其中有一个滚动视图,它有一个UILabel和一个UITextView作为子视图。每次打开应用程序时,我都会从Google数据库中检索数据,从而更新UILabel作为标题,UITextView作为文本体。我设定了约束条件,一切都很完美。但是,如果contentView的大小超过UIViewController视图的边界,那么我不知道如何计算UIScrollView的textView高度,这样就可以向下滚动阅读文本的其余部分。我在底部也有一个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),
        ])我试过很多东西,但似乎什么都没有用。
谢谢大家提前给我时间和答复。
发布于 2020-04-08 20:09:26
你们很亲密..。但是您不需要担心设置滚动视图的.contentSize。让自动布局帮你处理吧。
您缺少的关键点是滚动视图的子视图的底部约束。
下面是您的类(例如标题和正文文本),只是稍微修改一下才能工作。我补充的评论应该清楚地表明:
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
        // sample Title and Body
        storyTitle = "Example\nTitle Label"
        storyBody = ""
        storyBody += "UITextView:"
        storyBody += "NL"
        storyBody += "When a user taps a text view, a keyboard appears; when a user taps Return in the keyboard, the keyboard disappears and the text view can handle the input in an application-specific way. You can specify attributes, such as font, color, and alignment, that apply to all text in a text view."
        storyBody += "NL"
        storyBody += "UIScrollView:"
        storyBody += "NL"
        storyBody += "UIScrollView provides a mechanism to display content that is larger than the size of the application’s window and enables users to scroll within that content by making swiping gestures."
        storyBody += "NL"
        storyBody += "UILabel:"
        storyBody += "NL"
        storyBody += "A label can contain an arbitrary amount of text, but UILabel may shrink, wrap, or truncate the text, depending on the size of the bounding rectangle and properties you set. You can control the font, text color, alignment, highlighting, and shadowing of the text in the label."
        storyBody += "NL"
        storyBody += "UIButton:"
        storyBody += "NL"
        storyBody += "You can set the title, image, and other appearance properties of a button. In addition, you can specify a different appearance for each button state."
        titleLabel.text = storyTitle
        textView.text = storyBody.replacingOccurrences(of: "NL", with: "\n\n")
        // you do not need either of these two lines
        //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),
            // don't use .centerXAnchor constraints
            //titleLabel.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            //textView.centerXAnchor.constraint(equalTo: scrollView.centerXAnchor),
            // don't need to set titleLabel bottom constraint
            //titleLabel.bottomAnchor.constraint(equalTo: textView.topAnchor, constant: -16),
            titleLabel.topAnchor.constraint(equalTo: scrollView.topAnchor, constant: 16),
            titleLabel.leadingAnchor.constraint(equalTo: scrollView.readableContentGuide.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: scrollView.readableContentGuide.trailingAnchor),
            textView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 16),
            textView.leadingAnchor.constraint(equalTo: scrollView.readableContentGuide.leadingAnchor),
            textView.trailingAnchor.constraint(equalTo: scrollView.readableContentGuide.trailingAnchor),
            // set the textView's bottomAnchor to let auto-layout determine content size
            textView.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor),
        ])
    }
}https://stackoverflow.com/questions/61097565
复制相似问题