gpt4 book ai didi

ios - UITextView 放置在自动布局中表现得很奇怪

转载 作者:行者123 更新时间:2023-11-28 15:21:57 24 4
gpt4 key购买 nike

我有一个 UITextView,它的行为真的很奇怪,为什么我试图用自动布局来限制它。背景将被正确放置,但其中的文本不合适。我尝试创建一个 UIView,然后将其添加到 UIView,但问题仍然存在。

这是 UITextView 和 UIView 声明

let noteTextView: UITextView =
{
let tv = UITextView()
tv.textColor = UIColor.white
tv.backgroundColor = UIColor.clear
tv.font = UIFont.systemFont(ofSize: 16)
tv.isEditable = false
tv.isUserInteractionEnabled = true

return tv
}()

let backgroundView: UIView =
{
let view = UIView()
view.backgroundColor = UIColor.rgb(red: 156, green: 44, blue: 252)

return view
}()

这是约束条件(使用称为 anchor 的扩展)

    view.addSubview(backgroundView)
backgroundView.addSubview(noteTextView)

backgroundView.anchor(top: view.topAnchor, left: view.leftAnchor, bottom: nil, right: view.rightAnchor, paddingTop: 65, paddingLeft: 0, paddingRight: 0, paddingBottom: 0, width: 0, height: 80)

noteTextView.anchor(top: backgroundView.topAnchor, left: backgroundView.leftAnchor, bottom: backgroundView.bottomAnchor, right: backgroundView.rightAnchor, paddingTop: 2, paddingLeft: 5, paddingRight: 5, paddingBottom: 2, width: 0, height: 0)

这是一个演示问题的视频:https://streamable.com/y8v70

注意:当我刚刚将 UITextView 添加到 View (没有 UIVIew)时,发生了完全相同的事情。

最佳答案

viewDidLoad 中禁用 automaticallyAdjustsScrollViewInsets

self.automaticallyAdjustsScrollViewInsets = false

也试试:

textView.textContainerInset = UIEdgeInsetsZero;  
textView.textContainer.lineFragmentPadding = 0;

希望这有帮助。

关于ios - UITextView 放置在自动布局中表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45832747/

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