Why does the Xcode "Page-Based Application" project template have a PageController? - ios

I'm learning page controllers and have created a blank app using the Page-Based Application template. By default it creates 12 month pages that you can scroll left to right.
On the storyboard there is a Page View Controller Scene with a Page View Controller. However, in the code, there are commands to create this controller programmatically in the root controller. i.e.
var pageViewController: UIPageViewController?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = UIPageViewController(transitionStyle: .pageCurl, navigationOrientation: .horizontal, options: nil)
self.pageViewController!.delegate = self
let startingViewController: DataViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
let viewControllers = [startingViewController]
self.pageViewController!.setViewControllers(viewControllers, direction: .forward, animated: false, completion: {done in })
self.pageViewController!.dataSource = self.modelController
self.addChildViewController(self.pageViewController!)
self.view.addSubview(self.pageViewController!.view)
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
var pageViewRect = self.view.bounds
if UIDevice.current.userInterfaceIdiom == .pad {
pageViewRect = pageViewRect.insetBy(dx: 40.0, dy: 40.0)
}
self.pageViewController!.view.frame = pageViewRect
self.pageViewController!.didMove(toParentViewController: self)
}
In fact, if I delete the Page View Controller from the storyboard, the app works just as before.
I am trying to understand why this would be on the storyboard when it does not appear to be used. Also, are the good reasons to create a Page View Controller programmatically rather than using the storyboard?

Inside storyboard You have a standard UIViewController subclass, not UIPageViewController. Your UIPageViewController is created in code, and then added as a subview to this RootViewController. RootViewController displays UIPageViewController and act as its delegate. I don't think this will work correctly if you delete anything from the storyboard, are you sure about this? There is not instance of UIPageViewController in storyboard.

Related

Share Extension Modal Presentation Style iOS 13 is not working

I implemented the share extension and I want animate my View Controller with a crossDissolve, so i set the modalPresentationStyle = .overFullScreen and modalTransitionStyle = crossDissolve but it seems not working. The VC still appear from the bottom to the top and with the new iOS 13 modal style (not completly full screen).
Anyone know how to solve it? It tried both with and without storyboard.
NB: I'm not talking about a normal VC presentation, but the presentation of the share extension, it means that it's another app that present my VC.
One way to do it would be to have the system presented viewcontroller as a container.
And then present your content viewcontroller inside modally.
// this is the entry point
// either the initial viewcontroller inside the extensions storyboard
// or
// the one you specify in the .plist file
class ContainerVC: UIViewController {
// afaik presenting in viewDidLoad/viewWillAppear is not a good idea, but this produces the exact result you are looking for.
// meaning the content slides up when extension is triggered.
override func viewWillAppear() {
super.viewWillAppear()
view.backgroundColor = .clear
let vc = YourRootVC()
vc.view.backgroundColor = .clear
vc.modalPresentationStyle = .overFullScreen
vc.loadViewIfNeeded()
present(vc, animated: false, completion: nil)
}
}
and then use the content viewcontroller to show your root viewcontroller and its view hierarchy.
class YourRootVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = UIViewController() // your actual content
vc.view.backgroundColor = .blue
vc.view.frame = CGRect(origin: vc.view.center, size: CGSize(width: 200, height: 200))
view.addSubview(vc.view)
addChild(vc)
}
}
Basically a container and a wrapper in order to get the control over the views being displayed.
Source: I had the same problem. This solution works for me.

How do I keep UI elements above a UIViewController and its presented ViewController?

I want to display some UI elements, like a search bar, on top of my app's first VC, and also on top of a second VC that it presents.
My solution for this was to create a ContainerViewController, which calls addChildViewController(firstViewController), and view.addSubview(firstViewController.view). And then view.addSubview(searchBarView), and similar for each of the UI elements.
At some point later, FirstViewController may call present(secondViewController), and ideally that slides up onto screen with my search bar and other elements still appearing on top of both view controllers.
Instead, secondViewController is presented on top of ContainerViewController, thus hiding the search bar.
I also want, when a user taps on the search bar, for ContainerViewController to present SearchVC, on top of everything. For that, it's straightforward - containerVC.present(searchVC).
How can I get this hierarchy to work properly?
If I understand correctly, your question is how to present a view controller on top (and within the bounds) of a child view controller which may have a different frame than the bounds of the parent view. That is possible by setting modalPresentationStyle property of the view controller you want to present to .overCurrentContext and setting definesPresentationContext of your child view controller to true.
Here's a quick example showing how it would work in practice:
override func viewDidLoad() {
super.viewDidLoad()
let childViewController = UIViewController()
childViewController.view.backgroundColor = .yellow
childViewController.view.translatesAutoresizingMaskIntoConstraints = true
childViewController.view.frame = view.bounds.insetBy(dx: 60, dy: 60)
view.addSubview(childViewController.view)
addChildViewController(childViewController)
childViewController.didMove(toParentViewController: self)
// Wait a bit...
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2) {
let viewControllerToPresent = UIViewController()
viewControllerToPresent.modalPresentationStyle = .overCurrentContext // sets to show itself over current context
viewControllerToPresent.view.backgroundColor = .red
childViewController.definesPresentationContext = true // defines itself as current context
childViewController.present(viewControllerToPresent, animated: true, completion: nil)
}
}

add subview over UITableView

