如何在 UIPageViewController 顶部添加按钮?

Wil*_*lor 5 button uibutton ios uipageviewcontroller swift

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

我已尝试使用此问题中所述的容器视图: UIButton 跨 UIPageViewController 的所有页面

Pet*_*ert 2

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

宣布:

let button = UIButton()
Run Code Online (Sandbox Code Playgroud)

如果需要,可以创建一个名为 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)
}
Run Code Online (Sandbox Code Playgroud)

并使用按钮:

@objc func buttonAction(sender: UIButton!) {
   //Do whatever.
   print("ButtonTapped")
}
Run Code Online (Sandbox Code Playgroud)

最后,确保在 viewDidLoad() 中调用 setupView() :

 override func viewDidLoad() {
    super.viewDidLoad()

    setupView()

    self.delegate = self
    configurePageControl()
    self.dataSource = self

}
Run Code Online (Sandbox Code Playgroud)

当您滚动页面视图时,此按钮将保留在屏幕上。如果这能回答您的问题,请告诉我。