如何使用Vertical constraintsWithVisualFormat(SWIFT)实现UIScrollView

时间:2015-08-05 01:28:22

标签: ios iphone swift uiview uiscrollview

我需要在我的应用程序中使用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而终止

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

2 个答案:

答案 0 :(得分:0)

阅读错误消息。它告诉你究竟是什么问题。您正在为repositories { jcenter() maven { url 'http://clojars.org/repo' } } 添加约束,但您从未将其scrollView设置为translatesAutoresizingMaskIntoConstraints

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

答案 1 :(得分:0)

你忘记了

scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);

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

另一方面......你为什么要为自己明显适合StoryBoard的视觉AutoLayout的事情付出痛苦?!

Matt说你还有其他问题......例如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
    }
}