Why won't my UITableView rotate? - iphone

Within Interface Builder, I have the following
UIViewController
-- View
---- TableView
In my UIViewController I have set
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
I also have other view controllers that are in IB. What am I missing here? The only way I can get it to rotate is if I use the transform method.

Are you using the tableview in a tab-based app by any chance? In case you are, you can only get a view to support landscape mode if all the viewcontrollers of the tabbar controller support landscape mode.
Other than that, I don't see any reason why your view should not support landscape mode.

The problem is with the autosizing settings. To be more specific, in Interface Builder, make sure autoresizing is clicked, then in the Size Inspector, make sure the tableview all it's superviews of the table view have red arrows filled in on the top, left, right, and also select the two internal arrows.
I am wondering if the questioner actually had the table view in a subview of the main view. I see this behavior for a table view that is one level deeper in the view hierarchy:
Main View
Subview
Table View
By default, the Main View has it's autosizing arrows set up correctly, but if you add an additional view, it does not.
In response to the question about the problem being with multiple view controllers: Note that a UIViewController is not a UIView's delegate. In fact, it looks like chain of events goes the other way - first the system sees an autorotation and tells the UIViewControllers about it. If the UIViewControllers have shouldAutoresize returning yes, then the UIViewControllers resize their main views. The resizing of the main views can automatically cause their subviews to resize if they are set correctly.
According to Apple docs, you should not have multiple view controllers controlling different parts of a view - for example a separate tableview controller for a table view that is in a subview of the main view - because it mucks with the event chain - you could see how that would be the case here. Don't know if that's helpful or not.

I just tried this, and it works as expected. You will need to provide more detail, I think. In my experience, when a view "fails" to rotate, that's because some view controller somewhere is telling it not to. Check to make sure all your view controllers are returning the right values from shouldAutorotateToInterfaceOrientation:

if your are using storyboard, your VC contains a UITableview, check if your Scene have AutoLayout unchecked.

Related

Rotating view controllers within a hierarchy of Tab Bar Controller -> Navigation Controller -> View Controller

My app has a view controller hierarchy set up like this:
UITabBarController
|
UINavigationController
| |
| UIViewController
|
UINavigationController
|
UIViewController
All of my view controllers that are within this hierarchy override the method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
and return YES - therefore the view controller should be able to rotate to any rotation - even upside down.
However, within this setup none of the view controllers successfully rotate. I was under the impression that navigation and tab bar controllers would rotate if their view controllers respond to rotating.
Why won't my view controllers rotate?
The only way I've been able to get them to rotate is by subclassing UINavigationController and overriding it's shouldAutorotate method, but this feels unnecessary to me and I was wondering if there's something I've missed to make this work.
Edit:
According to the User Experience Coding How-to:
If you are also using a toolbar, the view controller for each toolbar item must implement the shouldAutorotateToInterfaceOrientation: method and return YES for each of the orientations you wish to support. If you have a navigation controller for a toolbar item, the root view controller of that navigation controller must implement the shouldAutorotateToInterfaceOrientation: method and return YES.
It says 'toolbar' - but I think this is a typo and is probably supposed to be 'tab bar'.
So it seems that I'm implementing this correctly, yet my controllers still do not auto rotate.
I've run into this problem, but I can't remember the exact reason it occurred. The tab bar controller requires all of its view controllers to respond YES when asked about a particular orientation for it to rotate to that orientation.
If presented modally, it seems like it doesn't matter about the underlying VC system.
I have created a test to show this (RotationTest on GitHub), but it all seems to be working. Hopefully I can remember why I was failing with this one at some point.
Have you tried subclassing the Tabbarcontroller and setting it as your tabbarcontroller? In there, set
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
to
YES
The only way I've been able to get them to rotate is by subclassing UINavigationController and overriding it's shouldAutorotate method, but this feels unnecessary to me and I was wondering if there's something I've missed to make this work.
I don't know for sure if this is the wrong approach, but I would subclass the UITabBarController, sooner than the UINavigationController. Also, you can try wrapping everything in a subclassed UIViewController that implements the rotation method, but this will create the overhead of an extra view. I once tried to do rotation in an with UINavigationController, but it was not pretty. I suspect that the reason the views only rotate if you subclass the UINavigationController is that the view hierarchy will only pass the rotation if the parent rotates. If the parent doesn't rotate, the child won't. (Imagine an iPhone in a dock. The iPhone only can rotate if the dock rotates. Now, compare the dock to an iPhone case. The case can also rotate, so the iPhone will rotate too.)
It says 'toolbar' - but I think this is a typo and is probably supposed to be 'tab bar'.
I do not think that the HIG has a typo in that regard. The terms may interchangeable.
Generally, a "toolbar" is relevant to the view that contains it, and therefore should rotate with its parent view. A tab bar, however, is the "parent", so to speak, of the view controller on the screen. The view controller should therefore only rotate if the entire app rotates. This concept basically boils down to this: Which view (bar or view controller) is dependent on the other? (The tab bar is persistent, but the views change, or is the toolbar only there if the view is visible.)
Subclass the UITabBarController as well as the UINavigationController. It works as using xCode 4.4.
I have developed an extension that allows you to do just this without subclassing UITabBarController https://github.com/piercifani/TabBarBetterRotation

