6

I am working on an iPhone app, and created a UIPageViewController (lets call it the container), which contains a number of UIViewController pages (lets call them the subpages). The subpages transition style is scroll.

Now, what I like to do is create a button on the top right corner of the container and NOT in the subpages. The idea is, the button will stay on screen when the subpages scroll from one page to the other. If I create the button in one of the subpages, then each subpage will have its own button, and the button will scroll with the subpages. I want to keep the button without moving in the container, while the subpages scroll.

I tried to add the button using the storyboard to the container, but it is now allowed in. I cannot drop it there, and I suspect the reason is because container is of type UIPageViewController.

How can I do that using the storyboard?

Thanks.

Greeso
  • 7,544
  • 9
  • 51
  • 77

4 Answers4

15
  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.
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • What do you mean exactly by "a container view" in step 2? Can you explain more? UIContainerView? – Greeso Nov 23 '14 at 04:18
  • If you look in the object palette between "view" and "navigation bar" you should see an object called "container view" - You can add this to your scene, set constraints to position it and then it can contain another View Controller – Paulw11 Nov 23 '14 at 04:21
  • @Greeso Container Views are used in storyboards to add child view controllers. Paul sorry for off topic, but could you shed some details on the computer in the pic :) – GoodSp33d Nov 23 '14 at 04:36
  • Great answer +1 – sleep May 01 '17 at 10:19
0

In code, add the button to the uipageviewcontroller

chrs
  • 5,906
  • 10
  • 43
  • 74
  • Can I do that using the storyboard? I could not do that using the storyboard. I updated my question to be more clear. – Greeso Nov 23 '14 at 04:05
  • I couldn't get it to work in storyboard. But it worked fine using code. – chrs Nov 23 '14 at 13:58
0

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:

enter image description here

TMin
  • 2,280
  • 1
  • 25
  • 34
-1

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.

Florian
  • 855
  • 7
  • 12
  • This did not work for me, I cannot drop the button into the storyboard when it is of type UIPageViewController. – Greeso Nov 23 '14 at 04:06