Preferred content height not working in Swift 3.0 - ios

I'm presenting view controller(embedded with navigation controller) as form sheet, setting the preferred content size changes the width of presented view but height is always from top to bottom. Following is my code
let chooseCategoryNavController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "stbIdChooseCatehoryManufacturerNavVC") as? UINavigationController
chooseCategoryNavController?.modalPresentationStyle = UIModalPresentationStyle.formSheet
chooseCategoryNavController?.transitioningDelegate = self
chooseCategoryNavController?.presentationController!.delegate = self
if let aController = chooseCategoryNavController?.viewControllers.first as? ChooseCategoryManufacturerViewController
{
aController.preferredContentSize = CGSize(width: 100, height: 300)
}
chooseCategoryNavController?.preferredContentSize = CGSize(width: 100, height: 300)
self.present(chooseCategoryNavController!, animated: false, completion: nil)
How can I customize height? As above code doesn't seem to affect height.

Try to call
self.layoutIfNeeded()
and put this line of code right before frame properties changes.

Related

Get rid of shadow form popover view controller (Swift)

I am trying to create number pad in iPad using popover view controller. Everything is achieved but there is a shadow beneath the pop view. I tried to remove that shadow but nothing worked for me. Here is my code which presents pop over view in my view controller.
let vc = self.storyboard?.instantiateViewController(withIdentifier: "PopOverVC") as? PopOverVC
vc?.modalPresentationStyle = .popover
vc?.preferredContentSize = CGSize(width: 280, height: 425)
vc?.delegate = self
if let popoverPresentationController = vc?.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = [.down, .up, .right, .left]
popoverPresentationController.sourceView = self.view
popoverPresentationController.sourceRect = txtNumber.frame
popoverPresentationController.delegate = self
if let popoverController = vc {
present(popoverController, animated: false, completion: nil)
}
}
Can anybody help me removing the shadow? Thanks in advance!!

How to change size of Popover view

I am trying to lay out a popover view when a button on current view is pressed. This works fine with following code but problem is that the popover view does not take the size from .preferredContentSize.
IngredientVC.preferredContentSize = CGSize(width: 200, height: 300)
Popover view has height and width of current view. How do I make Popover view smaller size please?
#IBAction func actionIngredients(_ sender: Any)
{
// Load and configure your view controller.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let IngredientVC = storyboard.instantiateViewController(
withIdentifier: "testViewIdentifier")
//let IngredientVC:testViewController = testViewController()
// Use the popover presentation style for your view controller.
IngredientVC.modalPresentationStyle = .popover
IngredientVC.preferredContentSize = CGSize(width: 200, height: 300)
// Specify the anchor point for the popover.
//IngredientVC.popoverPresentationController?.barButtonItem =
//optionsControl
//let popovercontroller = optionsVC.popoverPresentationController
IngredientVC.popoverPresentationController?.delegate = self
IngredientVC.popoverPresentationController?.sourceView = self.view
IngredientVC.popoverPresentationController?.sourceRect = CGRect(x:self.view.frame.width/2, y: self.view.frame.height/2,width: 200,height: 300)
IngredientVC.popoverPresentationController?.permittedArrowDirections = [.down, .up]
// Present the view controller (in a popover).
self.present(IngredientVC, animated: true)
}
Note: I have popover view controller in Storyboard with Simulated Metrics size as "Freeform".
I figured out that all I needed was following function -
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle
{
return UIModalPresentationStyle.none
}

How to position subview below left bottom corner of a button in view controller?

I have an options button (that 3 vertical dotted button) on top of the view controller.
When I click that button, A list view should appear like in many other apps like WhatsApp...
I really don't know how to position it always near to the button programmatically.
Placing at the left-bottom of an existing view is quite easier:
func placeSubView(existingView: UIView)
{
let desiredWidth = CGFloat(50.0)
let desiredHeight = CGFloat(35.0)
let (x, y) = (existingView.frame.origin.x - desiredWidth, existingView.frame.origin.y + existingView.frame.size.height)
let desiredView = UIView(frame: CGRect(x: x, y: y, width: desiredWidth, height: desiredHeight))
existingView.superview?.addSubview(desiredView)
}
UPDATE:
In case you are looking for popupo menu like view, you should search about UIPopoverPresentationController.
Something like this.
Use this method and make PopoverViewController a table view controller if you want a list.
#IBAction func displayPopover(_ sender: UIBarButtonItem) {
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "PopoverViewController")
vc.modalPresentationStyle = .popover
let popover: UIPopoverPresentationController = vc.popoverPresentationController!
popover.barButtonItem = sender
present(vc, animated: true, completion:nil)
}

Present small viewController

Problem State: I've two viewController
parentViewController (375 * 667)
childViewController (300 * 667)
and i want to present childViewController at Axis (75 * 0) as shown below
childViewController is show on this IBAction
#IBAction func btnShowVC(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newVC = storyboard.instantiateViewController(withIdentifier: "cvc") as? childViewController
self.present(newVC!, animated: true, completion: nil)
}
But it stretches itself like
can anyOne help me to tackle it?
Thanks
You can try in this way.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newVC = storyboard.instantiateViewController(withIdentifier: "ChildViewController") as? ChildViewController
view.addSubview((newVC?.view)!)
newVC?.view.frame = CGRect(x:75,y:0,width:view.frame.size.width-75,height:view.frame.size.height)
newVC?.view.autoresizingMask = \[.flexibleWidth, .flexibleHeight\]
newVC?.didMove(toParentViewController: self)
Where are you setting the frame of your ViewController? If you are using autolayout you will need to set constraints to make the ViewController look how you want it to. If you want to do it programatically you can set the frame somewhere for example in the code that you posted above you could do this:
#IBAction func btnShowVC(_ sender: UIButton) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let newVC = storyboard.instantiateViewController(withIdentifier: "cvc") as? childViewController
newVc.view.frame = CGRect(x: 75, y: 0, width: 300, height: 667)
self.present(newVC!, animated: true, completion: nil)
}
By setting preferredContentSize property will solve your problem, Please try out below code snippet
newVC?.preferredContentSize = CGSize(width: 300, height: 667)

Resize a PopOver View Controller

I have a view controller that I want to present as a popover. How can I change its size?
let carsViewController = CarsViewController()
carsViewController.modalPresentationStyle = .Popover
if let popoverPresentationController = a carsViewController.popoverPresentationController {
popoverPresentationController.permittedArrowDirections = .Up
popoverPresentationController.sourceView = carsButton
presentViewController(alertsViewController, animated: true, completion: nil)
}
It took me some time but I finally found an answer to this. It is as simple as adding the following line after initialising the view controller.
carsViewController.preferredContentSize = CGSize(width: 220,height:90)

Resources