Get UINavigationBar landscape height while device is in portrait - ios

I have a simple question: I want to find out the height of a UINavigationBar in landscape while my device is in portrait. Is this possible and if so, how?
Some background:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
[navBar sizeToFit];
NSLog(#"navBar: %#", NSStringFromCGRect(navBar.frame));
This returns the correct height for the current device orientation, for example 44. That means that UINavigationBar's sizeToFit method must somehow look at the current device orientation. Is there any way to get find out what the height would be in landscape without going to landscape?

Why not grab and set that information in willAnimateRotationToInterfaceOrientation. By overriding this method you should be able to get the info you desire prior to the view being shown.
In other words, don't worry about it until you are actually in the process of changing your orientation.
willAnimateRotationToInterfaceOrientation

OK, this is one possible solution that works:
#interface TestVC : UIViewController
#property (assign, nonatomic) UIInterfaceOrientationMask orientationMask;
#end
#implementation TestVC
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return _orientationMask;
}
#end
#implementation ViewController
- (IBAction)getNavigationBarHeight:(id)sender {
TestVC *vc = [[TestVC alloc] init];
if (UIInterfaceOrientationIsPortrait(self.interfaceOrientation)) {
vc.orientationMask = UIInterfaceOrientationMaskLandscapeLeft;
} else {
vc.orientationMask = UIInterfaceOrientationMaskPortrait;
}
[self presentViewController:vc animated:NO completion:^{
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectZero];
[navBar sizeToFit];
NSLog(#"navBar frame in 'opposite' orientation: %#", NSStringFromCGRect(navBar.frame));
}];
[self dismissViewControllerAnimated:NO completion:nil];
}
#end

Related

UIVisualEffectView in iOS 10

I am presenting a UIViewController that contains a UIVisualEffectView as follows:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:#"segueBlur" sender:nil];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:#"segueBlur"]) {
((UIViewController *)segue.destinationViewController).providesPresentationContextTransitionStyle = YES;
((UIViewController *)segue.destinationViewController).definesPresentationContext = YES;
((UIViewController *)segue.destinationViewController).modalPresentationStyle = UIModalPresentationOverFullScreen;
}
}
As you can see, I'm using the UIModalPresentationStyleOverFullScreen so that when the view controller with blur appears, the blur will be 'applied' to the content of the view controller that is presenting it; the segue has a Cross Dissolve transition style.
The effect looks as expected. However, in iOS 9 the presentation is smoother than in iOS 10. In iOS 10 when the view controller appears it seems like a 2-step animation, while in iOS 9 the blur is applied immediately.
A picture is worth a thousand words so I uploaded a video showing this strange behavior:
UIVisualEffectView iOS 9 vs iOS 10
My question is: How can I present the view controller in iOS 10 as it is presented in iOS 9?
iOS 10 has changed the way UIVisualEffectView works, and it has broken many use cases which were not strictly speaking "legal", but worked before. Sticking to documentation, you should not be fading in UIVisualEffectView, which is what happens when you use UIModalTransitionStyleCrossDissolve. It seems now broken on iOS 10, along with masking of visual effect views, and others.
In your case, I would suggest an easy fix which will also create a better effect than before, and is supported on both iOS 9 and 10. Create a custom presentation and instead of fading the view in, animate the effect property from nil to the blur effect. You can fade in the rest of your view hierarchy if needed. This will neatly animate the blur radius similar to how it looks when you pull the home screen icons down.
UIView.animate(withDuration: 0.5) {
self.effectView.effect = UIBlurEffect(style: .light)
}
Tested on both iOS9 and 10. Works fine for me
Code below blur parent view controller when ViewController presented. Tested on both iOS9 and 10.
#interface ViewController () <UIViewControllerTransitioningDelegate, UIViewControllerAnimatedTransitioning>
#property (nonatomic) UIVisualEffectView *blurView;
#end
#implementation ViewController
- (instancetype)init
{
self = [super init];
if (!self)
return nil;
self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.transitioningDelegate = self;
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.blurView = [UIVisualEffectView new];
[self.view addSubview:self.blurView];
self.blurView.frame = self.view.bounds;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
presentingController:(UIViewController *)presenting
sourceController:(UIViewController *)source
{
return self;
}
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
{
return 0.3;
}
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
UIView *container = [transitionContext containerView];
[container addSubview:self.view];
self.blurView.effect = nil;
[UIView animateWithDuration:0.3 animations:^{
self.blurView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
} completion:^(BOOL finished) {
[transitionContext completeTransition:finished];
}];
}
#end

Modal transition style like in Mail app

