Container View Page Controller is Being Clipped instead of Resized - ios

I am about fifteen seconds away from flipping over my desk and rage quitting, because I just cannot figure this out.
I have a Page View Controller as the embed of a Container View Controller. Sounds easy, right?
Problem is, the Page View Controller is getting clipped. I need it to resize. So they would be cute little pages with their full VC content.
I've tried every option on the Storyboard Display... things.
Here's a couple of pics to describe the problem:
Now I would share the code with you, but all it is the loading of the (single) page. The page does exist in a different storyboard, but even when I changed the view for View Controllers in the Main storyboard, they were still being cut off.
override viewDidLoad() {
let storyboard = UIStoryboard(name: "TEMPLATES", bundle: Bundle.main)
let vc = storyboard.instantiateViewController(withIdentifier: "template1")
self.setViewControllers([vc!], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight] //Some code I found on Stack Overflow that did nothing
super.viewDidLoad()
}
Any help would be appreciated!

if you add it from the storyboard - it will just work.
if you add the child view controller from code - you also need to set the frame like so:
carsTableVC.view.frame = self.container.bounds
here is a full code example (carsTableVC is in a container):
let sb = UIStoryboard.init(name: "Cars", bundle: nil)
self.carsTableVC =
sb.instantiateViewController(withIdentifier:
"CarsTableViewController") as? CarsTableViewController
carsTableVC.view.frame = self.container.bounds
carsTableVC.willMove(toParent: self)
addChild(carsTableVC)
carsTableVC.didMove(toParent: self)
container.addSubview(carsTableVC.view)

Related

Adding Custom popup doesn't filling the full view iOS, Swift

I have created a UIViewControlller and designed in storyboard, I want to show it as popup for my another view.
I used the following to show as popup:
guard let popupVC = UIStoryboard(name: "More", bundle: nil).instantiateViewController(withIdentifier: "LanguageSelectionViewController") as? LanguageSelectionViewController else{
fatalError("Unexpected destination VC")
}
self.addChildViewController(popupVC)
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.tableView.frame)")
popupVC.view.frame = self.tableView.frame
print("POPUP Frame VIEW -- \(popupVC.view.frame)")
print("SElF VIEW -- \(self.view.frame)")
popupVC.modalPresentationStyle = .currentContext
popupVC.modalTransitionStyle = .crossDissolve
popupVC.view.bindFrameToSuperviewBounds()
self.view.addSubview(popupVC.view)
popupVC.didMove(toParentViewController: self)
It is working properly, but on my device half of the screen space is not filled by popup view.
Have you tried using the present() function? Something like this:
let popupVC = PopupViewController()
popupVC.modalPresentationStyle = .overFullScreen
popupVC.modalTransitionStyle = .crossDissolve
self.present(popupVC, animated: true, completion: nil)
I find designing view controllers in their own xib files easier, and it allows initialisation like in my example, rather than having to use the storyboard in code, but thats personal preference.
If it still isnt working, maybe check that the layout constraints for the popup's background are correct, so it actually fills it's parent.
You need to swap these two lines:
self.view.addSubview(popupVC.view)
popupVC.view.bindFrameToSuperviewBounds()
You can't bind a view to it's superview if the view hasn't been added to it yet.
First thing you need click on popup(storyboard or xib) and set it to background color .clear Color
Then make it to center. like
popupVC.center = self.view.center
it's just a guide you can change and set names accordingly. But my question why you are using storyboard why not have created Xib or View
Hope it will serve your purpose.

Issues using pageSheet in iOS 12, Xcode 10

I'm trying to recreate the effect where the view controller underneath the one I'm presenting onto the user's iPhone is positioned behind the presented view controller so you can see the top of the previous VC.
#objc func showSteps() {
let vc = UIStoryboard(name: Storyboards.Main.rawValue, bundle: nil).instantiateViewController(withIdentifier: HowToSteps.four.stepAsString())
vc.modalPresentationStyle = .pageSheet // I've also tried the other enum options here to no effect
let nc = UINavigationController(rootViewController: vc)
present(nc, animated: true)
}
However the presented view controller just comes up as full screen without the ability to see the view controller underneath behind it.
Any help here would be appreciated. Thanks!
Edit:
The effect I'm going for:

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)

Programatically creating Segues in ios swift

In my app I use side bar as in facebook. when the user slides out the side bar a uiimageview is displayed. when user taps on the image it takes hm to a different viewcontroller. the problem i am facing is that I have created sidebar programatically and the other view to which I want to navigate the user is created using storyboard. So my source view is created programatically and destination view is created using storyboard. So can someone explain me if there is any way of using "Segue" in this scenario. Since i can not create segue using storyboard I need to do it programatically but even after a lot of googling i could not find the answer.
Well, to get another instance of another storyboard programmatically you can use something like:
let newController = UIStoryboard(name: "MyStoryboard", bundle: nil).instantiateViewControllerWithIdentifier("MyIdentifier") as! MyViewController
and then you push to your navigation controller, or add as a child view controller or something...
If you don't wanna bother with identifiers you can just use the instantiateInitialViewController instead of instantiateViewControllerWithIdentifier
Might help
"userSB" is the viewcontroller storyboard identifier
#IBAction func tapSearchCriteria(_ sender: Any?) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let aVC = storyboard.instantiateViewController(withIdentifier: "userSB") as? AViewController
aVC?.modalPresentationStyle = UIModalPresentationStyle.custom
aVC?.transitioningDelegate = self
aVC?.udelegate = self
self.present(aVC!, animated: true, completion: nil)
}

showViewController method only displays modal views

Looking at Apples Resource for using showViewController() versus presentViewController() I can't seem to figure out my issue here.
I want to display a viewController from my storyboard programmatically by doing the following:
if let resultController = storyboard!.instantiateViewControllerWithIdentifier("mainTab") as? TabBarController {
showViewController(resultController, sender: UIBarButtonItem.self)
}
This works fine. However, it's presentation is always modal. Covering the screen vertically from bottom to top like so:
I was/still am under the impression that I should use showViewController to display a screen without the animation... But clearly I'm missing something here. All I want to do is display the VC non-modally. I feel like I've tried everything to no avail.
Thanks for the advice in advance.
I think you want to use something like this.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("Controller") as! UIViewController
self.presentViewController(vc, animated: false, completion: nil) // no animation or presentation as modal because animated is false
The key is to have animated set to false. This method works for me. Hope this helps!

Resources