首页 > 解决方案 > 如何在 UIPageViewController 上添加按钮?

问题描述

我有一个 3 页的页面视图控制器。我希望在所有这些按钮上都可以看到一个“浮动”按钮。我曾尝试为 PageView 使用容器视图,但这似乎不起作用。我怎样才能让按钮在所有 3 个页面上保持可见?

我曾尝试使用此问题中所述的容器视图: UIButton across all pages of UIPageViewController

标签: iosswiftbuttonuibuttonuipageviewcontroller

解决方案


除了我的其他答案,您可以以编程方式添加一个按钮。

宣布:

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

}

当您滚动浏览页面视图时,此按钮将留在屏幕上。让我知道这是否回答了您的问题。


推荐阅读