I am trying to achieve a modal presentation effect where the presented view covers the parent view only partially as shown in the picture below.
I know I could achieve this by implementing custom transitions using UIPresentationController. I don't want to reinvent the wheel so before I roll on with development I would like to ask.
Is there a build in support for this kind of transition in the APIs?
I researched all available Modal Presentation Styles and it appears to me there is no support for the transition I want to make and the only way of achieving it is just to code it.
I ran into this exact same issue. I went down the modal presentation styles route as well and kept hitting a wall (specifically getting it working on an iPhone rather than an iPad).
After some digging around, I was able to get it working though. Here's how I did it:
To start, we need a view controller that we will be presenting (the modal one) to set it's view's background color to transparent and set the frame of the navigation controller's view to some offset.
ModalViewController.h
#import UIKit;
#class ModalViewController;
#protocol ModalViewControllerDelegate <NSObject>
- (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController;
#end
#interface ModalViewController : UIViewController
#property (weak, nonatomic) id<ModalViewControllerDelegate> delegate;
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
#end
ModalViewController.m
static const CGFloat kTopOffset = 50.0f;
#implementation ModalViewController {
UINavigationController *_navController;
}
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
rootViewController.navigationItem.leftBarButtonItem = [self cancelButton];
_navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.view.backgroundColor = [UIColor clearColor];
[self.view addSubview:_navController.view];
// this is important (prevents black overlay)
self.modalPresentationStyle = UIModalPresentationOverFullScreen;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect bounds = self.view.bounds;
_navController.view.frame = CGRectMake(0, kTopOffset, CGRectGetWidth(bounds), CGRectGetHeight(bounds) - kTopOffset);
}
- (UIBarButtonItem *)cancelButton
{
return [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelButtonClicked:)];
}
- (void)cancelButtonClicked:(id)sender
{
[_delegate modalViewControllerDidCancel:self];
}
#end
Next, we need to set up the presenting controller to run the following animation:
Scale itself down
Fade out a lil' bit
Present the modal view controller using presentViewController:animated:completion
This is what I did
PresentingViewController.m
static const CGFloat kTransitionScale = 0.9f;
static const CGFloat kTransitionAlpha = 0.6f;
static const NSTimeInterval kTransitionDuration = 0.5;
#interface PresentingViewController <ModalViewControllerDelegate>
#end
#implementation PresentingViewController
...
...
- (void)showModalViewController
{
self.navigationController.view.layer.shouldRasterize = YES;
self.navigationController.view.layer.rasterizationScale = [UIScreen mainScreen].scale;
UIViewController *controller = // init some view controller
ModalViewController *container = [[ModalViewController alloc] initWithRootViewController:controller];
container.delegate = self;
__weak UIViewController *weakSelf = self;
[UIView animateWithDuration:kTransitionDuration animations:^{
weakSelf.navigationController.view.transform = CGAffineTransformMakeScale(kTransitionScale, kTransitionScale);
weakSelf.navigationController.view.alpha = kTransitionAlpha;
[weakSelf presentViewController:container animated:YES completion:nil];
} completion:^(BOOL finished) {
weakSelf.navigationController.view.layer.shouldRasterize = NO;
}];
}
#pragma mark - ModalViewControllerDelegate
- (void)modalViewControllerDidCancel:(ModalViewController *)modalViewController
{
__weak UIViewController *weakSelf = self;
[UIView animateWithDuration:kTransitionDuration animations:^{
weakSelf.navigationController.view.alpha = 1;
weakSelf.navigationController.view.transform = CGAffineTransformIdentity;
[weakSelf dismissViewControllerAnimated:YES completion:nil];
}];
}
#end
pretty sure its done like this
let newVC = <view controller you want to display>
let nav: UINavigationController = UINavigationController(rootViewController: newVC)
if let currVc = UIApplication.sharedApplication().keyWindow?.rootViewController {
nav.transitioningDelegate = currVc
nav.modalPresentationStyle = UIModalPresentationStyle.Custom;
currVc.presentViewController(nav, animated: true, completion: nil)
}
I'm pretty sure this is your answer - Page sheet - as in UIModalPresentationPageSheet
https://developer.apple.com/library/ios/documentation/userexperience/conceptual/mobilehig/Alerts.html#//apple_ref/doc/uid/TP40006556-CH14-SW3

iOS 7+ Dismiss Modal View Controller and Force Portrait Orientation