UIView subview doesn't change orientation

I have a view controller which manages a view.
I'm adding the my view controller subclass as a subview of the window swapping out another view.
I'm running landscape mode on an iPad.
The view apparently doesn't know that its in landscape mode. Its frame is confused.
Is there something I can/should do to tell it that its in landscape, and/or that the orientation has changed. How does this normally happen. Why isn't it happening?
I used to have my view controller within a UITabBarController and it worked fine there.
Override:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return YES;
}
Your ViewController is not getting rotation events because you have not presented the viewController but have added the viewController's view in the view hierarchy.
Your Tab bar controller previously used to take the responsibility to forward the rotation events to the view controller which it manages, that was how it used to work.
I would though suggest that swapping the view out of window is a bad idea. Instead you should have a main viewController which accepts the rotation events and then swap the view within this viewController based on the current orientation. Consider re-desiging before you code further.
My problem was that my storyboard was overriding my existing custom coded app delegate. After I deleted the story board file, and custom generated view controller code, it worked for me.

UITabBarController with portrait / landscape views

I have a subclass of UITabBarController which i am using so that i can rotate to use my app in landscape too.
How would i go about rotating my UI and getting each view controller to use a landscape view xib?
I have always just written apps before where returning YES for shouldAutorotate... handles it automatically for me... this isn't the case here now, as i'm using a custom view.
Thanks.
You don't need to subclass UITabBarController to get the autorotation behavior. Instead what you should do is have ALL the UIViewControllers that appear in your UITabBarController return YES for shouldAutorotateToInterfaceOrientation:. If even one of them does not, the UITabBarController will not autorotate.
As for the custom view, it is associated with a UIViewController, right? If so, then if your custom view implements layoutSubviews using the current view bounds to lay out all the subviews, then it should autorotate correctly as well.

selectively allowing autorotation in TabBar / UINavigation app

I have a UITabBar/UINavigation application and I'm having some trouble allowing autorotation in a given view.
The TabBar allows changing sections, with table view items. When one of the items is tapped, I push a new view which hides the TabBar and which should autorotate. I tried the easy way, which seemed most logical to me: disable autorotate in the rootViewController and allow in the detailViewController, but this didn't work (shouldAutorotateToInterfaceOrientation returns YES, but then willRotateToInterfaceOrientation is never called and view doesn't autorotate). I read that all VCs in a TabBar should return YES to shouldAutorotateToInterfaceOrientation, so I did that, but the result is that now my whole application rotates.
I then subclassed my UINavigationController and set shouldAutorotate to NO, hoping that I could detect when the view that was being shown was in fact a detailView, and then return YES... I can't seem to do that.
Any help out there?
Thanks!
Antonio
It sounds like you've got a set up like the iPod app which has a tabbar for playlist view, songs view etc but which disappears when you go to a detail view for a song. The detail view can rotate but the tabbar views do not. When you do rotate the tabbar it turns into a cover flow detail view.
I'm pretty sure they do this by putting the tabbar inside a navigation controller. When you go to the detail view, it pops the tabbar entirely and pushes the detail view.
So the actual hierarchy looks something like:
Nav {
tabbar {
playlist
Artist
//... other tabs
}
detail view portrait
detail view cover flow
}
Only one of the sibling views (tabbar, detail portrait, detail coverflow) is pushed at any one time.
The iPod app does this because the detail view is the primary functional view for the entire app so the rest of the app is built around navigating to it. If that is not the case for your app, then this setup may be more trouble than it is worth.

iPhone Dev - Autorotating all views

(By the way I develop without Interface Builder)
If you have a tab bar app that autorotates, so all the autoresizing masks are set, how do you make it work with all the views? Like if one view autorotates to landscape, you select a different tab in the tab bar, and the view associated with that tab comes up, and its all messed because it never got autorotated, it got initialized with the frame that makes it fit in portrait mode, even though the autoresizingmasks are set to have it look fine in landscape, it never got rotated. Whats the solution? (By the way, I'm lazy loading the views, so the only view loaded at any given time is the view(view controller's view) associated with the selected tab).
Does declaring all the required orientations in the didrotatefrominterfaceorientation method in every view controller you are using fix the problem?
e.g. your main view, with the tab bar is called "mainView", and when you choose a tab, it loads a view called "firstView", does the "firstView" view controller have the orientations set?
If a view controller is not loaded, it won't be able to respond to autorotation messages. So in your view controller, when it's loaded from the nib, it should check the orientation and resize and move things as necessary.