【问题标题】:How can I add a button on top of a UIPageViewController?如何在 UIPageViewController 上添加按钮?
【发布时间】:2019-02-10 22:42:59
【问题描述】:

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

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

【问题讨论】:

标签: ios swift button uibutton uipageviewcontroller


【解决方案1】:

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

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

如果您使用 Storyboard,它应该看起来像这样:

【讨论】:

    【解决方案2】:

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

    声明:

    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
    
    }
    

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

    【讨论】:

    • 在 IB 中相当于什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    相关资源
    最近更新 更多