使用动态按钮以编程方式在UIScrollView中添加约束 - Swift

时间:2015-07-20 17:52:25

标签: swift uiscrollview autolayout constraints

我正在尝试在ScrollView中以编程方式添加约束但不起作用。问题是它对我的标签和按钮的前导和尾随不起作用。

我想在滚动视图上首先放置一个标签并在标签下方,例如10个动态按钮。

代码是:

var buttons : [UIButton] = []

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    //Create controller and get view
    //var controller = UIViewController()
    var view = self.view
    var tag:String
    var count = 0

    //Create and layout scroll view
    var scrollView = UIScrollView()
    scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
    view.addSubview(scrollView)
    view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
    view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
    view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
    view.addConstraint(NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))

    //Create and add content view
    var contentView = UIView()
    contentView.setTranslatesAutoresizingMaskIntoConstraints(false)
    scrollView.addSubview(contentView)
    scrollView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0.0))
    scrollView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0.0))
    scrollView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
    scrollView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))

    //Contraint contentView and view
    view.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0.0))
    view.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0.0))

    //Add Label
    var preguntaLabel = UILabel()
    preguntaLabel.numberOfLines = 0
    preguntaLabel.text = "tenemos al mejor del mundo, que va y hace cuatro goles con el Real Sociedad, y viene acá y no la toca. Vos decís, pero la puta, ¿sos argentino o sueco? Que se dejen de romper las pelotas los que dicen ‘a Messi hay que mimarlo’. A Messi hay que tratarlo como a todo jugador que se pone la camiseta de la Selección. Como el mejor del mundo, para lo bueno y lo malo. Pero ojo que tampoco Messi mató, violó, no hagamos una novela"
    preguntaLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
    contentView.addSubview(preguntaLabel)

    //contraits leading y trailing
    contentView.addConstraint(NSLayoutConstraint(item: preguntaLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 5.0))
    contentView.addConstraint(NSLayoutConstraint(item: preguntaLabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 5.0))

    //contraits top
    contentView.addConstraint(NSLayoutConstraint(item: preguntaLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 15.0))

    for var i=0;i<10;i++
    {
        //Create all buttons
        var btn = UIButton.buttonWithType(UIButtonType.System) as! UIButton
        btn.backgroundColor = UIColor.greenColor()
        btn.setTitle("hola"+String(i), forState: UIControlState.Normal)
        btn.titleLabel?.numberOfLines = 0
        buttons.append(btn)
    }
        //Add buttons to view with constraints
        var prevButton:String
        var constH:NSArray
        var constV:NSArray
        var hString:String
        var vString:String
        var index = 0
        for button in buttons{
            button.setTranslatesAutoresizingMaskIntoConstraints(false)
            contentView.addSubview(button)
            //button.bounds.size.height = 90
            button.titleLabel?.numberOfLines = 0
            contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 5.0))
            contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 5.0))
            if(index == 0){
                contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: preguntaLabel, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 8.0))
            }
            else{
                contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: buttons[index-1], attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8.0))
            }
            if(index == buttons.count-1){
                contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -8.0))
            }
            index++
        }

}

1 个答案:

答案 0 :(得分:1)

问题是你用标签的顶部映射了第一个按钮,所以实际上你没有正确显示你需要指定第一个按钮的顶部等于标签的底部:

if(index == 0)
{        
    contentView.addConstraint(NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: preguntaLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 8.0))
}