gpt4 book ai didi

ios - 如何在 UITextView 的文本后面动态创建多个按钮

转载 作者:行者123 更新时间:2023-11-29 05:28:38 30 4
gpt4 key购买 nike

我正在尝试创建一个小型的选择你自己的冒险游戏。在每个页面的文本后面,我希望有一个区域供玩家选择不同的选项。这些按钮需要显示在所有文本之后,并且在玩家阅读(或向下滚动)所有文本之前不可见。

以下是我尝试创建按钮的方法:

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 的滚动范围之外创建。我该如何解决这个问题并在按钮之间添加填充?

最佳答案

首先你必须检查 ScrollView 是否在底部?如果它在末尾/底部,您可以添加按钮这将帮助您检查它是否结束

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
}

}

添加此扩展希望能发挥作用

关于ios - 如何在 UITextView 的文本后面动态创建多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901068/

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