以滚动方式以滚动方式显示初始位置swift 4

时间:2018-07-27 13:32:51

标签: ios swift xcode uiscrollview

我有一个滚动视图,其中包含一些文本,但是,当我访问该视图时,它显示了一半,我希望看到从上方开始的文本。

我的主要问题是我没有从初始位置看到滚动视图,而是从底部开始

请有人可以帮助我吗?

import Foundation
import AVFoundation
import UIKit

class ArTestoController: UIView, UIScrollViewDelegate {

    var scrollView = UIScrollView().then {
        $0.alpha = 1
        $0.contentMode = .scaleToFill
        //$0.contentOffset = CGPoint(x:30.0, y:30.0)
        //$0.setContentOffset(CGPoint(x: 0, y: 0), animated: true)
    }

    let text = UITextView().then {
        $0.text =
        """
        1. Introduzione  alla  Chiesa  di  Santa  Maria  delle  Grazie


        2. Crocifissione


        3. Pilato  si  lava  le  mani

        """
        $0.textColor = .white
        $0.textAlignment = .left
        $0.backgroundColor = UIColor.clear
        $0.font = UIFont(name: "Raleway-Medium", size: 15)
        $0.sizeToFit()
        $0.isEditable = false
        $0.isUserInteractionEnabled = true
        $0.isSelectable = false
    }

    func setText() {
    }

    func showOperaImage(){
        self.addSubview(scrollView)
        self.scrollView.addSubview(text)

        scrollView.activate([
            scrollView.centerXAnchor.constraint(equalTo: self.centerXAnchor),
            scrollView.centerYAnchor.constraint(equalTo: self.centerYAnchor),
            scrollView.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 1),
            scrollView.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 1),
            ])

        text.activate([
            text.centerXAnchor.constraint(equalTo: self.scrollView.centerXAnchor),
            text.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 10),
            text.heightAnchor.constraint(equalTo: self.heightAnchor, multiplier: 0.9),
            text.widthAnchor.constraint(equalTo: self.widthAnchor, multiplier: 0.9),
            text.bottomAnchor.constraint(equalTo: self.scrollView.bottomAnchor, constant: -28),
            text.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 5),
            text.trailingAnchor.constraint(equalTo: self.scrollView.trailingAnchor),
            ])
        scrollView.backgroundColor = UIColor.black
    }

    func activateView() {
        self.scrollView = UIScrollView()
        self.scrollView.delegate = self
        showOperaImage()
        setText()
    }
}

这不起作用:

self.testoView.scrollView.contentOffset = (CGPoint(x: 0, y: 0), animated: true)

0 个答案:

没有答案