Presentviewcontroller/presentmodelviewcontroller for portrait only - ios

I present my second VC like this.
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setViewControllers:#[vc] animated:NO];
[self presentViewController:nav animated:YES completion:nil];
In that second VC, I write this and that method is also being called.
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
However, when I rotate my phone, it change to landscape. May I know how to stop that? In my other views, I need to support for landscape but not for this view. How shall I do?

Related

Support all orientation only for video player view controller in a portrait application

I have implemented this code in AppDelegate.m
-(UIInterfaceOrientationMask) application:(UIApplication *)application supportedInterfaceOrientationsForWindow :(UIWindow *)window
{
UIViewController *currentVC = [(UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController topViewController];
if ([currentVC isKindOfClass:[VideoPlayerVC class]])
{
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
and pushing to the VideoPlayerVC with link like this :
NSURL *link = [NSURL URLWithString:strUrl];
VideoPlayerVC *vc = [[VideoPlayerVC alloc] init];
vc.videoUrl = link;
[self.navigationController pushViewController:vc animated:false];
This allows me to enable autorotate in the VideoPlayer ViewController but when the video playback ends in Landscape mode, the entire app is being converted into Landscape view mode only.
Please help me guys to fix the issue.
Thanks in advance.
The system only tries to invalidate your orientation when a full screen modal presentation happened or dismissed. So I suggest you to replace your [self.navigationController pushViewController:vc animated:false]; with [self presentViewController:vc animated:YES completion:nil];
And if your UE needs the navigation transition, you can try to mimic it with UIViewControllerContextTransitioning customization.
Also there is a tricky method if you must use push behavior(It's the only one method not using private api as I know)
Every time you push/pop from the navigation stack, call the code below:
[[vc presentViewController:[UIViewController new] animated:NO completion:^(BOOL completed){
[vc dismissViewControllerAnimated:NO completion:nil];
}];
The code try to make an invisible vc and dismiss it immediately to make iOS update the supported orientation.

viewWillAppear incorrect orientation when presentViewController without a navigation view controller

I came across a strange problem, i'am in landscape and present a VC
SomeViewController * vc = [[SomeViewController alloc] init];
[self.navigationController presentViewController:vc animated:YES completion:^{
}];
in the viewWillAppear the frame is incorrectly in portrait and in viewDidAppear it is correctly in landscape
but when i put it inside a UINavigationController
SomeViewController * vc = [[SomeViewController alloc] init];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:vc];
[self.navigationController presentViewController:nc animated:YES completion:^{
}];
than in the viewWillAppear the frame is correctly landscape
Is this a bug? I'am i missing something? Why it is behaving like this?
I think it is by design, see this answer A: View frame changes between viewWillAppear: and viewDidAppear:.
By viewDidAppear the frame geometry is correct since it is now on-screen and added to the view hierarchy but I would actually recommend doing it in viewDidLayoutSubviews since that will be called when rotating orientations as well.

How to make only MPMoviePlayerController in both landscape and portrait mode

I am working on a application which has to play videos when clicking on some thumbnail, I am displaying list. I want only the Video to rotate in Landscape and Portrait Mode but other screens must in Portrait Mode How can we do this.
In ViewWillAppear
UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:NO completion:NULL];
[self dismissViewControllerAnimated:NO completion:NULL];
This will trigger the redrawing of the view and thus calling following functions, provided you have implemented these
- (BOOL)shouldAutorotate{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

How to dismiss the keyboard on iPad when using modalPresentationStyle

For my iPad app, I have a view displayed modally as a formsheet when a button is pushed. In order to have the keyboard dismissed after entering text in a textfield i tried as suggested;
the "disablesAutomaticKeyboardDismissal" method.
This does not work, in fact, the method is never called acording to the log.
The keybord will dismiss for iPhone or when i choose to not present modally.
Here is my code:
- (BOOL)disablesAutomaticKeyboardDismissal
{
NSLog(#"method calls");
return NO;
}
- (IBAction)showNewView:(id)sender
{
MyViewController *mvc =
[[MyViewController alloc] init];
// some lines about setting content
//...
UINavigationController *navController = [[UINavigationController alloc]
initWithRootViewController:mvc];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentViewController:navController animated:YES completion:nil];
}
-(BOOL)disablesAutomaticKeyboardDismissal
or not, the keyboard is not dismissed unless i remove tis line:
// [navController setModalPresentationStyle:UIModalPresentationFormSheet];
However, then it is not presented the way I want anymore.
Can anyone see what I am doing wrong?
-(BOOL)disablesAutomaticKeyboardDismissal needs to overridden to return NO by the view controller that is presented as a form sheet, not by the presenter; That's your mistake. In your case you could subclass UINavigationController to get the desired behaviour:
#interface AutomaticKeyboardDismissingNavigationController : UINavigationController
#end
#implementation AutomaticKeyboardDismissingNavigationController
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}
#end
(The class name could probably be a bit shorter and still be comprehensible.)

Presenting ModalViewController Modally on iPad

Im calling this code from the MasterViewController in a UISplitVC for an iPad app:
-(void)viewWillAppear:(BOOL)animated{
//PRESENT MODALVC
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[self setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:modalVC animated:YES];
}
but it doesn't work. No ModalVC appears.
Try this code:
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[modalVC setModalPresentationStyle:UIModalPresentationFullScreen]; //You set the presentation style of the controller that would be presented, not the presenting controller
//This check is needed, because presentModalViewController:animated is depreciated in iOS5.0 and presentViewController:animated:completion must be used instead. The same is valid for dismissModalViewControllerAnimated and dismissViewControllerAnimated:completion
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:modalVC animated:YES completion:nil];
else
[self presentModalViewController:modalVC animated:YES];
If you are targeting iOS5.0+ only this check is not needed and you should use only presentViewController:animated:completion and dismissViewControllerAnimated:completion

Resources