gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:03:12 26 4
gpt4 key购买 nike

我正在尝试以编程方式在我的 ScrollView 中添加约束,但无法正常工作。问题是它对我的标签和按钮不起作用。

我想在 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++
}

}

最佳答案

问题是您将第一个按钮的顶部映射到标签的顶部,因此它没有正确显示,实际上您需要将第一个按钮的顶部分配给标签的底部:

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

关于swift - 使用动态按钮在 UIScrollView 中以编程方式添加约束 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31523131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com