问题
I'm trying to add constraints programmatically in my ScrollView but not working. The problem is it does not work the leading and Trailing for my label and buttons.
I want to put on a scroll view first one label and below the label eg 10 dynamic buttons.
The code is:
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:
The problem was you mapped first button's with label's top so it was not showing properly actually you need to assign first button's top equal to label's bottom:
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))
}
来源:https://stackoverflow.com/questions/31523131/adding-constraints-programmatically-in-uiscrollview-with-dynamic-buttons-swift