1

這裏是我到目前爲止的代碼:試圖按鈕添加到UIPageViewController,但按鈕不會顯示出來,即使他們在視圖層次

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資源創建我自己的自定義按鈕,其中在特定按鈕上的內容會延伸到各自的頁面。

回答

0
  1. 在viewDidLoad視圖幀的時間是實際的界面生成器默認幀。所以按鈕可能會超出界限。嘗試通過設置中心框按鈕將可見或添加viewDidLoad中的按鈕並更新viewDidLayoutSubviews方法中的按鈕框架。
  2. UIPageViewController包含一個手勢識別器。所以添加按鈕也沒用。 建議的方法來實現您的需求 而不是將UIButton添加到UIPageViewController,創建一個超級視圖控制器(UIViewController)包含子視圖作爲UIButtons和UIPageViewController。

參考鏈接:https://spin.atomicobject.com/2016/02/11/move-uipageviewcontroller-dots/

iOS: UIPageViewController - Use button to jump to next page

+0

謝謝!將嘗試這一點,讓你知道:) – srinitude