【问题标题】:How to implement a UIScrollView using Vertical constraintsWithVisualFormat (SWIFT)如何使用 Vertical constraintsWithVisualFormat (SWIFT) 实现 UIScrollView
【发布时间】:2015-10-27 14:34:05
【问题描述】:

我需要在我的应用程序中使用 constraintsWithVisualFormat 实现 UIScrollView 可能有人会用一些代码指导我。谢谢!

import UIKit

class ViewController: UIViewController, UIScrollViewDelegate{

    var scrollView: UIScrollView!
    var containerView = UIView()


    //FUNCION PARA PERMITIR COLORES HEXÁDECIMALES
    func uicolorFromHex(rgbValue:UInt32)->UIColor{
        let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0;
        let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0;
        let blue = CGFloat(rgbValue & 0xFF)/256.0;

        return UIColor(red:red, green:green, blue:blue, alpha:1.0);
    }
    //***


    override func viewDidLoad() {
        super.viewDidLoad()

        self.scrollView = UIScrollView()
        self.scrollView.delegate = self

        containerView = UIView()

        containerView.backgroundColor = uicolorFromHex(0x0099FF);
        containerView.setTranslatesAutoresizingMaskIntoConstraints(false);

        scrollView.addSubview(containerView)
        view.addSubview(scrollView)


        //*** DICCIONARIO
        let viewsDictionary = ["scrollView":scrollView
            ,"containerView":containerView]

        let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",
                                                                                        options: NSLayoutFormatOptions(0),
                                                                                        metrics: nil,
                                                                                        views: viewsDictionary)

        let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",
                                                                                        options: NSLayoutFormatOptions(0),
                                                                                        metrics: nil,
                                                                                        views: viewsDictionary)

        view.addConstraints(view_constraint_H as [AnyObject])
        view.addConstraints(view_constraint_V as [AnyObject])


        let view_constraintContainer_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[containerView]-0-|",
            options: NSLayoutFormatOptions(0),
            metrics: nil,
            views: viewsDictionary)

        let view_constraintContainer_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[containerView]-0-|",
            options: NSLayoutFormatOptions(0),
            metrics: nil,
            views: viewsDictionary)

        scrollView.addConstraints(view_constraintContainer_H as [AnyObject])
        scrollView.addConstraints(view_constraintContainer_V as [AnyObject])

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

错误:

2015-08-04 22:26:05.693 dsds[2017:56799] 无法同时 满足约束。可能至少有一个约束条件 以下列表是您不想要的。试试这个:(1)看每个 约束并尝试找出您不期望的; (2) 找到 添加不需要的约束或约束并修复它的代码。 (注意:如果您看到 NSAutoresizingMaskLayoutConstraints 不明白,参考UIView属性的文档 translatesAutoresizingMaskIntoConstraints) (

   "<NSLayoutConstraint:0x7a7cfc00 H:|-(0)-[UIScrollView:0x7c188200]   (Names: '|':UIView:0x7a8499d0 )>",
   "<NSLayoutConstraint:0x7a7cfe50 H:[UIScrollView:0x7c188200]-(0)-|   (Names: '|':UIView:0x7a8499d0 )>",
   "<NSAutoresizingMaskLayoutConstraint:0x7a7df310 h=--& v=--& H:[UIScrollView:0x7c188200(0)]>",
   "<NSLayoutConstraint:0x7a7d04e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7a8499d0(320)]>" )

将尝试通过打破约束来恢复

    <NSLayoutConstraint:0x7a7cfe50 H:[UIScrollView:0x7c188200]-(0)-|  
    (Names: '|':UIView:0x7a8499d0 )>

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点 在调试器中捕获它。中的方法 UIView 上的 UIConstraintBasedLayoutDebugging 类别列于 也可能有帮助。 2015-08-04 22:26:05.695

dsds[2017:56799] 无法同时满足约束。 以下列表中的至少一个约束可能是一个 你不想要。试试这个:(1)查看每个约束并尝试 找出你没想到的; (2) 找到添加的代码 不需要的约束或约束并修复它。 (注:如果你看到 NSAutoresizingMaskLayoutConstraints 你不明白的,参考 到 UIView 属性的文档 translatesAutoresizingMaskIntoConstraints) (

    "<NSLayoutConstraint:0x7a7cfa00 V:|-(0)-[UIScrollView:0x7c188200]   (Names: '|':UIView:0x7a8499d0 )>",
    "<NSLayoutConstraint:0x7a7d0160 V:[UIScrollView:0x7c188200]-(0)-|   (Names: '|':UIView:0x7a8499d0 )>",
    "<NSAutoresizingMaskLayoutConstraint:0x7a7df370 h=--& v=--& V:[UIScrollView:0x7c188200(0)]>",
    "<NSLayoutConstraint:0x7a7df920 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7a8499d0(568)]>" )

将尝试通过打破约束来恢复

    <NSLayoutConstraint:0x7a7d0160 V:[UIScrollView:0x7c188200]-(0)-|  
    (Names: '|':UIView:0x7a8499d0 )>

在 UIViewAlertForUnsatisfiableConstraints 创建一个符号断点 在调试器中捕获它。中的方法 UIView 上的 UIConstraintBasedLayoutDebugging 类别列于 也可能有帮助。来自调试器的消息: 因信号 15 而终止

帮帮我!!!!谢谢!!!

【问题讨论】:

    标签: ios iphone swift uiview uiscrollview


    【解决方案1】:

    阅读错误信息。它准确地告诉你问题是什么。您正在向 scrollView 添加约束,但您从未将其 translatesAutoresizingMaskIntoConstraints 设置为 false

    您可能还有其他问题,但在解决该问题之前您无法继续。

    【讨论】:

    【解决方案2】:

    你忘了

    scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);
    

    请记住,文档清楚地说所有以编程方式创建的视图都必须设置此项。

    另一方面...你为什么要让自己忍受这种痛苦,以获得如此明显适合 StoryBoard 视觉自动布局的东西?!?

    正如马特所说,您还有其他问题......例如 UIScrollViews 的行为与其他容器不同。将 UIScrollView 的子视图约束到其边缘是没有意义的。 UIScrollView 应该有一个contentSize 集。

    修正版:

    import UIKit
    
    extension UIColor {
       static func fromUInt(rgbValue: UInt32) -> UIColor{
            let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 256.0
            let green = CGFloat((rgbValue & 0xFF00) >> 8) / 256.0
            let blue = CGFloat(rgbValue & 0xFF) / 256.0
            return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
        }
    }
    
    class ViewController: UIViewController, UIScrollViewDelegate {
        var scrollView = UIScrollView()
        var containerView = UIView()
    
        override func viewDidLoad() {
            super.viewDidLoad()   
            scrollView.delegate = self
            containerView.backgroundColor = UIColor.fromUInt(0x0099FF)
            containerView.setTranslatesAutoresizingMaskIntoConstraints(false)
            scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)  
            scrollView.addSubview(containerView)
            view.addSubview(scrollView)
    
            let viewsDictionary = ["scrollView": scrollView]
            let view_constraint_H = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",
                options: NSLayoutFormatOptions(0),
                metrics: nil,
                views: viewsDictionary)
    
            let view_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",
                options: NSLayoutFormatOptions(0),
                metrics: nil,
                views: viewsDictionary)
    
            view.addConstraints(view_constraint_H)
            view.addConstraints(view_constraint_V)
    
            containerView.frame = CGRectMake(0, 0, 2000, 2000)
            scrollView.contentSize = containerView.frame.size
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 1970-01-01
      相关资源
      最近更新 更多