iDevelopper / PBRevealViewController

A UIViewController subclass for revealing a left and/or right view controller above or below a main view controller.
MIT License
80 stars 16 forks source link

Problem with PBrevealViewController and width #43

Closed FedericoSub closed 6 years ago

FedericoSub commented 6 years ago

Hi I'm starting with my new app for IOS in Swift and I'd like to use this Menu but there's something that doesn't work when I set this: revealViewController()?.rightViewRevealWidth = CGFloat(Float(210)) If I use the default all works with right menu but if i try to change with a width doesn't work. When the right menu is open if i click un HOME label the left VC didn't pushed on left and the left VC become Dark like in Picture. As I said if I commented the row with CGFloat(Float(210) all works . This is MainViewController : class MainViewController: UIViewController {

@IBOutlet weak var rightButton: UIBarButtonItem!

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.

    rightButton.target = self.revealViewController()
    self.revealViewController()?.isRightPresentViewOnTop = false
    revealViewController()?.rightViewRevealWidth = CGFloat(Float(210))
    rightButton.action = #selector(PBRevealViewController.revealRightView)

    if Reachability.isConnectedToNetwork(){
        print("Internet Connection Available!")
    }else{
        Validator.Alert(title: "Attenzione!", message: "rete non disponibile", vc: self)
        let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
        self.present(viewController, animated: false, completion: nil)

    }     
}

and this MenuViewController: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let revealViewController:PBRevealViewController = self.revealViewController()! let cell:MenuTableViewCell = tableView.cellForRow(at: indexPath) as! MenuTableViewCell

    if cell.vociMenuLbl.text == "Home" {

       let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
       let desController = mainStoryboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
        let newFrontViewController = UINavigationController.init(rootViewController:desController)
        revealViewController.setMainViewController(newFrontViewController, animated: true)

    }
    if cell.vociMenuLbl.text == "Profilo" {
        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let desController = mainStoryboard.instantiateViewController(withIdentifier: "ProfiloViewController") as! ProfiloViewController
        let newFrontViewController = UINavigationController.init(rootViewController:desController)
        revealViewController.pushMainViewController(newFrontViewController, animated: true)

    }
    if cell.vociMenuLbl.text == "Logout"  {
        let firebaseAuth = Auth.auth()
        do {
            try firebaseAuth.signOut()
            let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login")
            self.present(viewController, animated: false, completion: nil)
        } catch let signOutError as NSError {
            print ("Error signing out: \(signOutError)")
        } catch {
            print("Unknown error.")
        }
    }

}

As I said if I click "Home" this is the effect

schermata 2018-01-04 alle 18 09 49 schermata 2018-01-04 alle 18 10 07 schermata 2018-01-04 alle 18 10 22

Any suggest ? thanks

iDevelopper commented 6 years ago

Because you are setting rightViewRevealWidth when right menu is still open and in this case the behaviour of PBRevealViewController is to change the MainViewController and rightViewController frames causing the bug...

Solution 1: In your MainViewController, check the rightViewRevealWidth value:

        self.revealViewController()?.isRightPresentViewOnTop = false
        if revealViewController()?.rightViewRevealWidth != 210 {
            revealViewController()?.rightViewRevealWidth = 210
        }

Solution 2: Set rightViewRevealWidth in your right menu controller:

    override func viewDidLoad() {
        super.viewDidLoad()

        revealViewController()?.rightViewRevealWidth = 210
        // ...

Solution 3 (the better one): Keep a reference to your main view controller (Home) to not instantiate and load it each time from storyboard.

FedericoSub commented 6 years ago

Thankx ..later I'll try your solution.... Just a information ..what do you mean "Keep a reference to your main view controller (Home)" ? can you give me a example ? thanx

iDevelopper commented 6 years ago

Yes, you will also see in this sample the use of PBRevealViewDelegate protocol:

SimpleCollectionView.zip

FedericoSub commented 6 years ago

Thanks a lot now works well

FedericoSub commented 6 years ago

Hi again I need your help again for same app. :-( I made a new VC called ProfiloROTableViewController where I make the Navbar transparent with a nice aspect. If I call from ProfiloRO this VC with : performSegue(withIdentifier: "segueProfilo", sender: self)

all works and you see the navbar button but if I try to link using PBRevealViewController navbar item disappeared like in picture attached. Archivio.zip

schermata 2018-01-14 alle 00 26 10 schermata 2018-01-14 alle 00 31 53 schermata 2018-01-14 alle 00 32 32

the code to call the VC from MenuTableViewController is : if cell.vociMenuLbl.text == "ProfiloRO" {

        let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let desController = mainStoryboard.instantiateViewController(withIdentifier: "ProfiloROTableViewController") as! ProfiloROTableViewController
        let newFrontViewController = UINavigationController.init(rootViewController:desController)
        revealViewController.pushMainViewController(newFrontViewController, animated: true)

Any idea how to make navebar item visible ?

thanks again

iDevelopper commented 6 years ago

I think you have just to remove

    bar.alpha = 0.0

in your ProfiloROTableViewController

FedericoSub commented 6 years ago

Great work !!! just another question... If from this VC tap the Botton "Modifica Profilo" I open a new VC where I change and save some value about my Profile. After save data i need to return to this previous page. I use a back button with Unsegue return , but if I'd like to RELOAD the ProfiloROTableViewController how I can this ?

I tried with: let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let desController = mainStoryboard.instantiateViewController(withIdentifier: "ProfiloROTableViewController") as! ProfiloROTableViewController let newFrontViewController = UINavigationController.init(rootViewController:desController) revealViewController.pushMainViewController(newFrontViewController, animated: true)

but don't work

thanx for any suggestion .. Federico

iDevelopper commented 6 years ago

When you tap on "Modifica Profilo" button, how do you present the new VC? (push, present modal)?

In ProfiloROTableViewController, in viewWillAppear, reload the data for the tableView and the collectionView.

self.tableView.reloadData() self.collectionView.reloadData()

So, when you come back from the new VC, your datas will be updated on the view.

DO NOT re-instantiate a ProfiloROTableViewController!

FedericoSub commented 6 years ago

thanxs again