I'm working on an iPad app that start with a splash screen and then moves to the login screen. My app should support all the orientation, and iOS 4.3 and later. To do that, I added the four orientations in the plist, and the following code in the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window makeKeyAndVisible];
// Override point for customization after application launch
SplashViewController *aController = [[SplashViewController alloc] initWithNibName:#"SplashView" bundle:nil];
self.mainViewController = aController;
[aController release];
mainViewController.view.frame = [UIScreen mainScreen].bounds;// no effect
[window setFrame:[[UIScreen mainScreen] bounds]]; //no effect
//[window addSubview:[mainViewController view]];
[window setRootViewController:mainViewController];
return YES;
}
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
return UIInterfaceOrientationMaskAll;
}
In the splash screen
- (void) loadView {
[super loadView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(orientationChanged) name:#"UIDeviceOrientationDidChangeNotification" object:nil];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (BOOL)shouldAutorotate{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged
{
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
NSLog(#"Portrait");
else
NSLog(#"Landscape");
}
Here I get the correct orientation but when rotating, I get an inverted result, I get landscape for portrait and portrait for landscape. I tried to convert the code like this:
- (void) orientationChanged
{
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
NSLog(#"Landscape");
else
NSLog(#"Portrait");
}
I get the first result wrong, after that the result is correct. can you help?
note that I have put some uielement to test and the test come with the same result.
Thank you
I managed to solve this matter after two days of searching and testing, none of the answers in the forum was complete, and here is how I solved:
//this for the orientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
[self orientationChanged];
}
// here is the tricky thing, when startup you should have a special function for the orientation other than the orientationchanged method, and must be called here in viewDidAppear, otherwise it won't work
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self StartUpOrientation];
}
#pragma mark -
#pragma mark Orientation Methods
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (BOOL)shouldAutorotate {
return YES;
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
- (void) orientationChanged {
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
[self.view setBounds:CGRectMake(0, 0, 1024, 748)];
// your code here
}
else {
[self.view setBounds:CGRectMake(0, 0,768,1004)];
//your code here
}
}
- (void) StartUpOrientation {
UIInterfaceOrientation interfaceOrientation =[[UIApplication sharedApplication] statusBarOrientation];
if (interfaceOrientation != UIInterfaceOrientationLandscapeLeft && interfaceOrientation != UIInterfaceOrientationLandscapeRight) {
[self.view setBounds:CGRectMake(0, 0,768,1004)];
// your code here
}
else {
[self.view setBounds:CGRectMake(0, 0, 1024, 748)];
// your code here
}
}
hope it will help someone someday
Related
I have an app that changes the view when the orientation changes using: [[UIApplication sharedApplication] statusBarOrientation]. On the iPhone, it works but on the iPad, it gives me landscape instead of portrait and portrait instead of landscape.
When I call the getRelevantFrame method outside of the UIDeviceOrientationDidChangeNotification, it will return the correct frame.
It will return the opposite frame (if landscape, will return portrait and vice versa) when responding to the notification.
Both the versions (iPod/iPhone + iPad) uses the same code but this only breaks on the iPad version
Here is the code I used to calculate the relevant frame:
EDIT: Used rob5408's advice. Changed to use UI_USER_INTERFACE_IDIOM().
+ (CGRect)getRelevantFrame {
//Get orientation
UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) { //iPhone/iPod
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
return [self frameiPhoneLandscape];
}
else {
return [self frameiPhonePortrait];
}
} else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { //iPad
if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(#"iPad Landscape");
return [self frameiPadLandscape];
}
else {
NSLog(#"iPad Portrait");
return [self frameiPadPortrait];
}
}
return CGRectMake(0, 0, 0, 0);
}
Here is the notification I used:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
Here is the code I used to read the orientation change:
- (void) didRotate:(NSNotification *)notification
{
NSLog(#"Parent: %#", self.parentController);
if(self.parentController) {
[self setFrame:[TASFrames getRelevantFrame]];
}
}
I transferred the code to a new project. And now it works as it's supposed to. No idea as to why this happens. Code didn't change.
In my app, all view is opening in Portait mode, But I want to change the mode for AVPlayer view, means When user come on this view, device orientation is automatically change in to Landscape mode.
I have implemented this using following code. It works fine, I want to just confirm, by using following code, there will be any problem when I upload my app on appstore.
-(void)viewDidAppear:(BOOL)animated
{
if(UIDeviceOrientationIsPortrait(self.interfaceOrientation)){
if ([[UIDevice currentDevice] respondsToSelector:#selector(setOrientation:)])
{
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationLandscapeLeft );
}
}
}
-(void)viewDidDisappear:(BOOL)animated{
if(UIDeviceOrientationIsLandscape(self.interfaceOrientation)){
if ([[UIDevice currentDevice] respondsToSelector:#selector(setOrientation:)])
{
objc_msgSend([UIDevice currentDevice], #selector(setOrientation:), UIInterfaceOrientationPortrait );
}
}
}
Here you can find a brief discussion on the similar issue. Non of them worked for me. Here is my work around.
-(BOOL)shouldAutorotate
{
return true;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeLeft |
UIInterfaceOrientationLandscapeRight;
}
-(void) setSelfOrientation
{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[self shouldAutorotate];
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
[self.view setFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.height,
[[UIScreen mainScreen] bounds].size.width)];
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; // rollback previous orientation
[self shouldAutorotate];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setSelfOrientation];
}
I need to record video always in landscape mode even my device rotating (Portrait->Landscape->Portrait ).
To take angle is simple, im using hyroscope. My problem is to record video in landscape always. Any advices needed.
Im using AVCaptureSession to video rec. AVMutablecomposition? I dont know what to use...
help me,please
Why not forcing your Controller to be only in landscape mode ?
You can do it like this :
- (BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
You can try this code
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Also have look on the Apple's docs HERE for detail rotation implementations.
Try this before starting to record a video:
let videoDataOuputConnection = videoFileOutput.connection(with: .video)
videoDataOuputConnection!.videoOrientation = AVCaptureVideoOrientation.landscapeLeft
I am using tableview with search bar display controller, it shows good in portait and landscape view, but rotate from landscape to portrait search bar size is reduced, how to change the original size
The first your should catch the orientation:
//AppDelegate.h
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (nonatomic, unsafe_unretained) NSInteger isPortrait;
//AppDelegate.m
#synthesize isPortrait;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
isPortrait = 1;
}
Implementation state:
-(void)viewWillAppear:(BOOL)animated{
[[UIApplication sharedApplication] statusBarOrientation];
[[UIDevice currentDevice] orientation];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(deviceRotated:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
//methode deviceRotated
-(void)deviceRotated:(NSNotification*)notification{
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if(orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
[self searchBarLandscape];
app.isPortrait = NO;
}
else{
[self searchBarPortrait];
app.isPortrait = YES;
}
}
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || toInterfaceOrientation == UIInterfaceOrientationPortrait)
{
app.isPortrait = 1;
}
else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
app.isPortrait = 0;
}
[self prepareSearchBarOrientation];
}
-(void)prepareSearchBarOrientation
{
AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
if (app.isPortrait)
{
[self searchBarPortrait];//Set Frame here
}
else
{
[self searchBarLandscape];
}
}
Did you try setting the searchBar's frame in the rotation delegate methods or Try auto resizing in XIB.
See this for a better idea.
Im currently working with a IPhone-app that has a "timetable".
in portrait i want it to have a regular table-view with some customization! When i have the IPhone in landscape i want it to change to a more "timetable"-view, with tables and rows.
Is it possible?
Try This
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self adjustViewsForOrientation:toInterfaceOrientation];
}
-(void) adjustViewsForOrientation:(UIInterfaceOrientation)orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//write code for portrait mode
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//write code for landscape mode
}
}
See Apples documentation Creating an Alternate Landscape Interface
Make sure to read the documentation, but here is the code from the example implementation:
#implementation PortraitViewController
- (void)awakeFromNib
{
isShowingLandscapeView = NO;
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
}
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self performSegueWithIdentifier:#"DisplayAlternateView" sender:self];
isShowingLandscapeView = YES;
}
else if (UIDeviceOrientationIsPortrait(deviceOrientation) &&
isShowingLandscapeView)
{
[self dismissViewControllerAnimated:YES completion:nil];
isShowingLandscapeView = NO;
}
}
Answer is Yes it's possible through the following method:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
\\ your timetable customisation goes here
}
}
You also need this:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}