CGRectMake for different iphone ipad orientations - iphone

For following code I need to set different UIIMageView Positions according to iphone and ipad orientation.
// Animated images - centered on screen
animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(
(SCREEN_WIDTH / 2) - (IMAGE_WIDTH / 2),
(SCREEN_HEIGHT / 2) - (IMAGE_HEIGHT / 2) + STATUS_BAR_HEIGHT,
IMAGE_WIDTH, IMAGE_HEIGHT)];
As CGRect will be different for ipad portrait,iphone portrait, ipad landscape and iphone landscape.
How do I do this
if (Ipad Portrait orientation)
{
code with different position using CGRectMake (...............)
}
if (Iphone Portrait orientation)
{
code with different position using CGRectMake (...............)
}
if (Ipad Landscape orientation)
{
code with different position using CGRectMake (...............)
}
if (Iphone Landscape orientation)
{
code with different position using CGRectMake (...............)
}

You can use the UIViewController's interfaceOrientation property. It will give you one of the following values:
UIInterfaceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft,
UIInterfaceOrientationLandscapeRight
Your code would look like:
if (self.interfaceOrientation == UIInterfaceOrientationPortrait)
{
...
}
else if // etc.

You can differentiate between iPhone and iPad with
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// The device is an iPad running iPhone 3.2 or later.
}
else
{
// The device is an iPhone or iPod touch.
}
and you can differentiate between portrait and landscape with
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
{
// The device is in landscape.
}
else
{
// The device is in portrait.
}
Now combine this to get the customization as you like.

Related

iPhone 4s and iPhone 5 compatibility

I am new to iPhone development i want to make my code compatible with iPhone 4s and iPhone5
but when i apply image to background view of 4S its size 320*480 but it changes in iPhone 5.
How to manage this compatibility?
Also do i need manage compatibility for buttons and their position in iPhone 4s and iPhone 5.
If yes? then how to handle position of button pro-grammatically...
I found this solution ..
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGSize result = [[UIScreen mainScreen] bounds].size;
if(result.height == 480)
{
// iPhone Classic }
if(result.height == 568)
{
// iPhone 5
}}
Does it works?
Yes, you are doing it in correct way.
Now, what you need to do is, you just need check the condition and based on that, you have to set the Frame of the components on the screen.
if([UIScreen mainScreen].bounds.size.height == 568)
{
// iPhone 5
// Set the frame of the buttons for iPhone5 screen
}
else
{
// iPhone 4
// Set the frame of the buttons for iPhone4 & 4S screen
}
}

iOS - Support iPad & iPhone without using nib

I'm trying to write an app without using nib, everything I'll do it programmatically.
Now the problem is, how am I going to support both iPad and iPhone? Obviously, I can't do it with
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
// load iPad nib
} else {
// load iPhone nib
}
If I create 2 ViewControllers, then the IBAction will be redundant.
Any suggestion?
You should probably just figure out the device type in applicationDidFinishLaunching and then load separate controllers for each device. But if you want to just have a single implementation for all devices, do checks like this:
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
//do some iPad stuff
}
else
{
CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
if([UIScreen mainScreen].scale == 2.f && screenH == 568.0f)
{
//do iPhone 5 stuff
}
else
{
//do iPhone 4S and iPhone 4 stuff
//the dimensions are the same however, if you want to do iPhone 4S specific stuff
// you'll need to do additional checks against video resolution or other differences etc
}
}
CGFloat height = [UIscreen mainScreen].bounds.size.height;
if(height==568.00 || height == 480.0)
{
//For iphone 5 and iphone 4
}
else
{
//For ipad
}
You can use this code in your AppDelegate
- (BOOL) isPad()
{
if(UI_USER_INTERFACE_IDIOM)
{
return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
}
else
{
return NO;
}
}
This link gives some info about the idiom http://developer.apple.com/library/ios/#documentation/uikit/reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/macro/UI_USER_INTERFACE_IDIOM
OR
You can check the height and width of the screen to know whether its an iPhone or iPad
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat width = CGRectGetWidth(screen);
CGFloat height = CGRectGetHeight(screen);
If you are not using any nib, everything doen programmatically you dont want to create two view controllers for the iphone and ipad. Remember do not depends on any static values. ie your calculations should be made according to self.view.bounds some thing like that (I mean to create views/subviews). Then if some specific functionality that supports only in iPad do checks
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
One view controller done all the job for you.

orientation issue - ipad

