PDA

View Full Version : Navigation help




McBgnr
Jul 6, 2010, 03:46 AM
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.



robbieduncan
Jul 6, 2010, 04:00 AM
Subclass UINavigationController and override shouldAutorotateToInterfaceOrientation: to allow rotation. Remember to ensure you are using your subclass, not UINavigationController itself.

McBgnr
Jul 6, 2010, 04:23 AM
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.

robbieduncan
Jul 6, 2010, 04:24 AM
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.

McBgnr
Jul 6, 2010, 04:43 AM
I am not sure why I am not able to get it working with the navigation controller :-(.

I will try subclassing it.

McBgnr
Jul 6, 2010, 05:12 AM
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?

robbieduncan
Jul 6, 2010, 05:16 AM
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?

What? :confused: When the device rotates why are you pop/pushing view controllers? You have different controllers for portrait and landscape?

McBgnr
Jul 6, 2010, 05:43 AM
Yes, I am having different layout so I thought of using different XIBs with different view controllers for each. Not sure what is the correct way for doing so. Suggestions pls.

I was referring to the AlternateViews sample available at http://developer.apple.com/iphone/library/samplecode/AlternateViews/Introduction/Intro.html

robbieduncan
Jul 6, 2010, 05:46 AM
Yes, I am having different layout so I thought of using different XIBs with different view controllers for each. Not sure what is the correct way for doing so. Suggestions pls.

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

McBgnr
Jul 6, 2010, 06:02 AM
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.

robbieduncan
Jul 6, 2010, 07:02 AM
So can I have two Views with the same view controller, one for each orientation.

No idea. I've never wanted to do that. As I said I just resize/reposition my existing views as appropriate on orientation change.

adrian.oconnor
Jul 12, 2010, 04:12 AM
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.

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.

McBgnr
Jul 12, 2010, 04:33 AM
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.

PhoneyDeveloper
Jul 12, 2010, 10:16 AM
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.

This will only lead to problems. You should make all your view controllers support rotation.

adrian.oconnor
Jul 15, 2010, 04:29 AM
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.

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:


-(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.