尝试向UIPageViewController添加按钮,但即使按钮位于视图层次结构中,按钮也不会显示

时间:2016-12-16 16:58:44

标签: ios swift uiviewcontroller uipageviewcontroller uipagecontrol

到目前为止,这是我的代码:

class WarmPushPageViewController: UIPageViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.dataSource = self
        // Do any additional setup after loading the view.
        let feedViewController = storyboard!.instantiateViewController(withIdentifier: "FeedViewController")
        setViewControllers([feedViewController], direction: .forward, animated: false, completion: nil)

        let hashtagsButton = UIButton(frame: CGRect(x: 8, y: self.view.frame.height - 30, width: 70, height: 20))
        hashtagsButton.setTitle("Hashtags", for: .normal)
        hashtagsButton.tintColor = UIColor.blue
        self.view.addSubview(hashtagsButton)
        self.view.bringSubview(toFront: hashtagsButton)

        let feedButton = UIButton(frame: CGRect(x: self.view.frame.width/2, y: self.view.frame.height - 30, width: 50, height: 20))
        feedButton.setTitle("Feed", for: .normal)
        feedButton.tintColor = UIColor.blue
        self.view.addSubview(feedButton)
        self.view.bringSubview(toFront: feedButton)

        let userButton = UIButton(frame: CGRect(x: self.view.frame.width-65, y: self.view.frame.height - 30, width: 50, height: 20))
        userButton.setTitle("User", for: .normal)
        userButton.tintColor = UIColor.blue
        self.view.addSubview(userButton)
        self.view.bringSubview(toFront: userButton)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension WarmPushPageViewController: UIPageViewControllerDataSource {
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        if let _ = viewController as? UserViewController {
            let feedViewController = storyboard!.instantiateViewController(withIdentifier: "FeedViewController")
            return feedViewController
        } else if let _ = viewController as? FeedViewController {
            let hashtagsViewController = storyboard!.instantiateViewController(withIdentifier: "HashtagsViewController")
            return hashtagsViewController
        }
        return nil
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        if let _ = viewController as? HashtagsViewController {
            let feedViewController = storyboard!.instantiateViewController(withIdentifier: "FeedViewController")
            return feedViewController
        } else if let _ = viewController as? FeedViewController {
            let userViewController = storyboard!.instantiateViewController(withIdentifier: "UserViewController")
            return userViewController
        }
        return nil
    }
}

出于某种原因,一旦我到达最终页面的边缘,标签就出现在边缘:

Hashtag Label

标签也存在于我的视图层次结构中,但我的假设是问题可能存在于此处:

View Hierarchy

最终目标是使用Sketch资源创建我自己的自定义按钮,其中特定按钮内部的触摸将转移到相应的页面。

1 个答案:

答案 0 :(得分:3)

  1. 在viewDidLoad视图框架是实际界面构建器默认框架时。因此按钮可能会超出范围。尝试通过设置中心框架按钮可见或在viewDidLoad中添加按钮并更新viewDidLayoutSubviews方法中的按钮框架。
  2. UIPageViewController包含手势识别器。所以添加按钮也没用。 满足您需求的建议方法 而不是将UIButton添加到UIPageViewController,创建一个超级视图控制器(UIViewController)包含子视图作为UIButtons和UIPageViewController。
  3. 参考链接:https://spin.atomicobject.com/2016/02/11/move-uipageviewcontroller-dots/

    iOS: UIPageViewController - Use button to jump to next page