在scrollView中以编程方式添加按钮的AutoLayout约束

时间:2015-08-27 13:57:51

标签: ios swift uiscrollview autolayout

我正在努力为使用未知数量的按钮填充的水平滚动视图进行自动布局工作(在这种情况下,数字存储在sceneCount中)。 我试图以其他方式解决问题,但这似乎是我能得到的最接近的结果(没有矛盾的约束)。不幸的是,我只在运行时获得了一个没有错误的白屏。 This是我希望我的scrollView看起来像。 希望你们能发现问题!

import UIKit

class ViewController: UIViewController {

let sceneCount = 4
var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
var buttons: [UIButton] = [UIButton]()


func makeLayout(){

    //Make the content view
    let view1 = UIScrollView()
    view1.setTranslatesAutoresizingMaskIntoConstraints(false)
    view1.backgroundColor = UIColor.redColor()

    //Make the scroll view
    let view2 = UIScrollView()
    view2.setTranslatesAutoresizingMaskIntoConstraints(false)
    view2.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.1, alpha: 1.0)

    view.addSubview(view1)
    view2.addSubview(view1)

    //Create the buttons
    for i in 0...(sceneCount-1) {
        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        button.addTarget(self, action: Selector("scene\(i)Pressed"), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "Scene\(i+1)"), forState: UIControlState.Normal)
        button.sizeThatFits(CGSizeMake(40, 40))

        buttons.append(button)
        view1.addSubview(button)

    }

    //set horizontal spacing between the buttons
    for i in 1...(sceneCount-2) {
        var button1 = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        var button2 = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        button1 = buttons[i - 1]
        button2 = buttons[i]
        var dictionary1 = [String: UIButton]()
        dictionary1.updateValue(button1, forKey: "scene1")
        dictionary1.updateValue(button2, forKey: "scene2")

        view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[scene1]-[scene2]", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary1) as [AnyObject])
    }

    //set vertical spacing for all buttons
    for i in 0...(sceneCount-1) {
        var button1 = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        button1 = buttons[i]
        var dictionary2 = [String: UIButton]()
        dictionary2.updateValue(button1, forKey: "button1")

        view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[button1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary2) as [AnyObject])
    }

    //set horizontal distance to container for first button
    if sceneCount > 0 {
        var buttonFirst: UIButton = buttons[0]
        var dictionary3 = [String: UIButton]()
        dictionary3.updateValue(buttonFirst, forKey: "buttonFirst")
        view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[buttonFirst]", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary3) as [AnyObject])
    }

    //set horizontal distance to container for last button
    if sceneCount > 0 {
        var buttonLast: UIButton = buttons[sceneCount-1]
        var dictionary4 = [String: UIButton]()
        dictionary4.updateValue(buttonLast, forKey: "buttonLast")
        view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[buttonLast]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary4) as [AnyObject])
    }

}

override func viewDidLoad() {
    super.viewDidLoad()
    makeLayout()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

1 个答案:

答案 0 :(得分:0)

我设法使您的代码有三个主要更改,如下所示:

1 - 您的内容视图必须是UIView类型

let view1 = UIView()

此视图将包含UIScrollView(view2),并且必须设置其约束。在view1上,您将能够设置滚动视图的大小,这就是为什么我使视图2与超级视图的距离为0。

view1.addSubview(view2)

var dictionary = [String: UIView]()
dictionary.updateValue(view1, forKey: "view1")
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])

dictionary = [String: UIView]()
dictionary.updateValue(view2, forKey: "view2")
view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view2]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])
view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view2]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])

2 - 当你使用相同的引用来创建新按钮时,最终的数组在每个位置都有相同的按钮。     我移动了创建按钮的每个按钮的初始化。

var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton
view2.addSubview(button)

3-之后,我刚刚更新了要插入按钮和滚动视图(view2)之间的约束。

这是最终的代码:

import UIKit

class ViewController: UIViewController {

let sceneCount = 4
var buttons: [UIButton] = [UIButton]()

func makeLayout(){

    //Make the content view
    let view1 = UIView()
    view1.setTranslatesAutoresizingMaskIntoConstraints(false)
    view1.backgroundColor = UIColor.redColor()

    //Make the scroll view
    let view2 = UIScrollView()
    view2.setTranslatesAutoresizingMaskIntoConstraints(false)
    view2.backgroundColor = UIColor(red: 0.75, green: 0.75, blue: 0.1, alpha: 1.0)

    view.addSubview(view1)
    view1.addSubview(view2)

    var dictionary = [String: UIView]()
    dictionary.updateValue(view1, forKey: "view1")

    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])

    dictionary = [String: UIView]()
    dictionary.updateValue(view2, forKey: "view2")

    view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view2]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])
    view1.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view2]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary) as [AnyObject])

    //Create the buttons
    for i in 0...(sceneCount-1) {
        var button = UIButton.buttonWithType(UIButtonType.System) as! UIButton

        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        button.addTarget(self, action: Selector("scene\(i)Pressed"), forControlEvents: UIControlEvents.TouchUpInside)
        button.setBackgroundImage(UIImage(named: "Scene\(i+1)"), forState: UIControlState.Normal)
        button.sizeThatFits(CGSizeMake(40, 40))

        buttons.append(button)
        view2.addSubview(button)
    }

    //set vertical spacing for all buttons
    for i in 0...(sceneCount-1) {
        var button1 = buttons[i]
        var dictionary2 = [String: UIButton]()
        dictionary2.updateValue(button1, forKey: "button1")

        view2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[button1]-0-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary2) as [AnyObject])
    }

    //set horizontal distance to container for first button
    if sceneCount > 0 {
        var buttonFirst: UIButton = buttons[0]
        var dictionary3 = [String: UIButton]()
        dictionary3.updateValue(buttonFirst, forKey: "buttonFirst")
        view2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-[buttonFirst]", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary3) as [AnyObject])
    }

    //set horizontal spacing between the buttons
    for i in 1...(sceneCount-1) {
        var button1 = buttons[i - 1]
        var button2 = buttons[i]

        var dictionary1 = [String: UIButton]()
        dictionary1.updateValue(button1, forKey: "scene1")
        dictionary1.updateValue(button2, forKey: "scene2")

        view2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[scene1]-[scene2]", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary1) as [AnyObject])
    }

    //set horizontal distance to container for last button
    if sceneCount > 0 {
        var buttonLast: UIButton = buttons[sceneCount-1]
        var dictionary4 = [String: UIButton]()
        dictionary4.updateValue(buttonLast, forKey: "buttonLast")
        view2.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[buttonLast]-|", options: NSLayoutFormatOptions(0), metrics: nil, views: dictionary4) as [AnyObject])
    }

}

override func viewDidLoad() {
    super.viewDidLoad()
    makeLayout()
}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}