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 :)
Related
I'm using NavigationController to operate the view.
If the value is exceeded on the first view and the value is not satisfied with the 'if' on the second view, I hope 'dismiss' will work.
I have made a similar simple example.
//First View
class ViewController: UIViewController {
#IBAction func goSecond(_ sender: Any) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.navigationController?.pushViewController(DvC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
//Second View
class SecondViewController: UIViewController {
#IBAction func goThird(_ sender: Any) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "ThirdViewController") as! ThirdViewController
let num = 1
DvC.num = num
self.navigationController?.pushViewController(DvC, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
//Third View
class ThirdViewController: UIViewController {
var num = Int()
override func viewDidLoad() {
super.viewDidLoad()
print("num: ")
print(num)
if(num != 2) {
print("Success")
// dismiss(animated: true, completion: nil)
// navigationController?.dismiss(animated: true, completion: nil)
} else {
print("Failed")
}
}
}
The third view shows "Success", but the codes below it do not work.
dismiss(animated: true, completion: nil) // not work
navigationController?.dismiss(animated: true, completion: nil) // not work
Pressing the button on the first view moves to the second view, and pressing the button on the second view moves to the third view.
If the value passed when I go to the third view is not satisfied with 'if', I want to go back to the first view.
I want the second and third views to end, not just the screen shift.
Like the finish() of Android.
How can I exit the second and third views and return to the first view?
You are pushing the view controllers onto the stack. dismiss is for dismissing VCs presented modally.
What you want to do is pop the VC to go back one.
navigationController?.popViewController(animated: true)
or to go back to the root view.
navigationController?.popToRootViewController(animated: true)
Here i'm dismissing one VC and trying to navigate new VC , but navigation not working. Dismiss VC working fine.
#IBAction func onClickEmailBtn(_ sender: UIButton) {
//dismiss VC
self.dismiss(animated: false, completion: nil)
//Navigate to VC
DispatchQueue.main.async {
let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
self.navigationController?.pushViewController(cevc, animated: true)
}
}
Here I have one NavigationViewController called VC1, on that I'm presenting one VC called VC2. In this VC2 when I click button I want to navigate new VC called EmailVC.
try this code
#IBAction func onClickEmailBtn(_ sender: UIButton) {
if let controller = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController {
self.dismiss(animated: false, completion: nil)
self.presentingViewController?.present(controller, animated: true, completion: nil)
}
}
You were trying to push a ViewVontroller from dismissing view controller,
and the answer which you have accepted is presenting a viewController from dismissing viewController. Although it may serve your purpose, but I am going to answer you original question
// get the object of presenting nanvigation controller(navigation controller with has presented this view controller)
let presentingNavigationController = presentingViewController?.navigationController
dismiss(animated: true) {
let cevc = self.storyboard?.instantiateViewController(withIdentifier: "CEVC") as! EmailViewController
// here you push your view controller with presentingNavigationController
presentingNavigationController?.pushViewController(cevc, animated: true)
}
Try to find out Navigation controller using below code and replace Viewcontroller with your Controller name.
let productVC = SeconViewViewController()
let parentVC = (parent!.presentingViewController as! ViewController).childViewControllers.first as! UINavigationController
self.navigationController?.dismiss(animated: true, completion: {
parentVC.pushViewController(productVC, animated: true)
})
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!!
Im using a UITabBarController that I've created programatically to present UIViewControllers that I've created in Storyboard. I present these with
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// create tab one
let tabOne = storyboard.instantiateViewController(withIdentifier: "feedVC")
let tabOneBarItem = UITabBarItem(title: "feed", image: UIImage(named: "feed_icon"), selectedImage: UIImage(named: "feed_icon_selected"))
tabOne.tabBarItem = tabOneBarItem
self.viewControllers = [tabOne]
in one of the tabs where I don't show the TabBar, I have a cancel button in the top left. I prefer this to send the user back to the previous ViewController. But settle with a specific one. The code below works, but the TabBarController do not show... what is the most efficient way to implement a cancel/return button.
Should I use UINavigationController - or are there better alternatives?
#IBAction func cancelBtnWasPressed(_ sender: Any) {
let returnVC = storyboard?.instantiateViewController(withIdentifier: "feedVC")
present(returnVC!, animated: true, completion: nil)
}
I found the solution that worked for me.
Swift 4:
self.tabBarController?.selectedIndex = 0
This moves the view back to the first of the Tabs i the TabBarController
You have to dismiss the current one :
#IBAction func cancelBtnWasPressed(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
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)
}