RSS Feed

Category Archives: iPhone Programming

iOS 6 : How to fix rotation for different UIViewController Class

iOS seems awkward for rotation of iPhone/iPad app screen. Sometimes you need to rotate only single view controller class without rotating any other screen. In XCode if you check App Target (support interface orientations) there are four options are given :
1. Portrait 2. Upside down 3. Landscape left 4. Landscape right.

If you select only 1 and 2, you app will work only in portrait mode. Your rotation code will not affect on iPhone screen. Same for Landscape.

Lets consider my app in which i have 2 screens. Both are inherited from UIViewController class. My first screen (ViewController1) should run only in Portrait mode and my second screen (ViewController1) may work in all orientation (i.e portrait and landscape). Follow these step for fix the such case in your app :

1. In your app delegate make sure you are not using [self.window addSubview:RootController.view];
You should use : [self.window setRootViewController:RootController];

2. Inside App Target select all orientation

3. In your class ViewController1.m write this code : // This class will work only in Portrait.
– (BOOL)shouldAutorotate {

return NO;
– (NSUInteger)supportedInterfaceOrientations
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

return (toInterfaceOrientation == UIInterfaceOrientationPortrait);

4. In your class ViewController2.m write this code : // This class will work in all orientations.
– (BOOL)shouldAutorotate{

return YES;

return UIInterfaceOrientationMaskAll;
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

if ([self.navigationController.visibleViewController isKindOfClass:[ViewController2 class]])
return (YES);
return (toInterfaceOrientation == UIInterfaceOrientationPortrait);

Now run the app. It works like charms !!


Creating ‘Pulse’ style scrolling – Horizontally scrolling UITableView as a subview of UITableViewCell

A nice tutorial:

Creating ‘Pulse’ style scrolling – Horizontally scrolling UITableView as a subview of UITableViewCell.

Error 403 while getFollowers : Twitter iPhone MGTwitterEngine

I was trying to access the followers list from my twitter account by using MGTwitterEngine. But it is giving error. Here is all step what i have done to fixed it.

1. In MGTwitterEngine.m change #define TWITTER_DOMAIN @”” To #define TWITTER_DOMAIN @”″

andĀ  #define USE_LIBXML 0 to #define USE_LIBXML 1 (Most of the people are not doing this and getting no response)

2. In your working class.h add #import “MGTwitterEngine.h” and add its delegate <MGTwitterEngineDelegate>

3. In your working class.m add these methods:

#pragma mark TwitterEngineDelegate
– (void) requestSucceeded: (NSString *) requestIdentifier {
NSLog(@”Request %@ succeeded”, requestIdentifier);
– (void) requestFailed: (NSString *) requestIdentifier withError: (NSError *) error {
NSLog(@”Request %@ failed with error: %@”, requestIdentifier, error);
– (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)connectionIdentifier {

– (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier {

NSLog(@”Recieved Object: %@”, dictionary);
– (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier {

NSLog(@”Direct Messages Received: %@”, messages);
// This method will call when you received all your followers. I have used one NSMutableArray “friendsList” to store all followers information. You can modify this according to your need.
– (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {

NSLog(@”User Info Received: %@”, userInfo);

friendsList = [[NSMutableArray alloc]init];

for (NSDictionary *u in userInfo ) {

NSDictionary *followersDic = [[NSDictionary alloc]initWithDictionary:u];
[friendsList addObject:followersDic];
[followersDic release];
[self.tableView reloadData];

This is all what i have added in my class and then calling this method: [[GetAppDelegate() facebook] requestWithGraphPath:@”me/friends” andDelegate:self]; And it return all my followers list.

I hope this code will help to those who have scratching his head from some couple of hours šŸ˜‰

!! Enjoy !!

libz.1.2.3.dylib not found in iOS5, XCode 4.2

Posted on

It seems that libz.1.2.3.dylib is not included in iOS5 sdk. Here is simple method to fix this:

1. Open your project in XCode 4.2

2. Remove libz.1.2.3.dylib from framework

3. Add new framework libz.1.2.5

4. Run you app

It works fine. At least for me šŸ˜‰


!! Enjoy !!

Resize table cell according to text length

Posted on

Some times we need to customize our table cell’s height according to text length. For long text we need to make a bigger cell and vice versa. Here is the method to do this:

1. define you table cell’s size and font:

#define CONST_Cell_height 50.0f
#define CONST_Cell_width 270.0f
#define CONST_textLabelFontSizeĀ Ā Ā Ā  16
#define CONST_detailLabelFontSizeĀ Ā  14

2. Here is the method to calculate table cell width:

– (int) heightOfCellWithTitle :(NSString*)titleText
CGSize titleSize = {0, 0};
CGSize subtitleSize = {0, 0};

if (titleText && ![titleText isEqualToString:@””])
titleSize = [titleText sizeWithFont:[UIFont boldSystemFontOfSize:16]
constrainedToSize:CGSizeMake(CONST_Cell_width, 4000)

if (subtitleText && ![subtitleText isEqualToString:@””])
subtitleSize = [subtitleText sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(CONST_Cell_width, 4000)

descriptionLabelHeight = subtitleSize.height;

return titleSize.height + subtitleSize.height;

3. Use this in your table height for row:

– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *title = @”Yor cell title”;
NSString *subtitle =@”Your cell subtitle”;

int height =Ā  “some constant” + [self heightOfCellWithTitle:title andSubtitle:subtitle]; // some constant is used to adjust cell height. may be zero.

return (height < CONST_Cell_height ? CONST_Cell_height : height);


it will resize the table cell according to text size.

!! Enjoy !!

Masking of Image on another image

Posted on

Here is a small core graphics method for masking of images:


– (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

CGContextRef mainViewContentContext;
CGColorSpaceRef colorSpace;

colorSpace = CGColorSpaceCreateDeviceRGB();

// create a bitmap graphics context the size of the image
mainViewContentContext = CGBitmapContextCreate (NULL, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaPremultipliedLast);

// free the rgb colorspace

CGImageRef maskingImage = [maskImage CGImage];
CGContextClipToMask(mainViewContentContext, CGRectMake(0, 0, maskImage.size.width, maskImage.size.height), maskingImage);
CGContextDrawImage(mainViewContentContext, CGRectMake(0, 0, image.size.width, image.size.height), self.CGImage);

// Create CGImageRef of the main view bitmap content, and then
// release that bitmap context
CGImageRef mainViewContentBitmapContext = CGBitmapContextCreateImage(mainViewContentContext);

// convert the finished resized image to a UIImage
UIImage *theImage = [UIImage imageWithCGImage:mainViewContentBitmapContext];
// image is retained by the property setting above, so we can
// release the original

maskingImage = nil;
// return the image
return theImage;


For more detail about masking please checkout Quartz 2D developer guide:


!! Enjoy !!

Limiting Zoom level in UIPinchGestureRecognizer

Posted on

Sometimes when we are zooming image by using Pinch Gesture, we can’t handle the size of zooming. Here is a small code to do this.

The code below assumes there is an instance variable CGFloat lastScale and that a view has been set for the UIPinchGestureRecognizer.

– (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {

CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@”transform.scale”] floatValue];

// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;

CGFloat newScale = 1 –Ā  (lastScale – [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;

lastScale = [gestureRecognizer scale];Ā  // Store the previous scale factor for the next pinch gesture call

!! Enjoy !!