Tapping TabBar crashes App - tabbar

I have a very strange error on my app. The App has two Tabs. Tab two shows a list of reports. If I tap one of the reports in Tab Two, the app correctly shows the details of the report. I then tap Tab one and immediately tap Tab two. The app crashes in iOS 8 but works fine for iOS 7.
If I returned to the root view controller of Tab two before tapping on Tab one, tapping Tab two again does not crash the app. Any idea what could be causing this error?
In the AppDelegate Class I have the following code:
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
{
UIViewController *currentController = tabBarController.selectedViewController;
if ([currentController isKindOfClass:[UINavigationController class]])
[(UINavigationController *)currentController popToRootViewControllerAnimated:NO];
return YES;
}

I resolved the crashing by putting an if statement on the code as shown below. Still not sure why the code crashes in iOS 8.
- (BOOL)tabBarController:(UITabBarController *)tabBarController
shouldSelectViewController:(UIViewController *)viewController
{
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if (SYSTEM_VERSION_LESS_THAN(#"8.0"))
{
NSLog(#"The iOS is less than 8");
UIViewController *currentController = tabBarController.selectedViewController;
if ([currentController isKindOfClass:[UINavigationController class]])
[(UINavigationController *)currentController popToRootViewControllerAnimated:NO];
return YES;
}
return YES;
}

Related

iOS 8 - Rotation makes statusBar disappear even in portrait mode after toggling controls

I'm having a lot of troubles with the new auto hiding of the status bar in iOS 8.
In my app, I've got a view in which when the user taps once, the navigation bar and the status bar disappear.
When in landscape, the status bar hides by itself and it's fine to me. I only need it in portrait mode.
But the problem is that when the device is landscape and the bar are shown, when the user taps twice to toggle the bar (so showing), and turns the device in portrait mode, the status bar is still hidden.
Basically I need to be able to hide the statusBar without interfere with its natural behavior on iOS 8, so I recap the scenario:
User enters said view with tabBar and NavigationBar and statusBar;
Taps once in the view and the bars disappear
User rotates the device, statusBar not appearing - OK, I want this
User taps again to show the bars - StatusBar still hidden, OK.
User rotates from landscape to portrait and..
statusBar still hidden - NOT OK.
MRW >
(source: mshcdn.com)
I've tried to adjust the statusBar on willRotate, but I made a mess in which the statusBar would be visible when it wasn't supposed to. Code I'm using:
- (BOOL)prefersStatusBarHidden
{
return statusBarHidden;
}
-(void)toggleBars:(UITapGestureRecognizer *)gesture{
CATransition *animation = [CATransition animation];
animation.type = kCATransitionFromBottom;
animation.subtype = kCATransitionFromTop;
animation.duration = .2f;
[animation setTimingFunction:[CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionEaseInEaseOut]];
BOOL toggleNavigationBar = self.navigationController.navigationBarHidden;
[self.navigationController.navigationBar.layer addAnimation:animation forKey:nil];
[self.navigationController setNavigationBarHidden:!toggleNavigationBar animated:YES];
BOOL toggleTabHidden = self.tabBarController.tabBar.hidden;
if(![[[NSUserDefaults standardUserDefaults] objectForKey:#"showTabBar"]isKindOfClass:[NSNull class]]){
if([(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:#"showTabBar"]boolValue])
{
[self.tabBarController.tabBar.layer addAnimation:animation forKey:nil];
[self.tabBarController setHideTabBar:!toggleTabHidden animated:YES];
}
}
statusBarHidden = [UIApplication sharedApplication].statusBarHidden;
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation:UIStatusBarAnimationSlide];
[self setNeedsStatusBarAppearanceUpdate];
if (IS_IOS8){
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){
if (statusBarHidden){
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
}
}
}
I was thinking about setting a flag in which when the statusBar is hidden when in landscape and all the controls are there, on rotation it would trigger the statusBar. With no success, apparently..
Any help highly appreciated.
Are you using UIViewController-based status bar appearance?
If you implement prefersStatusBarHidden I assume you are.
Now,
[[UIApplication sharedApplication] setStatusBarHidden:!statusBarHidden withAnimation: UIStatusBarAnimationSlide];
is not supposed to work with UIViewController-based status bar appearance.
You need just to return different value from prefersStatusBarHidden method and call setNeedsStatusBarAppearanceUpdate to notify the app that returning value has changed.
So to change statusbar visibility you should just do
#property (nonatomic, assign) BOOL hideStatusBar;
- (BOOL)prefersStatusBarHidden
{
return self.hideStatusBar;
}
- (void)toggleBars:(UITapGestureRecognizer *)gesture
{
... hide navbar and tabbar ...
self.hideStatusBar = ! self.hideStatusBar;
[self setNeedsStatusBarAppearanceUpdate];
}
And that's it!
This worked for me:
- (void)viewWillLayoutSubviews {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
This is the swift (3.0) version of that prefersStatusBarHidden
override var prefersStatusBarHidden: Bool{
return false
}
You just need to add it to your ViewController
#pragma mark After and Before Oriantation Change Methods+Delegate
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
#pragma mark nav
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:NO];
}
This is Short and Easy method.

iPad Landscape orientation

I am creating an App in portrait and Landscape Mode for iPad . I create a XIB in Landscape Mode for iPad but when i run that App it always shows in portrait Mode .
I set all setting under property list(.plist) file as "Supported Interface orientation(iPad)" and set Landscape(Left Home button) and landscape(Right Home Button) and also check the Orientation from the Code but all this doesn't work.
please help us if any one knows the exact problem or this , we are using Navigation Controller
Add this line of code in didFinishLaunchingWithOptions in delegate.
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(preferredInterfaceOrientationForPresentation) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
Then add following methods in delegate first and run the app, if fixed, COOOOL else add following four methods in each your view controller. You problem will be fixed.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}

iOS 6 Orientation rotate automatically back to portrait on pressing done on MoviePlayer

I am using MoviePlayer controller to play a video in my iOS app. I am using orientation notification like this
if(deviceOrientation ==UIDeviceOrientationLandscapeLeft)
{
NSLog(#"Replay is in Landscape");
self.fullScreenFlag = YES;
[self.moviePlayer setFullscreen:YES animated:NO];
}
This makes my video screen to play in full screen when user turns the phone to landscape orientation. But when I press done button on moviePlayer control I go into following method
- (void)movieWillExitFullscreen:(NSNotification*)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if(deviceOrientation ==UIDeviceOrientationLandscapeLeft) {
NSLog(#"Pressed Done in Landscape");
//Problem: Here I want to force my VideoViewController to rotate back to portrait Mode
}
}
Not sure how can I make the VC to go back to portrait as soon as user pressed done button or video stops playing. I am aware to the moviePlayerNotificationMethods but what should I call in those method for orientation is not clear.
I solved this issue by having a separate view controller for the video playback.
So, you would have two view controllers
SomeViewController
MoviePlayerViewController
In your SomeViewController, when you want to play the movie:
MoviePlayerViewController *vc = [[MoviePlayerViewController alloc] initWithNibName:#"MoviePlayerViewController" bundle:nil];
[vc setPathToMovie:path];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
And then in your MoviePlayerViewController
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[self navigationController] popViewControllerAnimated:YES];
}
You can then lock down your SomeViewController to portrait, and if the user is in landscape when watching the video, they will return to portrait when popping back to SomeViewController.
I never found a solution using the deviceOrientation method and a modal MPMoviePlayerController. There may be one though!
I solved this by doing this in "moviePlayBackDidFinish"
UIViewController* forcePortrait = [[UIViewController alloc] init];
[self presentViewController:forcePortrait animated:NO completion:^{
[forcePortrait dismissViewControllerAnimated:NO completion:nil];
}];
It's not beautiful but it works like a charm :-)
Depending upon whether you using MPMoviePlayerController within a ViewController or as a separate ViewController the answer is as follows :-
Firstly :- This link will explain you how to restrict some views to portrait and allow others to rotate?
In that link you will see that, in the NavigationViewController you have made, following changes:-
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
What it does is, it give the child to make their own decision if they want to auto-rotate or not.
Next the ViewController containing your MoviePlayer should do this :-
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
Once you have done this, it gives the power of AutoRotation to your ViewController.
Now here's the tricky part, see I assume that you might have restricted your ViewController to Portrait, and since movie player allows you go fullscreen and in fullscreen when you rotate your screen it will turn to landscape, and now if you press done button it won't turn to portrait rather it will exit the fullscreen in landscape itself. In this case what you should do is, in your:-
- (BOOL)shouldAutorotate
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
if ([[[self.navigationViewController.viewControllers] lastObject] class] == [MoviePlayerViewController class] ) {
return YES;
}
return NO;
}
return NO;
}
So, what it does is, you should auto-rotate only when the orientation is landscape and not when its portrait.
So far so good, next comes the MoviePlayer, considering that you have already played the Video and your only interest is when we click "Done" button it should auto-rotate to portrait.
Register for a notification to your MoviePlayer
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerWillExitFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:_moviePlayer];
Then in the selector:
- (void) moviePlayerWillExitFullScreen:(NSNotification*)notification{
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerWillExitFullscreenNotification object:_moviePlayer];
}
Tada! the magic is done! try out let me know ;-)

iOS 6 Orientation with Navigation Controller

I have an app that works fine on ios 5,im trying to upgrade my app to work on ios 6, i had read tons of questions and tutorials about using ios 6 orientation,
my problem is when i call my rootViewController its work fine with me, but when i push any viewController the orientation look so bad because i use the orientation to change the view sizes (my app support any orientation)
here is my code:
AppDelegate:
UINavigationController *nav =[ [UINavigationController alloc] initWithRootViewController:theView] ;
self.window.rootViewController = nav;
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window // iOS 6
{
return UIInterfaceOrientationMaskAll;
}
myFirstViewController:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll;
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
[self viewWillAppear:NO];
}
-(BOOL)shouldAutorotate{
return YES;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:NO];
if ( [[UIDevice currentDevice].systemVersion floatValue] >= 6.0){
if (pointRange.location != NSNotFound) {
UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if( (interfaceOrientation >= 3) ) {
width=1024;
self.view.frame=CGRectMake(0, 0, 1024, 768);
}
if ( (interfaceOrientation == 1) || (interfaceOrientation == 2 )) {
width=768;
self.view.frame=CGRectMake(0, 0, 768, 1024);
}}
....etc
and i did the same in my second view, Hope to find why!!
You could always make an extension to the UINavigationController like this
#implementation UINavigationController (RotationFix)
-(NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
-(BOOL)shouldAutorotate
{
return YES;
}
#end
even i suffered for 2 days.. gone through many tutorials, blogs, forums, even apple docs.
So far i came to know, In iOS 6 each template of the app should be handled in a different ways.
So far the discussions were only for view based app, and these changes were not working on navigation based app or tabBar based app.
Finally i got the solution, it like this
Implement a subclass of these two types UITabBarController or UINavigationController.
Got to know from this blog. Thanks to Shabbir for the solution.

viewWillAppear not called in BCTabBarController

I've large project where customer want's to customize tabbar. I've choose BCTabBarController to replace UITabbarController. After few fixes it works fine but after testing I found one bug:
ViewWillAppear, ViewDidAppear, ViewWillDisappear ViewDidDisappear methods not called in selectded view controller and not called into BCTabBarController.
This problem appears after BCTabBarController show modal controller from instance of BCTabBarController class.
I've posted issue to github repo of briancolins, but still have no answer.
Here some code where I calling present modal view controller:
- (void) presentProperlyModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
{
if ([[self controllerToPresentModalFrom] respondsToSelector:#selector(presentViewController:animated:completion:)]) // For iOS 5
{
[[self controllerToPresentModalFrom] presentViewController:modalViewController animated:animated completion:^(){}];
}
else
{
[[self controllerToPresentModalFrom] presentModalViewController:modalViewController animated:animated];
}
}
-(void) dismissProperlyModalViewControllerAnimated:(BOOL)animated
{
if ([self respondsToSelector:#selector(dismissViewControllerAnimated:completion:)]) {
[self dismissViewControllerAnimated:animated completion:^(){}];
}
else
{
[self dismissModalViewControllerAnimated:YES];
}
}
UPDATE: this issue not reproduced in iOS5 but present at iOS 4.3
As you indicated. iOS 5 forwards the messages, where previous versions do not. Here's how I handle a similar situation:
- (BOOL)needsMessageForwarding:(UIViewController *)vc {
if ( [vc isKindOfClass:[UINavigationController class]] == NO)
return YES;
NSString *ver = [UIDevice currentDevice].systemVersion;
if ( [ver characterAtIndex:0 < '5'] )
return YES;
return NO;
}
- (void) viewWillAppear:(BOOL)animated {
...
if ( [self needsMessageForwarding:modalViewController] )
[modalViewController viewWillAppear:animated];
...
}
// repeat pattern in the other viewWill... viewDid... functions.
In my situation I have a list of view controllers that could be visible, so I manage which view controller is visible and forward the message to it.