我正在从数据库中获取数据,并试图将数据显示为按钮,但可以正常工作,但是当我在Xcode模拟器上运行应用程序时,按钮正在显示,并且可以看到它们不在屏幕上,但是我无法滚动。
我尝试在“滚动视图”中添加一个具有固定高度的“视图”,然后将按钮添加到该视图中,但还是一无所获。
// Adding the scroll view and the view
@IBOutlet weak var scroll: UIScrollView!
@IBOutlet weak var content: UIView!
// Button Settings
var buttonX = 100
var buttonY = 0
let buttonWidth = 200
let buttonHeight = 50
let image = UIImage(named: "button") as UIImage?
override func viewDidLoad() {
super.viewDidLoad()
for element in contentsArray {
// Adding a button for each client name
let button = UIButton(type: .system)
button.setTitle(element, for: .normal)
button.tintColor = .white
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 22)
button.setBackgroundImage(image, for: .normal)
button.centerXAnchor.constraint(equalTo: button.centerXAnchor).isActive = true
button.addTarget(self, action: #selector(buttonClicked), for: .touchUpInside)
button.frame = CGRect(x: buttonX, y: buttonY, width: buttonWidth, height: buttonHeight)
content.addSubview(button)
buttonY = buttonY + 70
}
}
运行该应用程序时,控制台没有出现任何错误。
答案 0 :(得分:0)
完成添加按钮后,将scrollView.contentSize.height设置为您计算出的buttonY
scrollView.contentSize.width = self.view.frame.size.width // non scrollable content width
for element in contentsArray {
...
buttonY = buttonY + 70
}
scrollView.contentSize.height = buttonY // scrollable content height