1
votes

I need to implement UIScrollView in my app using constraintsWithVisualFormat possible that someone would guide me with some code . Thank You!

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.
    }
}

error:

2015-08-04 22:26:05.693 dsds[2017:56799] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property 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)]>" )

Will attempt to recover by breaking constraint

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

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2015-08-04 22:26:05.695

dsds[2017:56799] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property 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)]>" )

Will attempt to recover by breaking constraint

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

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. Message from debugger: Terminated due to signal 15

help me!!!! thanks!!!

2

2 Answers

0
votes

Read the error message. It tells you exactly what the issue is. You are adding constraints to your scrollView but you never set its translatesAutoresizingMaskIntoConstraints to false.

You probably have other problems too, but you cannot proceed until you fix that one.

0
votes

You forgot

scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);

Remember the docs clearly say all programmatically created views must set this.

On another note...why are you putting yourself through this pain for something so clearly suited for StoryBoard's visual AutoLayout?!?

As Matt said you have other problems...such as the fact that UIScrollViews do not behave like other containers. Constraining a UIScrollView's subview to its edges is pointless. A UIScrollView should have a contentSize set.

Fixed up version:

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
    }
}