I have a UINavigationController as the root view controller of my UIWindow on iOS 7 and iOS 8. From one of its view controllers, I present a fullscreen modal view controller with a cross-dissolve presentation style. This modal view controller should be able to rotate to all orientations, and it works fine.
The problem is when the device is held in a landscape orientation and the modal view controller is dismissed. The view controller which presented the modal only supports portrait orientation, and I've confirmed that UIInterfaceOrientationMaskPortrait is returned to -application:supportedInterfaceOrientationsForWindow:. -shouldAutorotate returns YES, as well. However, the orientation of the presenting view controller, after dismissing the modal, remains landscape. How can I force it to remain in portrait orientation while allowing the modal to take the orientation of the device? My code follows:
App delegate:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UINavigationController *navigationController = (UINavigationController *)self.deckController.centerController;
NSArray *viewControllers = [navigationController viewControllers];
UIViewController *top = [viewControllers lastObject];
if (top && [top presentedViewController]) {
UIViewController *presented = [top presentedViewController];
if ([presented respondsToSelector:#selector(isDismissing)] && ![(id)presented isDismissing]) {
top = presented;
}
}
return [top supportedInterfaceOrientations];
}
return (UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskLandscapeRight);
}
Presenting view controller:
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
Modal view controller:
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskLandscape|UIInterfaceOrientationMaskLandscapeLeft|UIInterfaceOrientationMaskPortrait);
}
If the modal controller was in landscape orientation before dismissal, the presenting ViewController may not return to the origin orientation (portrait). The problem is because the AppDelegate supportedInterfaceOrientationsForWindow method is called before the controller is actually dismissed and the presented controller check still returns Landscape mask.
Set a flag to indicate whether the (modal) presented view controller will be displayed or not.
- (void)awakeFromNib // or where you instantiate your ViewController from
{
[super awakeFromNib];
self.presented = YES;
}
- (IBAction)exitAction:(id)sender // where you dismiss the modal
{
self.presented = NO;
[self dismissViewControllerAnimated:NO completion:nil];
}
And in the modal presented ViewController set the orientation according to the flag: When the modal ViewController is presented - return Landscape. When it is dismissed then return portrait
- (NSUInteger)supportedInterfaceOrientations
{
if ([self isPresented]) {
return UIInterfaceOrientationMaskLandscape;
} else {
return UIInterfaceOrientationMaskPortrait;
}
}
Last step - from your AppDelegate call the modal presented ViewController for its orientation. I am just checking the currently presented ViewController and call the supportedInterfaceOrientations on it
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
NSUInteger orientationMask = UIInterfaceOrientationMaskPortrait;
UIViewController *currentVC = self.window.rootViewController.presentedViewController; // gets the presented VC
orientationMask = [currentVC supportedInterfaceOrientations];
return orientationMask;
}
For more info check this link
This solution is for iOS 8+.
Problem description
Application key window have UINavigationController's subclass as its rootViewController.
This NC subclass prohibits some of the interface orientations.
Some View Controller (VC1) in the NC stack is presenting another View Controller (VC2) modally and fullscreen.
This presented VC2 allows more interface orientations than NC do.
User rotates device to orientation that is prohibited by NC, but allowed by presented VC2.
User dismisses the presented VC2.
View of VC1 has incorrect frame.
Setup and illustration
UINavigationController's subclass:
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate
{
return YES;
}
VC1 initial appearance and UI view stack:
Presenting VC2 (QLPreviewController in that example) from VC1:
QLPreviewController *pc = [[QLPreviewController alloc] init];
pc.dataSource = self;
pc.delegate = self;
pc.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:pc animated:YES completion:nil];
VC2 is presented and device rotated to landscape:
VC2 dismissed, device is back in portrait mode, but NC stack remains in landscape:
Cause
Apple documentation states:
When you present a view controller using the presentViewController:animated:completion: method, UIKit always manages the presentation process. Part of that process involves creating the presentation controller that is appropriate for the given presentation style.
Apparently there is a bug in handling UINavigationController stack.
Solution
This bug can be bypassed by providing our own transitioning delegate.
BTTransitioningDelegate.h
#import <UIKit/UIKit.h>
#interface BTTransitioningDelegate : NSObject <UIViewControllerTransitioningDelegate>
#end
BTTransitioningDelegate.m
#import "BTTransitioningDelegate.h"
static NSTimeInterval kDuration = 0.5;
// This class handles presentation phase.
#interface BTPresentedAC : NSObject <UIViewControllerAnimatedTransitioning>
#end
#implementation BTPresentedAC
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return kDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)context
{
// presented VC
UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];
// presented controller ought to be fullscreen
CGRect frame = [[[UIApplication sharedApplication] keyWindow] bounds];
// we will slide view of the presended VC from the bottom of the screen,
// so here we set the initial frame
toVC.view.frame = CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, frame.size.width, frame.size.height);
// [context containerView] acts as the superview for the views involved in the transition
[[context containerView] addSubview:toVC.view];
UIViewAnimationOptions options = (UIViewAnimationOptionCurveEaseOut);
[UIView animateWithDuration:kDuration delay:0 options:options animations:^{
// slide view to position
toVC.view.frame = frame;
} completion:^(BOOL finished) {
// required to notify the system that the transition animation is done
[context completeTransition:finished];
}];
}
#end
// This class handles dismission phase.
#interface BTDismissedAC : NSObject <UIViewControllerAnimatedTransitioning>
#end
#implementation BTDismissedAC
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
{
return kDuration;
}
- (void)animateTransition:(id<UIViewControllerContextTransitioning>)context
{
// presented VC
UIViewController *fromVC = [context viewControllerForKey:UITransitionContextFromViewControllerKey];
// presenting VC
UIViewController *toVC = [context viewControllerForKey:UITransitionContextToViewControllerKey];
// inserting presenting VC's view under presented VC's view
toVC.view.frame = [[[UIApplication sharedApplication] keyWindow] bounds];
[[context containerView] insertSubview:toVC.view belowSubview:fromVC.view];
// current frame and transform of presented VC
CGRect frame = fromVC.view.frame;
CGAffineTransform transform = fromVC.view.transform;
// determine current presented VC's view rotation and assemble
// target frame to provide naturally-looking dismissal animation
if (transform.b == -1) {
// -pi/2
frame = CGRectMake(frame.origin.x + frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
} else if (transform.b == 1) {
// pi/2
frame = CGRectMake(frame.origin.x - frame.size.width, frame.origin.y, frame.size.width, frame.size.height);
} else if (transform.a == -1) {
// pi
frame = CGRectMake(frame.origin.x, frame.origin.y - frame.size.height, frame.size.width, frame.size.height);
} else {
// 0
frame = CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, frame.size.width, frame.size.height);
}
UIViewAnimationOptions options = (UIViewAnimationOptionCurveEaseOut);
[UIView animateWithDuration:kDuration delay:0 options:options animations:^{
// slide view off-screen
fromVC.view.frame = frame;
} completion:^(BOOL finished) {
// required to notify the system that the transition animation is done
[context completeTransition:finished];
}];
}
#end
#implementation BTTransitioningDelegate
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
return [[BTPresentedAC alloc] init];
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
return [[BTDismissedAC alloc] init];
}
#end
Import that transitioning delegate in presenting VC:
#import "BTTransitioningDelegate.h"
Store a strong reference to an instance:
#property (nonatomic, strong) BTTransitioningDelegate *transitioningDelegate;
Instantiate in -viewDidLoad:
self.transitioningDelegate = [[BTTransitioningDelegate alloc] init];
Call when appropriate:
QLPreviewController *pc = [[QLPreviewController alloc] init];
pc.dataSource = self;
pc.delegate = self;
pc.transitioningDelegate = self.transitioningDelegate;
pc.modalPresentationStyle = UIModalPresentationFullScreen;
[self.navigationController presentViewController:pc animated:YES completion:nil];
I ended up subclassing the UINavigationController and overriding its rotation methods. The following solution works on iOS 7, but I believe there is a bug in iOS 8 beta 5 that causes the presenting view controller's view to shrink to half the screen-height after dismissing the modal in landscape orientation.
UINavigationController subclass:
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}

