gpt4 book ai didi

ios - 在 UIScrollView 中调整多行 UILabel 的大小会中断滚动。为什么?

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

我使用以下设置:

  • ViewControllers View 包含一个 UIScrollView,具有顶部、前导、尾随和底部约束以匹配 VC 大小
  • ScrollView 包含两个 subview :
    • 一个 UIView 来定义 ScrollView 的内容大小。它的高度与 ScrollView 相同,但宽度是它的两倍。因此只能水平滚动。
    • 一个包含一些长文本的 UILabel,带有用于设置固定大小的高度和宽度约束以及用于设置固定位置的 ScrollView 的顶部和前导约束。
  • 当 ScrollView 滚动时,Label 的宽度会发生变化。

问题:如果 Label 设置为使用多行并且手动设置了 ScrollViews contenOffset 属性,则 ScrollView 停止滚动。

ViewController View
+---------------------+
|+-------------------+|
||ScrollView ||
||+------------------||--------------------+
|||UIView to define || content size |
||| || |
||| || |
||| [MultiLine] || |
||| [ Label ] || |
||| || |
||+------------------||--------------------+
|+-------------------+|
+---------------------+


- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

// Setting the ContentOffset will stop scrolling
//[self.scrollView setContentOffset:CGPointMake(0, 0)];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Resize Label when scrolling
self.labelWidthConstraint.constant = MAX (50, 50 + self.scrollView.contentOffset.x);
}

如果

,使用此代码调整标签大小工作正常
  • 标签使用一行设置。在这种情况下,设置内容偏移不会造成任何伤害。
  • 内容偏移没有变化(甚至没有设置为(0, 0))。在这种情况下,将标签设置为多行不会造成任何伤害

同时设置内容偏移量和使用多行不会有效。滚动条不能再滚动了。

这是为什么?知道是什么原因造成的以及如何解决吗?

最佳答案

问题似乎是,当更改标签约束时,它会触发 viewDidLayoutSubviews,然后将 UIScrollView 设置为不滚动,因为 set contentOffset 会被一遍又一遍地调用。如果您只想通过使用 bool 作为标志在初始布局上将 UIScrollView 设置为 CGPoint.zero,则可以克服此问题。显然,由于 UILabel 需要重新绘制大小更改,因此它会触发 viewDidLayoutSubviews。这是 Swift 中的示例。

import UIKit

class ViewController: UIViewController {

lazy var scrollView : UIScrollView = {
let sv = UIScrollView(frame: self.view.bounds)
sv.translatesAutoresizingMaskIntoConstraints = false
sv.delegate = self
return sv
}()

lazy var contentView : UIView = {
let v = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width * 4, height: self.view.bounds.height))
v.translatesAutoresizingMaskIntoConstraints = false
return v
}()

lazy var label : UILabel = {
let lbl = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: 50))
lbl.numberOfLines = 0
lbl.text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s"
lbl.minimumScaleFactor = 0.5
lbl.adjustsFontSizeToFitWidth = true
lbl.font = UIFont.systemFont(ofSize: 22)
lbl.translatesAutoresizingMaskIntoConstraints = false
return lbl
}()

var widthConstraint : NSLayoutConstraint?
var heightConstraint : NSLayoutConstraint?
var startingHeight : CGFloat = 0
var startingWidth : CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
//first scrollview
self.view.addSubview(scrollView)
pinToAllSides(target: scrollView)

//now content view
self.scrollView.addSubview(contentView)
contentView.widthAnchor.constraint(equalTo: self.view.widthAnchor, multiplier: 2).isActive = true
contentView.heightAnchor.constraint(equalTo: self.scrollView.heightAnchor, multiplier: 1).isActive = true
contentView.backgroundColor = .green
pinToAllSides(target: contentView)
scrollView.layoutIfNeeded()

//now the label
self.scrollView.addSubview(label)
label.leadingAnchor.constraint(equalTo: self.scrollView.leadingAnchor, constant: 20).isActive = true
label.topAnchor.constraint(equalTo: self.scrollView.topAnchor, constant: 60).isActive = true
label.backgroundColor = .red
widthConstraint = NSLayoutConstraint(item: label, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: self.view.bounds.width/2)

heightConstraint = NSLayoutConstraint(item: label, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 300)
if let wc = widthConstraint,
let hc = heightConstraint{
startingHeight = hc.constant
startingWidth = wc.constant
label.addConstraint(wc)
label.addConstraint(hc)
}

}

func pinToAllSides(target:UIView){
guard let superview = target.superview else{
return
}
target.leadingAnchor.constraint(equalTo: superview.leadingAnchor).isActive = true
target.trailingAnchor.constraint(equalTo: superview.trailingAnchor).isActive = true
target.topAnchor.constraint(equalTo: superview.topAnchor).isActive = true
target.bottomAnchor.constraint(equalTo: superview.bottomAnchor).isActive = true
}

var hasHappenedOnce : Bool = false
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
if hasHappenedOnce == false{
hasHappenedOnce = true
self.scrollView.contentOffset = .zero
}
}
}

extension ViewController : UIScrollViewDelegate{

func scrollViewDidScroll(_ scrollView: UIScrollView) {
//hopefully it is laggy due to simulator but for the label i would ditch constraints myself
self.widthConstraint?.constant = max(startingWidth, self.scrollView.contentOffset.x * 1.1 + startingWidth)

let height = startingHeight - self.scrollView.contentOffset.x
self.heightConstraint?.constant = height
label.updateConstraints()
}
}

关于ios - 在 UIScrollView 中调整多行 UILabel 的大小会中断滚动。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55041861/

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