gpt4 book ai didi

ios - 以编程方式创建的 UITextView 不滚动

转载 作者:行者123 更新时间:2023-11-28 19:27:24 25 4
gpt4 key购买 nike

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

在我的 View Controller 中,这就是我所拥有的。

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()
}
}

在自定义 TextView 类中,这就是我所拥有的。

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!)
}
}

最佳答案

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

imageView?.isUserInteractionEnabled = true

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

关于ios - 以编程方式创建的 UITextView 不滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51173243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com