如何以编程方式滚动查看整个视图,包括图像

时间:2019-01-24 06:43:12

标签: ios swift uiscrollview

我有这样的布局:

它由包含UINavigationController的{​​{1}}组成

该视图包含一个UIScrollView和一个UIImage

标签中的文本可能会很长,如果发生这种情况,我想滚动整个视图使其可滚动,包括图像。

我尝试嵌入一个UIScrollView并将其锚定在顶部和底部,但是无法使图像(只有标签)散布。

我没有尝试将父视图设置为UIScrollView。这不起作用

UILabel

我还在使用扩展程序来帮助进行程序化自动布局

class ContentView: UIScrollView {

    private var margins: UILayoutGuide!
    private var activity: UIActivityIndicatorView!

    private var articleImageView: UIImageView!
    private var articleTextView: UILabel!

    override init(frame: CGRect) {
        super.init(frame: frame)

        margins = safeAreaLayoutGuide

        isScrollEnabled = true

        articleImageView = UIImageView(image: #imageLiteral(resourceName: "feed_header_bg"))
        articleImageView.contentMode = .scaleAspectFill

        addSubview(articleImageView)
        articleImageView.anchor(top: margins.topAnchor, leading: margins.leadingAnchor, bottom: nil, trailing: margins.trailingAnchor)


        articleTextView = UILabel(frame: .zero)

        articleTextView.numberOfLines = 0

        articleTextView.text = "foo\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nboo\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nbaz"

        addSubview(articleTextView)
        articleTextView.anchor(top: articleImageView.bottomAnchor, leading: margins.leadingAnchor, bottom: nil, trailing: margins.trailingAnchor)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func addLoadingView() {
        activity = UIActivityIndicatorView(style: .whiteLarge)
        activity.color = .black
        activity.isHidden = true
        addSubview(activity)
        activity.anchor(centerX: centerXAnchor, centerY: centerYAnchor)
        activity.startAnimating()
    }
}

我希望在顶部有一个图像,在下面有文本,如果文本不适合视图,则允许用户滚动。

1 个答案:

答案 0 :(得分:0)

您可以尝试继承UIView的子类并添加UIScroll并设置下面的锚点吗?

我能够复制它,现在看来对我来说是

class ContentView: UIView {
    private var sv: UIScrollView!

    override init(frame: CGRect) {
        super.init(frame: frame)

        sv = UIScrollView(frame: .zero)
        sv.backgroundColor = .purple

        addSubview(sv)
        sv.fillSuperview()

        let ai = UIImageView(image: #imageLiteral(resourceName: "feed_header_bg"))
        ai.contentMode = .scaleToFill

        sv.addSubview(ai)
        ai.anchor(
            top: sv.topAnchor, left: sv.leadingAnchor, bottom: nil, right: sv.trailingAnchor,
            paddingTop: 20, paddingLeft: 20, paddingBottom: 0, paddingRight: 20,
            width: 0, height: 0,
            centerX: sv.centerXAnchor, centerY: nil
        )

        let l = UILabel(frame: .zero)
        l.numberOfLines = 0
        l.text = """
        Bacon ipsum dolor amet turkey jowl sausage, fatback pork chop kevin frankfurter cupim leberkas picanha ground round swine andouille buffalo meatloaf. Kielbasa meatloaf filet mignon tail. Spare ribs strip steak pork belly t-bone tongue pork loin. Hamburger tenderloin prosciutto venison bacon buffalo.

        Shankle andouille sausage shank capicola kevin, pork loin beef ribs pig leberkas short loin prosciutto jerky beef. Swine kevin pork chop, ribeye cupim short loin boudin buffalo beef. Leberkas filet mignon beef ribs turducken, ribeye sausage shoulder t-bone. Capicola leberkas tail, salami buffalo doner brisket prosciutto sirloin pork loin pork chop ribeye spare ribs drumstick.

        Picanha tongue cow jowl landjaeger cupim meatball sausage kielbasa strip steak pork loin chuck jerky venison biltong. Leberkas capicola bacon, pork loin ground round venison strip steak swine picanha tail flank andouille. Ball tip shankle ham hock leberkas swine. Ball tip sirloin boudin chicken, ground round tail jowl ribeye bresaola. Beef ribs shankle rump jowl chuck cow frankfurter capicola. Sausage picanha short ribs porchetta. Picanha drumstick corned beef beef ribs meatball leberkas pancetta.

        Shankle pancetta kevin hamburger fatback jerky shoulder frankfurter alcatra flank meatloaf rump. Burgdoggen rump shank sirloin hamburger chicken turducken cupim tail andouille ham hock ribeye fatback. Swine pork belly bacon pork chop pig jowl. Drumstick shankle pork loin ham hock, bresaola filet mignon kielbasa.

        Drumstick tongue pork loin pork belly turducken, pig porchetta ground round pancetta bacon pastrami strip steak venison. Tail cow doner pig, beef venison filet mignon landjaeger shank. Swine ham hock burgdoggen picanha shankle capicola tail prosciutto. Short loin ball tip pork chop turkey, pork loin chuck pork shankle salami. Buffalo short ribs sausage doner shankle, filet mignon pancetta frankfurter short loin. Pastrami burgdoggen strip steak, ground round swine shank sirloin pork loin hamburger jowl ribeye. Buffalo turducken kielbasa pork capicola ground round tri-tip drumstick bacon.
        """

        sv.addSubview(l)
        l.anchor(
            top: ai.bottomAnchor, left: sv.leadingAnchor, bottom: sv.bottomAnchor, right: sv.trailingAnchor,
            paddingTop: 20, paddingLeft: 0, paddingBottom: 0, paddingRight: 0,
            width: 0, height: 0,
            centerX: sv.centerXAnchor, centerY: nil
        )
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}