Navigation help

Discussion in 'iOS Programming' started by McBgnr, Jul 6, 2010.

  1. macrumors regular

    Joined:
    Apr 13, 2009
    #1
    I have a simple application in which I have TableView inside a NavigationController. And when I select any row in table view I show the details view. I want to display details view in landscape orientation as well. But when I try to load landscape view for details I am not getting the navigation bar on it. The navigation bar is shown in portrait view.

    I am not sure what could be the problem. I am using presentModalViewController.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Subclass UINavigationController and override shouldAutorotateToInterfaceOrientation: to allow rotation. Remember to ensure you are using your subclass, not UINavigationController itself.
     
  3. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #3
    Is there any other possiblity like adding NavigationItem to the landscape view? I am trying this as well but the navigationitem does not show at all.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Why are you adding/removing navigation items in the first place: the UINavigationController is there to provide these, manage the navigation stack and so on.
     
  5. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #5
    I am not sure why I am not able to get it working with the navigation controller :-(.

    I will try subclassing it.
     
  6. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #6
    I have subclassed the NavigationController to support orientation.

    Now when I want to change from Portrait to Landscape, I am using popViewcontroller to pop the portrait view and pushViewController to push the landscape view.

    Although I can see the Navigation bar at the top but the newly pushed landscape view is not visible at all. Instead the parent tabelview is visible. Any ideas what is going wrong here?
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    What? :confused: When the device rotates why are you pop/pushing view controllers? You have different controllers for portrait and landscape?
     
  8. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #8
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    I'm afraid I can't suggest much. I don't use XIBs at all and when I switch to landscape I simply have the views auto-resize to fit (so only have one view controller).
     
  10. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #10
    So can I have two Views with the same view controller, one for each orientation.

    This seems to work and the landscape view gets shown. But one problem here is that although the navigation stack has only two view controllers, it requires 3 steps to go back to the tableview of the application. The navigation bar shows 3 levels even when the number of views is just 2.
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    No idea. I've never wanted to do that. As I said I just resize/reposition my existing views as appropriate on orientation change.
     
  12. macrumors 6502

    Joined:
    Jan 16, 2008
    Location:
    Nottingham, England
    #12
    This sounds wrong. You should try and design your view to work in both orientations -- use the little arrow in the top right of the IB layout window to quickly rotate the window. Use the elastic anchor points to make things resize properly. If you use the same view, you get the added bonus of being able animate the transition to make things grow/shink in a pleasing manner. There was a great video on this exact topic in the 2009 WWDC videos, if you have them.
     
  13. thread starter macrumors regular

    Joined:
    Apr 13, 2009
    #13
    Thanks adrian.oconnor for the reply.

    I have added resizing support in my view. So now I have a main view which does not support orientation and a detail view which supports both orientations. I am facing a problem even here and that is:

    1. Open application with Portrait of Simulator. It opens with main view in portrait. Go to the detail view, which opens in portrait. Rotate the simulator so that view changes to landscape. Now click back button on landscape view, it takes me back to the main view. But the main view is now shown in Landscape mode now. I want the main view to be shown in Portrait orientation.

    2. One more problem is that when I get back from detail landscape to main view the navigation bar still has a back button. Ideally it should not have any because we have returned to the main view.

    Any suggestions for these problems are welcome. I am not sure what is going wrong here.
     
  14. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #14
    This will only lead to problems. You should make all your view controllers support rotation.
     
  15. macrumors 6502

    Joined:
    Jan 16, 2008
    Location:
    Nottingham, England
    #15
    OK, this is possible (I know because I did the same thing in one of my applications just the other week), but because you're using a NavigationViewController for both views you have a little bit of extra work to do. Here's the relevant code to illustrate how you can control ability to rotate a ViewController:

    Code:
    -(BOOL) shouldRotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
      return interfaceOrientation == UIInterfaceOrientationPortrait; // also LandscapeLeft, LandscapeRight
    }
    Because you're using a single view controller, you'll need to change the result based on which screen is currently active, but you will find that simple enough.

    As for the back button, can't you just hide it? I haven't used a NavigationController for a while, so ignore me if my suggestion is silly :)

    BTW, there's nothing wrong with having one screen fixed in portrait and a second screen that rotates if it makes sense for your application. the iTunes Connect application does this, for instance, and I'm sure plenty of others do too.
     

Share This Page