Set different constraints for iPad in portrait and landscape mode - ios

When the device is in portrait orientation I have tableView and graphView in a viewController. When i change to landscape mode, i want to display only graphView. I am using size classes to do it. It works for all iPhones.
But for iPad, Size classes for portrait and landscape are same (regular width and height).
I need to set different constraints for portrait and landscape mode.How do i remove tableView when the device is in landscape.

This code may help you:
- (void) handleOrientation:(UIInterfaceOrientation) orientation
{
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
//handle the portrait view
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
//handle the landscape view
//write your code for removing Table
}
}

Related

View drawing correctly when changing orientation, but fails on first display

I have a UIViewController in iOS 9 that does not draw correctly when the view appears in landscape mode.
However if the view appears in portrait mode, and then is changed to landscape everything is fine.
This happens both on the simulator and devices. I am using Interface Builder to set the heights and constraints of the subviews. I do not do any resizing of frame sizes or updates to constraints in code.
So I tried to force a redraw similar to a change of orientation, but I could not get it to work and I am not sure if this is the best approach.
Does anyone have an idea how to solve this?
In your ViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

Force autorotate of view in ios 6

I want forcefully autorotate view from portrait to landscape mode in IOS 6. The view to be in landscape mode displays graphs. How can I achieve this ?
In your application’s Info.plist file, add the UIInterfaceOrientation key and set its value to the landscape mode.
you can set the value of this key to UIInterfaceOrientationLandscapeLeft or UIInterfaceOrientationLandscapeRight. for landscape orientations.
Override your view controller’s shouldAutorotateToInterfaceOrientation: method and return YES only for the desired landscape orientation and NO for portrait orientations.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

Set keyboard orientation only (iOS 6)

my app has been working fine until I've tried making some changes using the ios 6 SDK
The app runs in portrait mode 99% of the time. This has been constrained by only allowing portait mode to be available in the info.plist
There is one view controller which needs to be shown in landscape mode. This is achieved "manually" by simply rotating the view by 90 degrees, like so:
self.view.transform = CGAffineTransformMakeRotation(3.14159/2);
This still works fine in iOS 6.
However, this view controller has some text fields. When the user taps one it shows the keyboard. Since I've only rotated the view (and not actually changed the orientation of the device), the keyboard comes out in portrait mode, which is no good.
In previous versions of iOS, I set the orientation of the status bar to be landscape, and as a byproduct, this would set the keyboard to be landscape as well, like this:
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationLandscapeRight animated:NO];
However, this has stopped working for iOS 6.
I have read a million stack overflows trying to get this to work, but still have no luck.
Changing the keyboard orientation and transform is an difficult part and not a good solution(especially when it changes status bar orientations).
Better solutions is to allow application to support all orientations.
Implement the Orientation delegates inside your ViewControllers asper the rotation support.
For Supporting only Landscape
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return interfaceOrientation == UIInterfaceOrientationLandscapeLeft
|| interfaceOrientation == UIInterfaceOrientationLandscapeRight ;
}
- (BOOL)shouldAutorotate
{
return NO;
}
For Supporting only Portrait
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}

iOS best way to change the UI when orientation is changed

Which is the best way to change the UI when orientation is changed?
For example, use two different UIView one portrait and a landscape and show one of both if orientation is changed, or use one UIView and change the UI control sizes and positions?
Any other ideas?
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||
toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(#"Change to custom UI for landscape");
}
else if (toInterfaceOrientation == UIInterfaceOrientationPortrait ||
toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(#"Change to custom UI for portrait");
}
}
I always recommend using the autoresizingmask for all the subviews in the view controller view. With this being set correctly all the views will resize automatically from the orientation and you don't need the extra rotation specific subviews (one portrait view and one landscape view).

iPad orientation behaviour inconsistent - changes on each launch?

I have a very strange problem that I am as yet unable to diagnose.
In my iOS app (it's a universal binary), when I run on iPad 4.3, the orientation at launch is inconsistent.
The app delegate adds a splash screen (UIViewController) to the main window, then removes it and adds the app's primary view. It is this primary view which is the issue - approximately half of the time, it loads up correctly in landscape orientation, the other half it loads the view in portrait (though both the status bar and keyboard are correctly in landscape).
I'm at a bit of a loss as to why the orientation at launch is changing, when I am making no changes to the code or the simulator/device orientation.
I have UIInterfaceOrientationLandscapeLeft and UIInterfaceOrientationLandscapeRight set as the only supported orientations for iPad in the Info.plist and every view controller is using the following:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
// The device is an iPad running iPhone 3.2 or later.
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
return YES;
}
} else {
// The device is an iPhone or iPod touch.
if (interfaceOrientation == UIInterfaceOrientationPortrait) {
return YES;
}
}
return NO;
}
Can anyone help me out with this??
Cheers,
Olly
If the status bar and keyboard are always in landscape then the problem should not be in shouldAuto... method. It might be in viewDidLoad. Also, try the following: in project-info.plist, set "Initial interface orientation" as landscape left/right, "clean" the project and run.

Resources