How to Set Screen Orientation in iPhone

Introduction

In this article I will create an Empty View application. Here to manage orientation I will implement the method:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

To better understand it we use the following procedure.

Step 1

Open XCode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select Empty View Application and click on "Next".

Step 4

Now provide your Product Name and Company Identifier.

Step 5

Select the location where you want to save your project and click on "Create".

Step 6

Now we write the code of the Objective-C classes:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
BOOL isLandscape = UIDeviceOrientationIsLandscape(self.interfaceOrientation);
BOOL isPortrait = UIDeviceOrientationIsPortrait(self.interfaceOrientation);
if(isLandscape){
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
button = [[UIButton alloc] initWithFrame:CGRectMake(2.0, 10.0, 80.0, 80.0)];
[button setImage:[UIImage imageNamed:@"Sachin.png"] forState:UIControlStateNormal];
button.clipsToBounds = YES;
button.layer.cornerRadius = 40;//half of the width
button.layer.borderColor=[UIColor yellowColor].CGColor;
button.layer.borderWidth=3.0f;
[button addTarget:self action:@selector(clickland) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
CGRect frame = lineView1.frame;
frame.origin.x = 82;
frame.origin.y =50;
frame.size.width = 350;
frame.size.height = 10;
lineView1.frame = frame;
CGRect frame1 = lineView3.frame;
frame1.origin.x = 260;
frame1.origin.y = 50;
frame1.size.width = 10;
frame1.size.height = 130;
lineView3.frame = frame1;
CGRect frame2 = lineView4.frame;
frame2.origin.x = 425;
frame2.origin.y = 50;
frame2.size.width = 10;
frame2.size.height = 150;
lineView4.frame = frame2;
CGRect frame3 = btn1.frame;
frame3.origin.x = 65;
frame3.origin.y =100;
frame3.size.width = 80;
frame3.size.height = 80;
btn1.frame = frame3;
CGRect frame4 = btn2.frame;
frame4.origin.x = 225;
frame4.origin.y =150;
frame4.size.width = 80;
frame4.size.height = 80;
btn2.frame = frame4;
CGRect frame5 = btn3.frame;
frame5.origin.x = 390;
frame5.origin.y = 200;
frame5.size.width = 80;
frame5.size.height = 80;
btn3.frame = frame5;
}
if(isPortrait){
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
button = [[UIButton alloc] initWithFrame:CGRectMake(2.0, 10.0, 80.0, 80.0)];
[button setImage:[UIImage imageNamed:@"Sachin.png"] forState:UIControlStateNormal];
button.clipsToBounds = YES;
button.layer.cornerRadius = 40;//half of the width
button.layer.borderColor=[UIColor yellowColor].CGColor;
button.layer.borderWidth=3.0f;
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
CGRect frame = lineView1.frame;
frame.origin.x = 82;
frame.origin.y =50;
frame.size.width = 201;
frame.size.height = 10;
lineView1.frame = frame;
CGRect frame1 = lineView3.frame;
frame1.origin.x = 185;
frame1.origin.y =60;
frame1.size.width = 10;
frame1.size.height = 200;
lineView3.frame = frame1;
CGRect frame2 = lineView4.frame;
frame2.origin.x = 273;
frame2.origin.y =60;
frame2.size.width = 10;
frame2.size.height = 300;
lineView4.frame = frame2;
CGRect frame3 = btn1.frame;
frame3.origin.x = 65;
frame3.origin.y =130;
frame3.size.width = 80;
frame3.size.height = 80;
btn1.frame = frame3;
CGRect frame4 = btn2.frame;
frame4.origin.x = 150;
frame4.origin.y =250;
frame4.size.width = 80;
frame4.size.height = 80;
btn2.frame = frame4;
CGRect frame5 = btn3.frame;
frame5.origin.x = 238;
frame5.origin.y = 358;
frame5.size.width = 80;
frame5.size.height = 80;
btn3.frame = frame5;
}

Step 7

Click on the Run button to show the output.

Output1 in iPhone:

Portrait-View-in-iPhone.png

Output2 in iPhone:

Landscape-View-in-iPhone.png

Up Next
    Ebook Download
    View all
    Learn
    View all