Home > Enterprise >  showing ViewController inside navigationController from a presented screen : ios, swift
showing ViewController inside navigationController from a presented screen : ios, swift

Time:06-30

So I have a profile page and on rightBarButtonItem I have a menu option. when I click that menu, the next viewController is presented as a half-screen sheet. using code.

let nextVc = storyboard.instantiateViewController(withIdentifier: "MenuSheetStoryboard") as! MenuSheetController
        
      
        if let sheet =  nextVc.sheetPresentationController {
        sheet.detents = [.medium ()]
        }
        
        self.present(nextVc, animated: true)

The menu sheet contains a table view with menu items. one of them is Edit profile. So when I Click that option I want to go to EditProfileViewController which shows the back button and has option to pop the current Vc as there is on ViewControllers inside NavigationviewControllers.

I am using this code from going from menuVc to EditProfileVc

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "EditProfileStoryboard") as! EditProfile
        
        
        self.present(nextViewController, animated: true)

Now the problem with this is. Either I present it as a sheet so that users can swipe it down(which I don't want and also there is a save button on which by clicking user gets back to the previous screen automatically, as popViewController does) or with full screen,in this case there is no way to get back to the previous screen from here.

CodePudding user response:

Q1.

While presenting any view controller you can use the modalPresentationStyle property to tell your view controller how it should display on the current context

Refer available UIModalPresentationStyle

public enum UIModalPresentationStyle : Int, @unchecked Sendable {
    case fullScreen = 0
    case pageSheet = 1
    case formSheet = 2
    case currentContext = 3
    case custom = 4
    case overFullScreen = 5
    case overCurrentContext = 6
    case popover = 7
    case none = -1
    case automatic = -2
}

Q2.

So, use UINavigationController when you are presenting ViewController

Ex:-

func onEditProfileTapped(_ sender: Any) {
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    if let profileVC = storyBoard.instantiateViewController(withIdentifier: "EditProfileStoryboard") as? EditProfileStoryboard {
        let nav = UINavigationController(rootViewController: profileVC)
        nav.modalPresentationStyle = .currentContext
        self.present(nav, animated: true)
    }
}

And now you are able to add Done button in EditProfileStoryboard screen like below

class EditProfileStoryboard: UIViewController {
    override func viewDidLoad() {
        let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(onDoneButtonTapped(_:)))
        self.navigationItem.leftBarButtonItems = [doneButton]
    }
    
    @objc func onDoneButtonTapped(_ sender: UIBarButtonItem) {
        self.dismiss(animated: true)
    }
}
  • Related