Проблема заключается в том, что при изменении ограничений метки вызывается viewDidLayoutSubviews, который затем устанавливает UIScrollView на отсутствие прокрутки, так как set contentOffset вызывается снова и снова. Вы можете преодолеть это, если вы только хотите установить для UIScrollView значение CGPoint.zero в исходном макете, используя bool в качестве флага. Очевидно, поскольку 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()
}
}