我正在尝试创建一个小型选择自己的冒险游戏。在每页文字之后,我想有一个供玩家选择的不同区域。 按钮需要在所有文本之后显示,并且在播放器读取(或向下滚动)所有文本后才可见。
这是我尝试创建按钮的方式:
func CreateButtons(buttons: Int, loadedQuest: XML) {
let buttonHeight: CGFloat = 44
let contentInset: CGFloat = 8
//inset the textView
txtQuestText.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: (buttonHeight+contentInset*2), right: 0)
for i in 1...buttons{
let button = UIButton(frame: CGRect(x: contentInset, y: (txtQuestText.contentSize.height - (contentInset * CGFloat(i))) + (buttonHeight * CGFloat(i)) - contentInset, width: txtQuestText.contentSize.width-contentInset*2, height: buttonHeight))
//setup your button here
button.setTitle("Option \(i)", for: .normal)
button.setTitleColor(UIColor.black, for: .normal)
button.backgroundColor = UIColor.darkGray
//button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
//Add the button to the text view
txtQuestText.addSubview(button)
}
}
Here's how it's currently looking(make sure to scroll all the way down)这些按钮是在textview的滚动范围之外创建的。如何解决此问题并在按钮之间添加填充?
答案 0 :(得分:0)
首先,您必须检查滚动视图是否在底部? 如果在末尾,则可以添加按钮 这将帮助您检查它是否在结尾
extension UIScrollView {
var isAtTop: Bool {
return contentOffset.y <= verticalOffsetForTop
}
var isAtBottom: Bool {
return contentOffset.y >= verticalOffsetForBottom
}
var verticalOffsetForTop: CGFloat {
let topInset = contentInset.top
return -topInset
}
var verticalOffsetForBottom: CGFloat {
let scrollViewHeight = bounds.height
let scrollContentSizeHeight = contentSize.height
let bottomInset = contentInset.bottom
let scrollViewBottomOffset = scrollContentSizeHeight + bottomInset - scrollViewHeight
return scrollViewBottomOffset
}
}
添加此扩展名 希望它能工作