自动布局警告“NSLayoutConstraint 正在配置超过内部限制的常量”

发布于 2025-01-12 02:10:51 字数 1774 浏览 5 评论 0原文

我有一个名为 MyScrollView 的 UIScrollView 子类。 MyScrollView 中有一个名为 contentView 的子视图。宽度约束设置为 MyScrollView 的 contentSize。

   private func setupSubviews() {
      contentView = ContentView()
      contentView.backgroundColor = UIColor.blue
      contentView.translatesAutoresizingMaskIntoConstraints = false
      contentView.isUserInteractionEnabled = true
    
    
      self.addSubview(contentView)
    
      contentView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
      contentView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
      contentView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
      contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
      
      // create contentView's Width and Height constraints
      cvWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: 0.0)
      cvHeightConstraint = contentView.heightAnchor.constraint(equalToConstant: 0.0)
           
    // activate them
      cvWidthConstraint.isActive = true
      cvHeightConstraint.isActive = true


      cvWidthConstraint.constant = myWidthConstant //<--- problem here if myWidthConstant is very high, such as 512000
      cvHeightConstraint.constant = frame.height
      contentView.layoutIfNeeded()
}

问题是,如果稍后我将 cvWidthConstraint.constant 设置为非常高的值,例如 521000,我会收到警告:

   This NSLayoutConstraint is being configured with a constant that exceeds internal limits.  A smaller value will be substituted, but this problem should be fixed. Break on BOOL _NSLayoutConstraintNumberExceedsLimit(void) to debug.  This will be logged only once.  This may break in the future.

我想知道如何将 UIScrollView 内容大小设置为非常高的值?

I have a subclass of UIScrollView called MyScrollView. There is a subview called contentView inside MyScrollView. The width constraint is set to be the contentSize of MyScrollView.

   private func setupSubviews() {
      contentView = ContentView()
      contentView.backgroundColor = UIColor.blue
      contentView.translatesAutoresizingMaskIntoConstraints = false
      contentView.isUserInteractionEnabled = true
    
    
      self.addSubview(contentView)
    
      contentView.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
      contentView.trailingAnchor.constraint(equalTo: self.trailingAnchor).isActive = true
      contentView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
      contentView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
      
      // create contentView's Width and Height constraints
      cvWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: 0.0)
      cvHeightConstraint = contentView.heightAnchor.constraint(equalToConstant: 0.0)
           
    // activate them
      cvWidthConstraint.isActive = true
      cvHeightConstraint.isActive = true


      cvWidthConstraint.constant = myWidthConstant //<--- problem here if myWidthConstant is very high, such as 512000
      cvHeightConstraint.constant = frame.height
      contentView.layoutIfNeeded()
}

The problem is if later I set cvWidthConstraint.constant to a very high value such as 521000, I get a warning:

   This NSLayoutConstraint is being configured with a constant that exceeds internal limits.  A smaller value will be substituted, but this problem should be fixed. Break on BOOL _NSLayoutConstraintNumberExceedsLimit(void) to debug.  This will be logged only once.  This may break in the future.

I wonder how does one set UIScrollView content size to be very high values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文