First of, I am noob to Swift programming.
I am trying to show a custom view over a UITableView.
I am facing two major issues :
a) The subview does not appear correctly firstly. ref image below :
and after few moments actual view is loaded :
b) After the UITableView is scrolled, the view is not coming over the current top scroll position of UITableView
Following is the code to add subview :
let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! AdPopupWithCrossViewController
vaAdPopupViewController.adID = adID
vaAdPopupViewController.tableView = self.tableView
if let message = parseJSON["adv_text"] as? String{
vaAdPopupViewController.message = message
}
let navigationBarFrame: CGRect = (self.navigationController?.view.frame)!
let frameSize = CGRect(x: navigationBarFrame.minX, y: navigationBarFrame.minY, width: navigationBarFrame.width, height: (navigationBarFrame.height - (self.navigationController?.navigationBar.frame.size.height)!))
vaAdPopupViewController.view.frame = frameSize
self.tableView.addSubview(vaAdPopupViewController.view)
vaAdPopupViewController.view.tag = 1000
self.addChildViewController(vaAdPopupViewController)
vaAdPopupViewController.didMove(toParentViewController: self)
also in AdPopupWithCrossViewController :
override func viewDidLoad() {
self.view.superview?.layoutIfNeeded()
self.view.layoutIfNeeded()
super.viewDidLoad()
}
Please guide me how to resolve this issue.
Thank You.
You probably want to either create a Present Modally Segue or use present() via code.
You've already created your AdPopupWithCrossViewController. Set its background color to be translucent. Then you can present it like this:
let vaAdPopupViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdPopupWithCrossViewController") as! ModalOverTableViewController
vaAdPopupViewController.modalPresentationStyle = .overCurrentContext
self.present(vaAdPopupViewController, animated: true, completion: nil)
Then, your little "X" button can remove the ad with:
#IBAction func closeTapped(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
You can try out the various options on .modalPresentationStyle and .modalTransitionStyle, as well as setting animated to true or false, to get the appearance / behavior you desire.
As Dan says, you should add your popup as a subview of the view controller's view, not of the table view. A Table view's view hierarchy is private and you should not be trying to mess with it.
Note that you can't do this with a table view controller. This has always bugged me, but it's a fact: A UITableViewController manages a single table view and nothing else. If you want a more complex view hierarchy you need to embed your table view controller in another view controller. I do this with a container view and an embed segue, which is very easy.
If you do that then you can add your new view controller's content to the parent view controller's content view, not to the table view controller's view (or to the table view itself)

Navigation bar not working properly under UIPageController?

I am trying to implement pagination in my application.
for that, I have created one UIPageViewController and added multiple view controller under the UIPageViewController.
for adding controller in UIPageViewController, I have used following code.
let page1: SPWalkController! = self.storyboard!.instantiateViewController(withIdentifier: "walkid") as! SPWalkController
let page2: SPCreateWalkController! = self.storyboard!.instantiateViewController(withIdentifier: "createwalkid") as! SPCreateWalkController
page1.isCreateWalk = true
pages.append(page1)
pages.append(page2)
// Create the page container
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
view.addSubview(pageContainer.view)
but the problem is added controller view showing under the navigation bar.
for that reason, I have deselected property "Under top bar" of Container controller. So my issue resolved. but when I have tried to present another view controller on that view controller and dismissed it again it shows it's sub view controller under the navigation bar.
Here are my screenshots of facing issue
First screenshot before present another view controller on it
presented another viewcontroller
After dismissal above view controller it looks like following.
The view goes under the navigation bar
How can I resolve this issue? or implement UIPageViewController is there any guideline?
I found a solution myself. I have presented view controller on root view controller. so now it is working properly.
[self.view.window.rootViewController presentViewController:navigationController animated:YES completion:nil];

UIView created in Interface Builder not rendering as expected

I'm trying to get my head around iOS development (Swift + Xcode 6), and I'm trying to figure out how what I do in the Interface Builder relates to my code.
What I've done is created PageViewController : UIPageViewController which, in its viewDidLoad method, calls setViewControllers to add a single view controller, an instance of Page11ViewController : UIViewController.
Thanks to a helpful answer on a recent question, I now know that PageViewController and Page1ViewController are being created successfully. However, I am trying to design Page1ViewController in IB, and what's being rendered doesn't reflect the work I'm doing in IB. In my storyboard, I have the following:
So "Page 1 View Controller Scene" contains a view that contains a label ("Page 1"). I have made "Page 1 View Controller" an instance of Page1ViewController by setting its class in the Identity Inspector for that view controller:
But when I run the app, I don't see my white view with a "Page 1" label; instead I see a blank red view. Why red? Because I did this:
class Page1ViewController : UIViewController {
#IBOutlet var mainView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.redColor()
println("Page 1 loaded")
println("Subviews: \(view.subviews.count)")
}
}
So I know that code is being executed, and the background is being set there...and the subview count printed out is 0...so I know that the view I'm constructing in my IB Storyboard is not the view that's actually getting drawn.
Here is the code from PageViewController that adds Page1ViewController:
class PageViewController : UIPageViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("Page view controller loaded")
let page1vc = Page1ViewController()
setViewControllers([page1vc],
direction: UIPageViewControllerNavigationDirection.Forward,
animated: true, completion: nil)
}
}
How do I attach the view I'm creating in IB to the code?
Your problem is that you are instantiating an empty Page1ViewController, rather than loading it from your storyboard so it isn't connected to any of the objects you defined in the scene.
You should use -
class PageViewController : UIPageViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
println("Page view controller loaded")
let storyboard = UIStoryboard(name:"MainStoryboard" bundle:nil);
let page1vc = storyboard.instantiateViewControllerWithIdentifier("page1ViewController") as Page1ViewController // Check the Storyboard ID for your scene in the storyboard
setViewControllers([page1vc],
direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
}
}

Resources