【发布时间】:2021-03-05 06:32:20
【问题描述】:
我想在 ui 按钮数组中添加另一个 Uibbuton。当在滚动视图上调用 func 时,它不会被添加。正如您在下面的 gif 中看到的,当按下添加按钮时,没有添加任何内容。我想做的是当按下添加按钮时添加按钮标题 Button 10。由于按钮已在视图中初始化并加载,我想不出该怎么做?
import UIKit
class SwipeableUIScrollView: UIScrollView {
override func touchesShouldCancel(in view: UIView) -> Bool {
if view is UIButton || view is UILabel{
return true
}
return touchesShouldCancel(in: view)
}
}
class ViewController: UIViewController {
var scrollView:SwipeableUIScrollView!
var greenView:UIView!
var addMore:UIButton!
var counter = 10
var scrollViewHeightConstraint:NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
scrollView = SwipeableUIScrollView.init(frame: CGRect.zero)
scrollView.translatesAutoresizingMaskIntoConstraints = false
greenView = UIView.init(frame: CGRect.zero)
greenView.translatesAutoresizingMaskIntoConstraints = false
greenView.backgroundColor = UIColor.green
addMore = UIButton.init(frame: CGRect.zero)
addMore.translatesAutoresizingMaskIntoConstraints = false
addMore.backgroundColor = UIColor.orange
addMore.setTitle("add", for: .normal)
self.view.addSubview(scrollView)
self.view.addSubview(greenView)
self.view.addSubview(addMore)
addMore.addTarget(self, action: #selector(increaseC), for: .touchDown)
scrollViewHeightConstraint = NSLayoutConstraint.init(item: scrollView, attribute: .height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: 50.0)
NSLayoutConstraint.activate([
addMore.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 50.0),
addMore.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: -15.0),
addMore.heightAnchor.constraint(equalToConstant: 25.0),
addMore.widthAnchor.constraint(equalToConstant: 100.0),
scrollView.topAnchor.constraint(equalTo: self.addMore.bottomAnchor, constant: 10.0),
scrollView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
scrollViewHeightConstraint,
greenView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
greenView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
greenView.topAnchor.constraint(equalTo: self.scrollView.bottomAnchor),
greenView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor)
])
var leadingAnchor = self.scrollView!.leadingAnchor
for i in 0..<counter{
let t_button = UIButton.init(frame: CGRect.zero)
t_button.translatesAutoresizingMaskIntoConstraints = false
t_button.backgroundColor = UIColor.blue
scrollView.addSubview(t_button)
NSLayoutConstraint.activate([
t_button.leadingAnchor.constraint(equalTo: leadingAnchor, constant:5.0),
t_button.centerYAnchor.constraint(equalTo: scrollView.centerYAnchor),
t_button.heightAnchor.constraint(equalToConstant: 20),
t_button.widthAnchor.constraint(equalToConstant: 75.0)
])
leadingAnchor = t_button.trailingAnchor
t_button.setTitle("Button \(i)", for: .normal)
}
self.scrollView.trailingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
@objc func increaseC(){
counter += 1
}
}
【问题讨论】:
标签: arrays swift button insert scrollview