ios - 如何在 UIPageViewController 上添加按钮?
问题描述
我有一个 3 页的页面视图控制器。我希望在所有这些按钮上都可以看到一个“浮动”按钮。我曾尝试为 PageView 使用容器视图,但这似乎不起作用。我怎样才能让按钮在所有 3 个页面上保持可见?
我曾尝试使用此问题中所述的容器视图: UIButton across all pages of UIPageViewController
解决方案
除了我的其他答案,您可以以编程方式添加一个按钮。
宣布:
let button = UIButton()
如果需要,创建一个名为 setupView() 的函数
private func setupView() {
//Here we set up the size and attributes of the button.
currencyButton.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50)
currencyButton.backgroundColor = UIColor.red
currencyButton.setTitle("YourButtonTitle", for: .normal)
currencyButton.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(currencyButton)
}
并使用按钮:
@objc func buttonAction(sender: UIButton!) {
//Do whatever.
print("ButtonTapped")
}
最后,确保在 viewDidLoad() 中调用了 setupView():
override func viewDidLoad() {
super.viewDidLoad()
setupView()
self.delegate = self
configurePageControl()
self.dataSource = self
}
当您滚动浏览页面视图时,此按钮将留在屏幕上。让我知道这是否回答了您的问题。
推荐阅读
- r - 使用 ggplot 将误差线添加到 Barplot
- android - 有时在片段视图中获取 IllegalStateException 说视图不能为空,为什么?
- reactjs - 如何在 React Route 的父级中设置状态变量?
- python - Keras - 通过测试所有可能的超参数来调整顺序模型
- python - 反向地理编码 Python Pandas 数据框
- ios - Cordova 离子打印插件在 iOs 中发送 nil
- javascript - 如何使用 Selenium 和 Java 提取不在其自身标签内的文本节点的文本?
- android - 重复 android:theme androidmanifest
- python - 如何在使用碰撞块计算 pi 时防止快速移动的物体通过静力学
- c# - 在try/catch中捕获异常后如何测试Api是否返回特定消息