How to show image in overCurrentContext view in the ViewController2.swift class? - ios

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!!

Related

Show BlurEffect on manual segue

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)
}

segue back with NavigationBar

Hello I am in Viewcontroller B with its NavigationBar, button pressed in Viewcontroller B goes to Viewcontroller C (I don't want NavigationBar in Viewcontroller C)
let vc = self.storyboard?.instantiateViewController(withIdentifier: "viewC") as! ViewcontrollerC
vc.passAction = "saveedit"
vc.passName = passName
self.present(vc, animated: true, completion: nil)
When I click save Button in ViewcontrollerC I should go back to ViewcontrollerB with ViewcontrollerB NavigationBar
let vc = self.storyboard?.instantiateViewController(withIdentifier: "viewB") as! ViewcontrollerB
vc.passName = "\(firstNameTxt.text!)\(" ")\(lastNameTxt.text!)"
self.present(vc, animated: false, completion: nil)
Here the problem is When I go back to ViewcontrollerB I don't see NavigationBar there.
EDITED
class ViewcontrollerB : UpdateDataDelegate {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}
#IBAction func click_edit(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "patientPersonalData") as! patientPersonalDataVC
vc.passName = passName
vc.passAction = "saveedit"
self.navigationController?.pushViewController(vc, animated: true)
}
func loadData() {
}
}
//ViewcontrollerC
protocol UpdateDataDelegate {
func loadData()
}
class Viewcontroller C {
var delegate: UpdateDataDelegate?
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
fun click_save() {
self.navigationController?.popViewController(animated: true)
self.delegate?.loadData()
}
}
It seems that you create a new ViewcontrollerB.
Try this to go back to ViewcontrollerB.
self.dismiss(animated: true, completion: nil)
Why don't you simply dismiss ViewcontrollerC on pressing Save button?
In ViewcontrollerC:
var closure: ((String)->())? //Set this closure when you present ViewcontrollerC from ViewcontrollerB
func save()
{
self.dismiss(animated: true) {[weak self] in
self?.closure?("Your_Data")
}
}
This will move you back to ViewControllerB where the navigation bar is already present.
You don't need to hide/show the navigation bar anywhere, neither while presenting ViewControllerC, nor when dismissing it.
Try to present it using navigation controller
let vc = self.storyboard!.instantiateViewController(withIdentifier: "viewC") as! ViewcontrollerC
let navController = UINavigationController(rootViewController: vc)
self.present(navController, animated:true, completion: nil)
and add the following line in your ViewcontrollerC's viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
when leave from ViewcontrollerC's viewWillDisAppear method.
override func viewWillDisAppear(_ animated: Bool) {
super.viewWillDisAppear(animated)
self.navigationController?.isNavigationBarHidden = false
}
I just have posted code dedicated on UINavigationBar appearance management on github. check out RRViewControllerExtension, it will solve your problem gracefully.
with RRViewControllerExtension all you have to do is just override method in your viewcontroller.
//override any of the methods below in your viewcontroller's .m file to make specific navigation bar appearance
-(BOOL)prefersNavigationBarHidden;
-(BOOL)prefersNavigationBarTransparent;
-(nullable UIColor *)preferredNavatationBarColor;
-(nullable UIColor *)preferredNavigationItemColor;
-(nullable UIImage *)preferredNavigationBarBackgroundImage;
-(nullable NSDictionary *)preferredNavigationTitleTextAttributes;
Try this .. unhide navigationBar in viewWillAppear of viewControllerC and hide it in viewWillAppear of viewControllerB .
//ViewController C
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.isNavigationBarHidden = true
}
//ViewController B
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.isNavigationBarHidden = false
}
try this then
//ViewController C
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.setNavigationBarHidden(true, animated: true)
}
//ViewController B
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated);
self.navigationController?.setNavigationBarHidden(false, animated: true)
}

How to present a view controller with smaller size

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 :)

Cannot update textfield in delegate callback method

I have implemented delegate in LoginViewController defined in RegistrationViewController. Callback function is calling but the problem is that I am not able to update the textfield of LoginViewController in delegate method.
LoginViewController.swift
import UIKit
class LoginViewController :UIViewController,RegisterViewDelegate {
#IBOutlet weak var mobileNumber: UITextField!
#IBAction func showRegistrationView(_ sender: Any) {
let controller = storyboard?.instantiateViewController(withIdentifier: "registration") as! RegistrationViewController
controller.delegate = self
present(controller, animated: false, completion: nil)
}
func onUserRegistrationCompletion(number: String) {
print(number) // output is 05010101010
DispatchQueue.main.async {
self.mobileNumber.text! = number
print(self.mobileNumber.text!) . // output is empty
}
}
}
RegistrationViewController.swift
import UIKit
class RegistrationViewController: UIViewController {
weak var delegate:RegisterViewDelegate?
#IBAction func register(_ sender: Any) {
self.delegate?.onUserRegistrationCompletion(number: "05010101010")
let controller = self.storyboard?.instantiateViewController(withIdentifier: "login")
present(controller!, animated: false, completion: nil)
}
}
protocol RegisterViewDelegate:class {
func onUserRegistrationCompletion(number:String)
}
it means you allocating the memory again on self.storyboard?.instantiateViewController(withIdentifier: "login") in Register VC, that the reason your delegate is by default goes to nil.
#IBAction func register(_ sender: Any) {
self.delegate?.onUserRegistrationCompletion(number: "05010101010")
self.dismiss(animated: true, completion:nil)
}
I suggest you to use navigationController.
and you can push to next view this way:
let controller = storyboard?.instantiateViewController(withIdentifier: "registration") as! RegistrationViewController
controller.delegate = self
self.navigationController?.pushViewController(controller, animated: true)
and when you want to go back to previous view use this code:
self.delegate?.onUserRegistrationCompletion(number: "05010101010")
self.navigationController?.popViewController(animated: true)
And your result will be:
Check THIS sample for more info.
And don't forget to Embed In your LoginViewController into navigationController from storyboard. as shown in demo project.

Add Transition effect without using navigationController- IOS

I want to add a transition effect to my app from a ViewController to another.
I want use the transition effect that the Navigation Controller usually add (sliding effect) but without using the Navigation Controller.
What is the simplest and easiest way to achieve that? (I'm using swift 3)
EDIT:
My Code Now:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func GoToMenu(_ sender: UIButton) {
//TRANSITION EFFECT
let nextVCID="010101"
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: nextVCID) else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(presentedController, animated: true, completion: nil)
}
}
You can present it modaly and set modal transition style you like.
guard let presentedController = self.storyboard?.instantiateViewController(withIdentifier: nextVCID) else { return }
presentedController.modalTransitionStyle = UIModalTransitionStyle.coverVertical
self.present(presentedController, animated: true, completion: nil)

Resources