UIButton across all pages of UIPageViewController

偶尔善良 提交于 2019-12-05 11:19:38
  1. In your storyboard, create a standard Viewcontroller scene.
  2. To this scene add your fixed buttons and a container view.
  3. Adding the container view will automatically add an embedded view controller. Select this and delete it.
  4. Drag a Page view controller into the storyboard.
  5. Select the container view and drag from the "viewDidLoad" item in its "triggered segues" list to the page view controller. Select "Embed" as the segue type.

In code, add the button to the uipageviewcontroller

Here is a solution using storyboard. You have to do some code, but it's minimal

  1. Add a View to your Page View Controller View Hierarchy in your attributes inspector
  2. Create a UIView Subclass that allows touches to pass through the view if the user is not interacting with a subview (otherwise the user will not be able to swipe between pages). Thanks to @john Stephen for his answer to this question.
class TouchThroughView: UIView {
        override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            for subview in subviews {
                if !subview.isHidden && subview.isUserInteractionEnabled && subview.point(inside: convert(point, to: subview), with: event) {
                    return true
                }
            }
            return false
        }
    }
  1. Create an outlet to this view in your PageViewController instance.
  2. set translateAutoresizingMaskINtoConstraints= false
  3. add the outlet as a subview to your PageViewController's root view
  4. Add constraints positioning the outlet in the root view
  5. Set the background of the view you added to the page view controller to clear (In interface builder).
  6. You are done! Add your subviews and constraints to the view you added to your page view controller in storyboard.

Your PageViewControllerWill look like this:

class MyPageViewController: UIPageViewController {
    // step 3
    @IBOutlet var touchThroughView: UIView!  

    override func viewDidLoad() {
        super.viewDidLoad()

        // your regular page View Controller implementation

       // step 4
       stationaryView.translatesAutoresizingMaskIntoConstraints = false 
        // step 5
        self.view.addSubview(touchThroughView) 

        // Step 6
        touchThroughView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        touchThroughView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
        touchThroughView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
        touchThroughView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
    }
}

Your Story board will look like this:

Drag and drop a button to your controller (UIPageViewController) (make sure it is the good controller). And add some constraint to block it at the top corner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!