使用排除时,UITextView 不会在小屏幕上显示所有文本

问题描述 投票:0回答:1

我有一个 UITextView:

var textView: UITextView = {
    let textView = UITextView()
    textView.textColor = .orange
    textView.isScrollEnabled = false
    textView.isEditable = false
    return textView
}()

我将它限制到另一个视图,而该视图又是 UIStackView 的一部分

    mainStack.axis = .vertical
    mainStack.addArrangedSubview(titleView)
    mainStack.addArrangedSubview(bodyView)
    
    titleView.addSubview(textView)
    textView.translatesAutoresizingMaskIntoConstraints = false
    
    let padding: CGFloat = 5
    
    NSLayoutConstraint.activate([
        textView.leadingAnchor.constraint(equalTo: titleView.leadingAnchor, constant: padding),
        textView.trailingAnchor.constraint(equalTo: titleView.trailingAnchor, constant: -padding),
        textView.topAnchor.constraint(equalTo:  titleView.topAnchor, constant: padding),
        textView.bottomAnchor.constraint(equalTo: titleView.bottomAnchor, constant: -padding)
    ])
    

此外,我有一个按钮部分限制在同一区域

let mainActionButton = UIButton()
titleView.addSubview(mainActionButton)
        
mainActionButton.translatesAutoresizingMaskIntoConstraints = false
        
        NSLayoutConstraint.activate([
            mainActionButton.trailingAnchor.constraint(equalTo: titleView.trailingAnchor),
            mainActionButton.topAnchor.constraint(equalTo:  titleView.topAnchor),
            mainActionButton.heightAnchor.constraint(equalTo: mainActionButton.widthAnchor)
        ])

在运行时,调用 viewDidLoad 后,我使用以下命令来确保文本围绕按钮流动

    textView.textContainer.exclusionPaths = [UIBezierPath(rect: mainActionButton.frame)]
    self.layoutSubviews()

一般来说,这效果很好,正如您从屏幕截图中看到的:

working

但是,当我将 uitextview 的约束中的填充更改为小于 4 的值时,仅在小屏幕上,它不会显示最后一行,如下所示:

not working

再次强调,如果满足以下条件,则不会发生这种情况:

  • 没有排除路径(按钮)
  • 在大屏幕上
  • 内边距超过3px

有人知道为什么会发生这种情况吗?

swift uikit uitextview
1个回答
0
投票

HangarRash 的评论回答了这个问题 -

我从使用 UIStackView 切换到使用 UIScrollView 并手动约束 titleView 和 bodyView

© www.soinside.com 2019 - 2024. All rights reserved.