Present UIViewController in Landscape mode in Portrait only app

My app is a portrait only. I need to present a UIViewController in Landscape mode(Reason for that is I am using Core-Plot sdk for drawing graphs on that viewcontroller, so it needs to be in landscape mode).
I tried the following methods and it work fine. But the issue is, when I dismiss the landscape viewcontroller, app cannot force to portrait mode.
http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/
http://b2cloud.com.au/tutorial/forcing-uiviewcontroller-orientation/
How can I force the app to become portrait only mode after dismiss the landscape viewcontroller?
This is How I presenting the landscape view controller and dismissing it.
LineChartViewController *obj = [[LineChartViewController alloc]initWithNibName:#"LineChartViewController" bundle:nil];
[self.navigationController presentViewController:obj animated:YES completion:nil];
- (IBAction)btnDonePressed:(id)sender{
[self dismissViewControllerAnimated:NO completion:nil];
}
XIB of the LineChartViewController is in landscape mode.
In simple words, My app is portrait only, and I wanted to show CorePlot hostView in landscape mode.
Actually I could solve the issue by rotating the CorePlot hostView. The solution isn't the perfect for the the question described, but I'd like to put my solution here, since it solved my problem
self.hostView = [(CPTGraphHostingView *) [CPTGraphHostingView alloc] initWithFrame:self.viewGraphBack.bounds];
self.hostView.allowPinchScaling = YES;
[self.viewGraphBack addSubview:self.hostView];
CGAffineTransform transform =
CGAffineTransformMakeRotation(DegreesToRadians(90));
viewGraphBack.transform = transform;
viewGraphBack.frame = CGRectMake(-285, 0, 568, 320);
[self.view addSubview:viewGraphBack];
This kind of workaround works for me (temporary pushing fake model view controller), called after other view controller which introduces new orientation is demised:
- (void)doAWorkaroudnOnUnwantedRotation {
// check if is workaround nesesery:
if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
double delayInSeconds = 0.7;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
UIViewController *fake = [[[UIViewController alloc] init] autorelease];
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootController presentModalViewController: fake animated: NO];
[fake dismissModalViewControllerAnimated: NO];
});
}
}
If Parent viewcontroller in UIInterfaceOrientationPortrait mode & current viewcontroller should be in UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationLandscapeRight in that case you may use device orientation changing delegate & a few codes in viewdidload
In ViewDidLoad
NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];
[[UIDevice currentDevice] setValue:value forKey:#"orientation"];
& implement shouldAutorotateToInterfaceOrientation delegate in ViewController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) return YES;
return NO;
}
Like this. Assure that landscape mode must be checked in from target settings.
Write this category in your project
#import "UINavigationController+ZCNavigationController.h"
#implementation UINavigationController (ZCNavigationController)
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
}
#end
And in your viewcontroller where you need Landscape
- (BOOL)shouldAutorotate {
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}

