简体   繁体   English

iOS 16.0 Beta 3 中的 UITextView 固有大小问题

[英]UITextView intrinsic size problem in iOS 16.0 Beta 3

I am having trouble with an app that uses the intrinsic size of a UITextView on a physical iPhone 12 Pro Max running iOS 16.0 beta 3 (20A5312j).我在运行 iOS 16.0 beta 3 (20A5312j) 的物理 iPhone 12 Pro Max 上使用UITextView的固有大小的应用程序遇到问题。 It seems to work on my other physical devices running production iOS versions (iOS 13 and later).它似乎适用于我的其他运行生产 iOS 版本(iOS 13 及更高版本)的物理设备。 And it works on the simulators.它适用于模拟器。

The image on the left is what I would expect and what I am actually seeing on simulators and non-beta iOS devices.左边的图像是我所期望的,也是我在模拟器和非测试版 iOS 设备上实际看到的。 The image on the right is what I am seeing on Beta 3, with the first text view's intrinsic width not wide enough to fit the text, and the text view's intrinsic height:右边的图像是我在 Beta 3 上看到的,第一个文本视图的固有宽度不足以容纳文本,而文本视图的固有高度:

在此处输入图像描述

On the right, the intrinsic width of the top text field on the right image is too narrow to fit the text.在右侧,右侧图像顶部文本字段的固有宽度太窄而无法容纳文本。 The intrinsic height of the bottom text field is too short to fit the text.底部文本字段的固有高度太短,无法容纳文本。

And before people ask, a few observations:在人们问之前,有一些观察:

  • Calling setNeedsLayout (or the like) does not fix it.调用setNeedsLayout (或类似的)并不能解决它。
  • Calling setContentCompressionResistancePriority with .required (or what have you) has no impact..required (或者你有什么)调用setContentCompressionResistancePriority没有影响。
  • The presence or absence of the border does not affect it either.边界的存在与否也不影响它。 I just included the border so you can see what is going on.我只是包括了边界,所以你可以看到发生了什么。
  • Adjusting textContainerInset adjusted the insets as expected, but manifested the same problem.调整textContainerInset按预期调整了插图,但出现了同样的问题。
  • I am not looking for UILabel or other alternatives (as I need the data detectors).我不是在寻找UILabel或其他替代品(因为我需要数据检测器)。
  • I am not looking for non-autolayout workarounds (eg, calculating size manually).我不是在寻找非自动布局的解决方法(例如,手动计算大小)。

The question is merely (a) whether I am missing some property that avoids manifesting this behavior, and (b) whether others can reproduce the problem on Beta 3 physical devices.问题仅仅是(a)我是否遗漏了一些可以避免表现出这种行为的属性,以及(b)其他人是否可以在 Beta 3 物理设备上重现该问题。 If others see the same behavior that I am, I am happy to file a bug report.如果其他人看到与我相同的行为,我很乐意提交错误报告。 I just want to make sure I am not overlooking anything obvious.我只是想确保我没有忽略任何明显的东西。


Here is the code that reproduced the above images:这是重现上述图像的代码:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let shortTextView = addTextView()
        shortTextView.text = "foo bar baz"

        let longTextView = addTextView()
        longTextView.text = "This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test. This is a test."

        NSLayoutConstraint.activate([
            shortTextView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100),
            longTextView.topAnchor.constraint(equalTo: shortTextView.bottomAnchor, constant: 40)
        ])
    }

    /// Add text view to main view
    ///
    /// This:
    ///    1. Creates text view (with border so we can see its `frame`);
    ///    2. Adds to the main view; and
    ///    3. Adds horizontal constraints (with `lessThanOrEqual` for width to take advantage of text view’s intrinsic size).
    ///
    /// Note, even if you turn off the border, I see the same problem on physical device running iOS 16.0 Beta 3 (20A5312j).
    /// But the border lets you see what's going on.

    func addTextView() -> UITextView {
        let textView = UITextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.isScrollEnabled = false
        textView.isEditable = false
        textView.layer.borderWidth = 1
        textView.layer.borderColor = UIColor.gray.cgColor

        view.addSubview(textView)

        let widthConstraint = NSLayoutConstraint(
            item: textView,
            attribute: .width,
            relatedBy: .lessThanOrEqual,
            toItem: view,
            attribute: .width,
            multiplier: 0.8,
            constant: 0
        )

        NSLayoutConstraint.activate([
            textView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            widthConstraint
        ])

        // note, vertical constraint not yet set ... will be set by caller

        return textView
    }
}

While I filed a bug report (FB10884711), it would appear that this was resolved in Beta 4.虽然我提交了错误报告 (FB10884711),但似乎这已在 Beta 4 中得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS 13 测试版中 WKWebView 的内在内容大小问题 - WKWebView's Intrinsic Content Size issue in iOS 13 beta 确定iOS UITextView的填充和大小 - Determine padding and size of iOS UITextView iOS - 自定义视图:在 layoutSubviews() 中更新后忽略固有内容大小 - iOS - Custom view: Intrinsic content size ignored after updated in layoutSubviews() 在iOS中的UITextView上以HTML加载字体的问题 - Problem with loading fonts in html on UITextView in iOS iOS - 在UILabel或UITextView中动画文本大小更改? - iOS - Animating text size change in UILabel or UITextView? iOS7中的UITextView内容大小不同 - UITextView content size different in iOS7 iOS 16.0如何开启开发者模式? - How to enable developer mode on iOS 16.0? 无法通过单击UITextVIew中的邮件链接来关闭显示的MFMailComposeViewController(iOS 8 Beta错误) - cant dismiss MFMailComposeViewController shown by clicking mail link in UITextVIew (iOS 8 beta bug) Swift iOS-如何设置UIView的高度锚&lt;=到标签的内在文本大小? &#39;NSLayoutConstraint&#39;不能转换为&#39;Bool&#39; - Swift iOS -How to set UIView's Height Anchor <= To A Label's Intrinsic Text Size? 'NSLayoutConstraint' is not convertible to 'Bool' iOS:如果我将UILabel添加到UIView,则视图是否具有标签的固有内容大小? - iOS: if I add UILabel to a UIView, will the view has intrinsic content size of the label?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM