UINavigationbar loses set image on rotation - objective-c

I have an app which starts with a root UINavigationController. This controller then pushes and pops various other UIViewControllers - that all works fine.
My app has a custom graphic for the navigationController.navigationBar.backgroundImage. There is one image for the iPhone platform. There are two images for the iPad platform - one for portrait, one for landscape. The iPad is the platform of issue as it rotates, the iPhone platform is portrait only.
I had already written code in -(void)willRotateToInterfaceOrientation: to detect the rotation, and set the navigationBar.backgroundImage to the correct orientation graphic, if the platform is iPad.
In the iPad 5.0 simulator - it works fine; rotating the iPad on screen results in the correct navigationBar graphic regardless of orientation.
On the device - it doesn't work - The graphic appears correctly on iPad device launch in portrait. When I rotate to landscape, the navigationBar changes to a standard grey one. When I rotate back the grey 'sticks'.
I have tried calling setNeedsDisplay - no change.
I have tried setting navigationBar.tintColor = [UIColor clearColor] - no change.
I have checked the filenames of the graphics in the code are identical to the actual filenames - they are.
Code for rotation:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES; }
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
UIImage *titleImage = [UIImage imageNamed:#"StandardHeaderIpadLandscape"];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"StandardBackgroundiPadLandscape.png"]];
[self.navigationController.navigationBar setNeedsDisplay];
} else {
UIImage *titleImage = [UIImage imageNamed:#"StandardHeaderIpadPortrait"];
self.navigationController.navigationBar.tintColor = [UIColor clearColor];
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics:UIBarMetricsDefault];
self.navigationController.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"StandardBackgroundiPad.png"]];
[self.navigationController.navigationBar setNeedsDisplay];
}
}
}

Check whether titleImage is nil. I have a feeling that your image paths are not correct, or those images are not correctly copied to iPad.

Related

Statusbar overlapping on UIImagepickercontroller's Camera View in iOS 7 and iOS 8

im using UIImagepickercontroller to take photo and choose existing photos, when the camera view appears, the top portion on camera was covered by the white status bar. How can i remove the status bar or hide the status bar. Many of them saying its a ios 7 bug, it has been fixed in ios 7.1, but still i'm facing this problem.
This is my code to show the imagepickerController
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = (id)self;
imagePicker.sourceType = (buttonIndex == actionSheet.firstOtherButtonIndex ?
UIImagePickerControllerSourceTypeCamera : UIImagePickerControllerSourceTypePhotoLibrary);
[self.viewController presentViewController:imagePicker animated:YES completion:nil];
Also i tried by with below codes to hide statusbar
- (void)navigationController:(UINavigationController *)navigationController
willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
}
This
-(BOOL) prefersStatusBarHidden {
return YES;
}
And This in Delegate
[UIApplication ShareApplication]SetStatusbarHidden:YES];
Right now i'm working with ios8, and this behaviour happening in all ios version.
Please help me out.
Actually the problem is in my rootviewcontroller i'm creating a Statusbar using UIView and setting its constraints for landscape and portrait programmatically
- (void)setTopBar {
if(topBar == nil && [[UIApplication sharedApplication] keyWindow] != nil) {
topBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
topBar.backgroundColor = navController.view.backgroundColor;
topBar.tag = topBarViewTag;
UIView *containerView = [[UIApplication sharedApplication] keyWindow];
[containerView addSubview:topBar];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"V:|[view(20)]" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:#"|[view]|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:#{#"view" : topBar}]];
[containerView layoutSubviews];
}
}
so when i commented the above method in ViewwillAppear, status bar where not showing up, so the problem is with my code.
if anyone face the same issue, i suggest you to have a look on your RootViewcontroller, which acts a navigation to all other classes.

How to change status bar colours separately for each ViewController?

I'm developing an app using iOS 7 and storyboards. I have several view controllers. What I want to do is change the status bar color according to the Navigation bar color. But each viewcontroller has a different color navigation bar. I know I can change the status bar colour by adding a flag to info.plist and code like this:
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.window.clipsToBounds = YES;
[[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleBlackOpaque];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
self.window.frame = CGRectMake(20, 0,self.window.frame.size.width-20,self.window.frame.size.height);
self.window.bounds = CGRectMake(20, 0, self.window.frame.size.width, self.window.frame.size.height);
} else
{
self.window.frame = CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);
}
}
But how do I change the color for each ViewController?
Add this method to each viewCOntroller
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault; // or UIStatusBarStyleLightContent
}
In viewDidLoad for each view controller:
[self preferredStatusBarStyle];
[self performSelector:#selector(setNeedsStatusBarAppearanceUpdate)];
Also in project properties you need to set to YES this option: View controller-based status bar appearance

Subclass UINavigationbar for customise in iOS7 does not work

I have made a subclass of the navigation bar.
In there I set a background image and I would like to disable the clipping, because my Image has some shadow and an arrow which is higher than the navigation bar.
in iOS 6&5 it works perfectly when I remove the tick Clip Subviews in the Interface Builder in Xcode.
In iOS7 unfortunately the background image is always clipped...
I also tried to add the following lines:
- (void) awakeFromNib
{
// Initialization code
UIImage *image = [UIImage imageNamed:#"tt_navigation_bar.png"];
if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) image = [UIImage imageNamed:#"tt_navigation_bar-ios7.png"];
[[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
NSDictionary *textTitleOptions = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:#"HelveticaNeue-Bold" size:21.0], UITextAttributeFont, [UIColor colorWithRed:255.0/255.0 green:109.0/255.0 blue:36.0/255.0 alpha:1], UITextAttributeTextColor, [UIColor clearColor], UITextAttributeTextShadowColor, nil];
[[UINavigationBar appearance] setTitleTextAttributes:textTitleOptions];
for (UIView *v in [self subviews]) {
NSLog(#"v: %#", v);
v.layer.masksToBounds = NO;
}
self.layer.masksToBounds = NO;
}
I solved this by manually editing my two images into the top n pixels and then "the rest" and setting the image of "the rest" to the shadowImage.

Navigation bar has wrong position when modal a view controller with flip horizontal transition in iOS 7

When I am trying to modal a view controller with flip horizontal transition in iOS7, the origin of navigation bar is (0, 0) at beginning and then jump to the right position at (0, 20). Is it possible to make it behave the same with in iOS6? You can download the project here.
I have created a customized navigation bar as following:
#implementation MyCustomNavigationBar
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
UIImage *image = [UIImage imageNamed:#"Custom-Nav-Bar-BG.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
if (IOSVersion <7) {
}else{
self.translucent = NO;
self.tintColor = [UIColor whiteColor];
self.barStyle = UIBarStyleDefault;
self.barTintColor = [UIColor redColor];
}
}
#end
Any help will be appreciated.
I have the same problem with you. I think It's a bug with UIKit on iOS 7.
I added a little of code at viewWillAppear method on presentation
[self.navigationController.navigationBar.layer removeAllAnimations];
When I dismiss this view, I added:
-(IBAction)btnDonePressed:(id)sender {
[UIView transitionWithView:self.navigationController.view
duration:0.75
options:UIViewAnimationOptionTransitionFlipFromLeft
animations:nil
completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
}
This worked for me. Hope this helps you.
Instead of hacking [navigationBar.layer removeAllAnimations];, something like this should work as well:
[UIView transitionWithView:self.view.window duration:FlipDuration options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[UIView performWithoutAnimation:^{
[self presentViewController:modalController animated:NO completion:nil];
}];
} completion:nil];
Just give the -20 in the xib in option "ios 6/7 Deltas"
or read this
https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/index.html#//apple_ref/doc/uid/TP40006556-CH66-SW1

iPad Black Screen on Interface Rotation

I have a universal application created in storyboard mode. It is set-up to automatically rotate to the current orientation. When the iPad is rotated, it should go through the following code BUT all I get is a black screen.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
self.view = landscape;
self.view.transform = CGAffineTransformMakeRotation(deg2rad*(90));
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
} else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
self.view = landscape;
self.view.transform = CGAffineTransformMakeRotation(deg2rad*(-90));
self.view.bounds = CGRectMake(0.0, 0.0, 1024.0, 748.0);
} else {
self.view = portrait;
self.view.transform = CGAffineTransformMakeRotation(0);
self.view.bounds = CGRectMake(0.0, 0.0, 768.0, 1004.0);
}
} else {
}
}
Why are all these problems arising here? What is going on and how can I fix this?
EDIT: Debug Console Result of: po [landscape recursiveDescription]
I end up with this response: Can't print the description of a Nil object
EDIT: Black Screen still appears even though the UIViews are linked to their outlets.
Your landscape variable is nil. That's why you are getting a black screen. You need to set it to a UIView.