I want to make the view of a view controller semi-transparent. For that I have set the background color like this in the viewDidLoad method.
view.backgroundColor = UIColor(white: 0, alpha: 0.5)
When the view controller is presented, the background appears as I need it and then it turns black right away.
Why is this happening?
This is the code for showing the PopupViewController:
#IBAction func didTapShowButton(_ sender: UIButton) {
let navController = UINavigationController(rootViewController: PopupViewController())
present(navController, animated: true, completion: nil)
}
I uploaded a demo project here as well.
You may add the flag overCurrentContext (or custom), so your present might be something like:
#IBAction func didTapShowButton(_ sender: UIButton) {
let navController = UINavigationController(rootViewController: PopupViewController())
navController.modalPresentationStyle = .overCurrentContext
present(navController, animated: true, completion: nil)
}
Related
I am trying to present a view that is showd from the bottom to the top. This is my code
let myPageViewController = storyboard?.instantiateViewController(withIdentifier: "myPageViewControllerID") as! MyPageViewController
myPageViewController.modalPresentationStyle = .fullScreen
let navController = UINavigationController(rootViewController: myPageViewController)
navigationController?.present(navController, animated: true, completion: nil)
Despite I am using .fullScreen, the view is not presented on full screen.
I tried using peformSegue to show the view with this code
self.performSegue(withIdentifier: "myPageViewSegue", sender: nil)
the page is presented on fullscreen but from the left to the right and not from the bottom to the top.
The third code I tried is this one
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
here I get an error Application tried to present modally an active controller. I tried to add self.dismiss when disappearing MyPageViewController but it didn't helped.
The problem maybe is you are not presenting navigationController you are presenting detail vc, Use this
'let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .fullScreen
present(navigationController, animated: true)'
if problem still exists use
navigationController.modalPresentationStyle = .overCurrentContext
Try this one:
let detailVC = MyPageViewController()
detailVC.modalPresentationStyle = .fullScreen
present(detailVC, animated: true)
You could try this, worked for me:
Setup up your segue:
performSegue(withIdentifier: "myPageViewSegue", sender: self)
Then use the prepare for segue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destVC = segue.destination as! MyPageViewSegue
destVC.modalPresentationStyle = .fullScreen
}
I ran into this problem and was stumped. Then I realized that overriding dismiss also overrides .fullscreen
// This breaks .fullscreen
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
super.dismiss(animated: flag, completion: completion)
// do something
}
It's clear that you don't need this if .fullscreen is being used, but leaving it in place caused problems.
You need to change your code with below lines and then Booom work like a charm
let vc = MyPageViewController()
vc.modalPresentationStyle = .fullScreen
present(vc, animated: true)
let detailVC = MyPageViewController()
let navigationController = UINavigationController(rootViewController: detailVC)
navigationController.modalPresentationStyle = .overCurrentContext
present(detailVC, animated: true, completion: nil)
Explanation:
Its very simple. Its better to explain it line by line. Line 1: Declare the viewcontroller. Line 2: Add Navigation Controller (its optional. If you don't need, then you may skip navigation bar). Line 3: We have defined the modal presentation style link as overCurrentContext. and present the navigationController (it will come up with navigation bar and viewcontroller) or only the viewcontroller as per your need. In this piece of code, you will get the viewcontroller without navigation bar.
Hello I prepared simple tvOS project, where I present view controller modally. I'm receiving fade animation with white flickering (see below) while presenting view controller modally. How can I remove the flickering?
My code:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.black
}
#IBAction func button1(_ sender: Any) {
let vc = UIViewController()
vc.view.backgroundColor = UIColor(red: 0.2, green: 0, blue: 0, alpha: 1.0)
present(vc, animated: true, completion: nil)
}
}
Animation I receive:
Possibly the outgoing view is having its alpha animated to zero, which means that the wallpaper image behind it may be starting to show through a bit before the incoming view has animated its alpha in enough to cover it up.
Is this a custom presentation animation? Can you adjust the way the alpha on the outgoing view is changed?
To remove the flickering animation change animated to false.
Code:
present(vc, animated: false, completion: nil)
How to show image in overCurrentContext view in the ViewController2.swift class ? I am doing this correctly when i worked manually but not doing when i using this programmatically in swift.
ViewController1.swift code -
#IBAction func btnImage(_ sender: Any)
{
let imageVC = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
self.navigationController?.pushViewController(imageVC, animated: true)
}
ViewController2.swift code -
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.setNavigationBarHidden(true, animated: true)
self.navigationController?.modalPresentationStyle = .overCurrentContext
}
I want this background alpha 0.7 and using .overCurrentContext
Output -
You are pushing your viewController, Instead you should present it with modalPresentationStyle .overCurrentContext. Do something like this:
#IBAction func btnImage(_ sender: Any)
{
let vc = self.storyboard?.instantiateViewController(withIdentifier: "ImageVC") as! ImageVC
vc?.view.backgroundColor = UIColor.black.withAlphaComponent(0.7)
vc?.modalPresentationStyle = .overCurrentContext
vc?.navigationController?.isNavigationBarHidden = true
self.navigationController?.present(vc, animated: true, completion: nil)
}
For more information you can read more about modalPresentationStyle in Apple Docs.
Hope it helps!!
I have a question regarding blureffects on a view controller. I program all of my segues manually for several reasons. But when I go to a viewcontroller that has a blureffect background, I only see a dark gray background. How can I solve this that the view controller blur effect lays on top of another viewcontroller where the content of the previous viewcontroller can be seen through?
The blureffects are now applied by storyboard with transparant background views.
My code for seque is:
Action:
#IBAction func ToUserSections(_ sender: Any) {
let controller =
Navigation.getDestinationViewControllerWith("User",
controllerName: "UserSectionsVC")
present(controller, animated: false, completion: nil)
}
Class:
class Navigation{
static func getDestinationViewControllerWith(_
storyboardName:String, controllerName:String)-> UIViewController{
let storyboard = UIStoryboard(name: storyboardName, bundle: nil)
let controller =
storyboard.instantiateViewController(withIdentifier:
controllerName) as UIViewController
return controller
}
}
Hope you can help me with this because it will maximize aesthetics for the app :D
try this.
let viewController : CustomViewControlller = UIStoryboard.getMainStoryboard().instantiateViewController(withIdentifier: "Identifier") as! CustomViewControlller
viewController.modalPresentationStyle = .overCurrentContext
viewController.modalTransitionStyle = .crossDissolve
self.present(viewController, animated: true, completion: { _ in })
Alright for others to know, what worked for me was the following:
At IBAction: adding the following line before presenting
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
Like this:
#IBAction func ToUserSections(_ sender: Any) {
let controller = Navigation.getDestinationViewControllerWith("User", controllerName: "UserSectionsVC")
controller.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
present(controller, animated: false, completion: nil)
}
I want presented view controller vc2 in smaller size than the screen, how to do that in Swift 3 ?? Thanks for help.
This is my code:
#IBAction func leftButtonPressed(_ sender: UIButton) {
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController2
vc2.modalPresentationStyle = .currentContext
present(vc2, animated: true, completion: nil)
}
Try this..
#IBAction func leftButtonPressed(_ sender: UIButton) {
let vc2 = self.storyboard?.instantiateViewController(withIdentifier: "Controller2") as! ViewController
vc2.providesPresentationContextTransitionStyle = true
vc2.definesPresentationContext = true
vc2.modalPresentationStyle=UIModalPresentationStyle.overCurrentContext
self.present(vc2, animated: true, completion: nil)
// Make sure your vc2 background color is transparent
vc2.view.backgroundColor = UIColor.clear
}
You can simply use a container view to display the smaller view controller. Here is a great tutorial on container views: https://cocoacasts.com/managing-view-controllers-with-container-view-controllers/
Hope this was helpful :)