如何在UIPageViewController顶部添加按钮?

问题描述 投票:0回答:2

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

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

ios swift button uibutton uipageviewcontroller
2个回答
0
投票

作为我的其他答案的替代,您可以以编程方式添加按钮。

宣布:

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

}

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


0
投票

您可以创建一个单独的ViewController,添加容器视图,按住Ctrl键并单击并拖动到pageViewController,然后单击“嵌入”。将任何按钮添加到新的单独ViewController中,并将ViewController与新的swift类相关联,并在那里管理您的按钮功能。

如果你的pageViewController当前是你的初始ViewController(或者如果你使用的是导航控制器的根视图),你只需更改它,以便单独的ViewController现在是初始的viewController,包含嵌入的PageViewController,以及你需要的任何按钮。滚动时,它们将保持静止在视图顶部。

如果您使用的是Storyboard,它应该如下所示:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.