How to Set Layout of Button in iPhone

Introduction

In this article I will create a single-view application. Here we set the layout of a button when the application is in the running mode. Here the button layout color will change when we move the iPhone screen's orientation from landscape to portrait or portrait to landscape.

To better understand, 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 Single 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.

AppDelegate.h

#import <UIKit/UIKit.h>

@class layoutViewController;

@interface layoutAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) layoutViewController *viewController;

@end

AppDelegate.m

#import "layoutAppDelegate.h"

#import "layoutViewController.h"

@implementation layoutAppDelegate

- (void)dealloc

{

[_window release];

[_viewController release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

self.viewController = [[[layoutViewController alloc] initWithNibName:@"layoutViewController" bundle:nil] autorelease];

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application

{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end


ViewController.h

#import <UIKit/UIKit.h>

@interface layoutViewController : UIViewController

@property (strong, nonatomic) IBOutlet UIButton *firstButton;

@property (strong, nonatomic) IBOutlet UIButton *secondButton;

@end


ViewController.m

#import "layoutViewController.h"

@interface layoutViewController ()

@end

@implementation layoutViewController

@synthesize firstButton, secondButton;

- (void)willAnimateRotationToInterfaceOrientation:

(UIInterfaceOrientation)toInterfaceOrientation

duration:(NSTimeInterval)duration

{

if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft ||

toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)

{

firstButton.frame = CGRectMake(20, 20, 210, 260);

firstButton.backgroundColor=[UIColor blackColor];

secondButton.frame = CGRectMake(250, 20, 210, 260);

}

else

{

firstButton.frame = CGRectMake(20, 20, 280, 205);

secondButton.frame = CGRectMake(20, 233, 280, 207);

firstButton.backgroundColor=[UIColor redColor];

}

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);

}

@end

ViewController.Xib

Xib-file-in-iPhone.png

Step 7

Click on the "Run" button to show output.

Step 8

Output1 in iPhone:

Output1-in-iPhone.png

Output2 in iPhone:

Output2-in-iPhone.png

Output3 in iPhone:

Output3-in-iPhone.png