以编程方式创建的UITextView无法滚动

时间:2018-07-04 12:02:33

标签: ios textview imageview uikit

我使用界面构建器创建了以下textview并进行了滚动。但是,如果以编程方式完成,则相同的设置不会滚动。我创建了一个演示项目来演示这一点。

在我的视图控制器中,这就是我所拥有的。

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let customTextView = CustomTextView(frame: CGRect(x: 0,
                                                      y: 0,
                                                      width: 400,
                                                      height: 400))

    customTextView.center = self.view.center


    self.view.addSubview(customTextView)
}

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

在自定义文本视图类中,这就是我所拥有的。

import Foundation
import UIKit


class CustomTextView: UIView {

    override init(frame: CGRect) {

        super.init(frame: frame)

        setLayout()
    }

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

    private var imageView: UIImageView?
    private var textView: UITextView?

    private func setLayout() {


        imageView = .init()
        textView = .init()

        imageView?.frame = self.frame
        textView?.frame = CGRect(x: (imageView?.frame.size.width)! * 0.5,
                             y: (imageView?.frame.size.height)! * 0.5,
                            width: 200,
                            height: 200)

        textView?.clipsToBounds = true
        textView?.font = UIFont.systemFont(ofSize: 16)
        textView?.isEditable = false
        textView?.preservesSuperviewLayoutMargins = false

        textView?.text = """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque aliquam gravida lobortis. Quisque mollis varius sollicitudin. Maecenas pharetra consectetur nunc et imperdiet. Etiam sagittis, libero ut mollis cursus, diam nibh lobortis mi, a aliquam dolor odio quis leo. Suspendisse semper leo nec dolor vulputate, sit amet fermentum elit aliquam. Curabitur ultricies, mi vel molestie pellentesque, purus lectus tincidunt lacus, quis iaculis nisi tellus non tellus. Vivamus at diam vel odio mollis accumsan. Aenean at eros lacinia, mattis ipsum id, dignissim erat. Quisque eget volutpat nisi. Etiam maximus dictum felis at suscipit. Suspendisse ac arcu iaculis, aliquet justo vestibulum, efficitur mi. Nam finibus ante nisl, at egestas dolor rutrum a.

    Ut vel mauris ac tellus commodo tempor ut ut dolor. Nam vel lectus maximus, vulputate tortor nec, mollis lectus. Aenean semper nisi a cursus ultrices. Aliquam dignissim blandit arcu quis efficitur. Vestibulum ipsum leo, molestie id justo non, ornare vulputate diam. Maecenas eu turpis euismod, placerat lorem ut, auctor lacus. Aenean at odio fringilla, bibendum metus in, dapibus lectus. Etiam dictum tempus velit, tempor ornare purus cursus a. Suspendisse odio erat, viverra non purus in, rutrum aliquet erat.
    """

        imageView?.addSubview(textView!)
        textView?.center = (imageView?.center)!
        self.addSubview(imageView!)
   }
}

1 个答案:

答案 0 :(得分:3)

您已将textView添加到imageView。 UImageView的userInteraction默认情况下是禁用的。 在imageView的init之后添加以下代码,它应该可以正常工作。

imageView?.isUserInteractionEnabled = true

现在,您应该可以滚动了。希望对您有所帮助。