【问题标题】:Autolayout constraint that uses UIScrollview contentSize使用 UIScrollview contentSize 的自动布局约束
【发布时间】:2014-12-29 05:49:49
【问题描述】:

我在UIScrollView 中有一个UIView。我可以轻松地将高度固定到UIScrollView 的框架高度。

如何添加一个固定到 UIScrollView 的 contentSize 的约束?

谢谢!

【问题讨论】:

    标签: xcode ios7 uiscrollview autolayout xcode6


    【解决方案1】:

    UIScrollView 有动态约束,left、top、width 和 height 是在运行时生成的。如果您将 UIView 放在 UIScrollview 中并在 Interface Builder 中固定固定约束,则会产生错误,因为参数是相对于 Superview/Container View 的。

    您可以尝试一些解决方法:

    1- 以编程方式添加 UIView 约束

    http://www.thinkandbuild.it/learn-to-love-auto-layout-programmatically/

    2- 在 UIView 子类中的 initWithFrame 函数中手动调整视图边界

    请给我任何有关您的进度的反馈。

    【讨论】:

    • 有什么进展吗? @sayguh
    【解决方案2】:

    “以编程方式添加约束”的答案是正确的,但我接受它作为完整答案的细节有点少。

    这就是我的做法!

    1. 删除 WebView 上的所有故事板约束
    2. 在情节提要中为您将使用代码添加的约束添加占位符约束。这一步非常重要(很容易错过),否则您将收到有关冲突约束的错误。
    3. 为 webviewDidFinishLoad 委托方法添加代码

    --代码--

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
    
        _scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width, _headerImageView.frame.size.height + webView.scrollView.contentSize.height);
    
    
    
        _webView.translatesAutoresizingMaskIntoConstraints = NO;
    
        NSDictionary *viewsDictionary = @{@"myWebView":_webView};
    
        NSString *constraintsString = [NSString stringWithFormat:@"V:[myWebView(%i)]", (int)_scrollView.contentSize.height];
    
    
        NSArray *constraint_H = [NSLayoutConstraint constraintsWithVisualFormat:constraintsString options:0 metrics:nil views:viewsDictionary];
    
        [_webView addConstraints:constraint_H];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2022-06-17
      • 2012-11-10
      • 2016-12-21
      • 1970-01-01
      • 2013-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多