Objective-C - Rotation inside modal even shouldAutoRotate = false

I have a view (InfoVC) that is rotating even if I set shouldAutoRotate to false.
This is the code that is opening the view (inside a Modal)
- (IBAction)presentInfoVC:(id)sender{
InfoVC *infoVC = [[InfoVC alloc] init];
UINavigationController *infoNVC = [[UINavigationController alloc] initWithRootViewController:infoVC];
UIImage *img =[UIImage imageNamed:#"image.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
infoNVC.navigationBar.tintColor = [UIColor lightGrayColor];
[infoNVC.navigationBar.topItem setTitleView:imgView];
[imgView release];
[self presentModalViewController:infoNVC animated:YES];
[infoVC release];
}
and the code that was supposed to avoid this view to rotate (inside InfoVC.m):
- (BOOL)shouldAutorotate
{
return FALSE;
}
What is wrong?
Regards!
Instead of creating a subclass of UINavigationController, you could use a category to perform the same task (if it's required for all instances of UINavigationController). It's a lot more lightweight than the subclassing method, and doesn't require you to swap class types for pre-existing UINavigationControllers.
To do so is as follows:
UINavigationController+NoRotate.h
#interface UINavigationController(NoRotate)
- (BOOL)shouldAutorotate;
#end
UINavigationController_NoRotate.m
#import "UINavigationController+NoRotate.h"
#implementation UINavigationController (NoRotate)
- (BOOL)shouldAutorotate
{
return NO;
}
#end
From then on, if you need a UINavigationController to no longer rotate, simply import UINavigationController+NoRotate.h where required. As category overrides will affect all instances of the class, if you need this behaviour for only a few cases, then you will need to subclass UINavigationController, and override -(BOOL)shouldAutorotate.
I got the answer. I figured out that I was supposed to implement shoulAutorotate in the UINavigationController, not in UIViewController. I created another class (subclass of UINavigationController), implemented shouldAutorotate like in this view and I used it replacing UINavigationController.
Code:
UINavigationControllerNotRotate.h
#import <UIKit/UIKit.h>
#interface UINavigationControllerNotRotate : UINavigationController
#end
UINavigationControllerNotRotate.m
#import "UINavigationControllerNotRotate.h"
#interface UINavigationControllerNotRotate ()
#end
#implementation UINavigationControllerNotRotate
- (BOOL)shouldAutorotate
{
return FALSE;
}
#end
New code:
- (IBAction)presentInfoVC:(id)sender{
InfoVC *infoVC = [[InfoVC alloc] init];
UINavigationControllerNotRotate *infoNVC = [[UINavigationControllerNotRotate alloc] initWithRootViewController:infoVC];
UIImage *img =[UIImage imageNamed:#"logo_vejasp_topbar.png"];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
infoNVC.navigationBar.tintColor = [UIColor lightGrayColor];
[infoNVC.navigationBar.topItem setTitleView:imgView];
[imgView release];
[self presentModalViewController:infoNVC animated:YES];
[infoVC release];
}
This worked fine for me. Thanks for everybody who tried to help!

Resources