我正在尝试在UIScollView中动态添加按钮,以便当按钮溢出超过其大小但未发生时,它可以滚动。
for data in result as! [NSManagedObject] {
let magButton=UIButton(frame: CGRect(x: Int(newScroll.frame.origin.x), y: verti, width: Int(newScroll.frame.width), height: 25))
magButton.addTarget(self, action: #selector(imageTapped), for: UIControlEvents.touchUpInside)
magButton.backgroundColor=UIColor.gray
magButton.tag=count
magButton.contentMode = .left
magButton.setTitle(data.value(forKey: "name") as! String, for: UIControlState.normal)
myScrollVew.addSubview(magButton)
verti+=26
}
最佳答案
添加更新滚动视图内容大小的最后一行。
myScrollVew.addSubview(magButton)
verti+=26
myScrollVew.contentSize = CGSize(width: myScrollVew.contentSize.width, height: magButton.frame.maxY)