my App has several uiViews and set to support both orientations. since my uiViews frames are only part of the whole size of the iPad, so I'm trying to center my uiviews frame depending on how the iPad is held aka orientation. It's is done in this manner in the view controller.m file:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) //iPad
{
CGRect ScreenBounds = [[UIScreen mainScreen] bounds];
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown)//ipad-portrait
{
ipStPosX=(ScreenBounds.size.width -360)/2;
ipStPosY=100;
return YES;
}
else//ipad -landscape
{
ipStPosX=(ScreenBounds.size.height -360)/2;
ipStPosY=100;
return YES;
}
}
else//iphone
{
UIInterfaceOrientation des=self.interfaceOrientation;
if(des==UIInterfaceOrientationPortrait||des==UIInterfaceOrientationPortraitUpsideDown) //iphone portrait
{
return NO;
}
else //iphone -landscape
{
return YES;
}
}
}
after launching the app and changing the orientation it will always go to the portrait part, UIInterfaceOrientationPortrait, regardless of how i hold the device.
I saw some old posts that didn't really fit the issue and were confusing or dated so any help here will be great.
shouldAutorotateToInterfaceOrientation: is for simply telling iOS that you support particular orientations. since you want everything but iPhone landscape, you should return
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
return YES;
return UIInterfaceOrientationIsPortrait(orientation);
}
for actually adjusting what you display, you want to use willRotateToInterfaceOrientation:duration: or didRotateFromInterfaceOrientation: for actually changing items. in your case, given that you are just adjusting some iVars depending upon which orientation you are in, i presume you will use the latter.
- (void)didRotateToInterfaceOrientation:(UIInterfaceOrientation)fromOrientation
UIInterfaceOrientation des = self.interfaceOrientation;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) //iPad
{
CGRect ScreenBounds = [[UIScreen mainScreen] bounds];
if (UIInterfaceOrientationIsPortrait(des))//ipad-portrait
{
ipStPosX=(ScreenBounds.size.width -360)/2;
ipStPosY=100;
}
else //ipad -landscape
{
ipStPosX=(ScreenBounds.size.height -360)/2;
ipStPosY=100;
}
}
}
I think there are two problems here:
Enabling rotation - This can be done by implementing the method below. You should put a breakpoint in this method to ensure its called and you return YES. Please note that this method is only called for those views that are pushed to the navigation bar or tab bar or are part of the window. This will not be called for views that have been added to other views using addSubView method.
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation;
Implementing the above method and not implementing any other method will rotate your view. If it does not, you need to investigate the points I have outlined above. Once your view starts rotating, you need to then play with the autoresizing masks to achieve what you are planning to do. Centering the views should be easily achievable using autoresizing masks. If using XIBs, you can review the resizing behavior from within xcode.

How to change the color of navigataionbar once we turn from landscape to portrait mode and vice varsa in IPhone

Hi Will some body help me
I am using 3.2.5 SDK and 4.2 ISO
Now I am creating a splitViews project but here I am facing problem my problem is that once i am in portrait maid and move other class then i change my portrait mode to landscape at that time my rootview navigation bar color has change But i did not want to change previous i made its green color but after shifting portrait mode to landscape its change please help me some one.
Thanks.....
You could subclass UISplitViewController, and override the willAnimateRotationToInterfaceOrientation:duration: method. For example:
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration {
if([UIDevice currentDevice].orientation == UIInterfaceOrientationPortrait ||
[UIDevice currentDevice].orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.masterNavigationController.navigationBar.tintColor = nil;
} else {
self.masterNavigationController.navigationBar.tintColor = [UIColor colorWithRed:(7.0/255.0) green:(140.0/255.0) blue:(170.0/255.0) alpha:1.0];
}
[super willAnimateRotationToInterfaceOrientation:interfaceOrientation duration:duration];
}

shouldAutorotateToInterfaceOrientation with iPad and iPhone differences

I have an iPhone app which I have created as a universal iPad/iPhone app. I've implemented a splitviewcontroller for the iPad version... all fine.
In my iPhone app, everything is in Portrait, except for a 2nd level view controller (a web view), which I override shouldAutorotateToInterfaceOrientation to allow landscape. On returning up the view chain I go back to portrait.. Excellent!
However, Now my iPad split view app is forced to stay in portrait. If I override shouldAutorotateToInterfaceOrientation in any of my views like rootviewcontroller or others it effectively allows landscape mode in my iPhone app which I cannot do. However it does fix my landscape problem in the iPad.
Is there a way round this? I effectively want to say YES to shouldAutorotateToInterfaceOrientation for iPad, but no for iPhone. I tried this, but it doesnt work, it allows landscape on both devices:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
BOOL rotate = NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
rotate = YES;
}
return rotate;
}
Any advice?
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return YES;
} else {
return UIInterfaceOrientationIsPortrait(interfaceOrientation);
}