Showing posts with label iPhone Tricks. Show all posts
Showing posts with label iPhone Tricks. Show all posts

Sunday, July 15, 2012

How to add Shadow,Border and Round corner to View programatically


here is simplest way what I do, When I need to add Shadow, Border or Round Corners Effects on any view

in ViewController.h

1. add QuartzCore.framwork in your project

2. add the UIView in XIB via Interface Builder

3. create UIView's IBOutlet and Hook-up with it
@property (weak, nonatomic) IBOutlet UIView *backgoundWall;

in ViewController.m

4. import QurtzCore header file.
#import <QuartzCore/QuartzCore.h>

3. synthesise the View property variable
@synthesize backgoundWall;

4. in viewDidLoad method add Shadow 

- (void)viewDidLoad
{
    [super viewDidLoad];    

    backgoundWall.layer.shadowColor =[UIColor blackColor].CGColor;
    backgoundWall.layer.shadowRadius =3.0;
    backgoundWall.layer.shadowOpacity =0.8;
    backgoundWall.layer.cornerRadius =5.0;
    backgoundWall.layer.shadowOffset =CGSizeMake(0, 7);
    backgoundWall.layer.borderColor = [UIColor lightGrayColor].CGColor;
    backgoundWall.layer.borderWidth =2.0;
}

5. Here is the result screen



Cheers!! We did it.

I would love to here you feedback!!

Saturday, June 2, 2012

Why we use #pragma mark in Objective-C/iPhone code and How to do that?

1. Why :  use pragma mark to help you organise your code for yourself  

Let! me make it more clear, When we work on huge projects and our classes contains tons of lines of code.
it's hard to manage all the methods specially when you are looking for any desired method and you need to scroll all the way again and again.

So #pragma is the way to organise the code, and provide you, Overview of the class showing all the methods, constants, properties etc.

Note: it doesn't affect anything on your code, just for the organising code

2. How : It's pretty simple when you start writing code just add #pragma above method or block every time when you need it.

Example :

#pragma mark - label name

so let's I have some life cycle methods of view controller then I will do like this

#pragma mark - ViewController's LIfe Cycle Methods

3. Here is Example code snip :

------------------------------------------------------------------------------------------------

#pragma mark - ViewController's LIfe Cycle Methods

- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];    
}

#pragma mark - Button Click methods

- (IBAction)goButtonPressed:(id)sender {    
    readMeViewController = [[ReadMeViewController alloc] initWithNibName:@"ReadMeViewController" bundle:nil];
    readMeViewController.readMeControllerDelegate = self;
    [self.navigationController pushViewController:readMeViewController animated:YES];
    
}
------------------------------------------------------------------------------------------------
and whenever I need to look for my class overview just press ctrl+6 and below is the result

4. result (class overview) :



Cheers!! we will use it from now ..Happy Coding :)

Tuesday, May 22, 2012

iPhone Tips and Tricks

How to disable ARC in Xcode for Latest iOS Applications

1. Select Project --> Builds Phases -->Compile Sources

double click and File for which you want to disable ARC --> In pop window paste this command
-fno-objc-arc
That's it, Build project, Enjoy!!


--------------------------------------------------------------------------------------------------------------

How to get Current Device Info (like it is iPhone,iPad or iPhone Simulator etc.)

Just use the Below code and you will get return string as

NSString *deviceType = [[UIDevice currentDevice] model];
NSLog(@"My Current Device is : %@",deviceType);

iPhone Device Name = " iPhone "
iPhone Simulator Name = " iPhone Simulator "
iPad Simulator Name = " iPad Simulator "
iPod Device Name = " iPod touch " 

--------------------------------------------------------------------------------------------------------------

Show/Hide Hidden Files on Your Mac

1. Show Hidden files
1.1 Launch the Terminal (you can find Terminal this way)

      Go --> Utilities - > Terminal

1.2  paste this command

defaults write com.apple.Finder AppleShowAllFiles TRUE

1.3 Hit Enter
1.4 now for restart Finder hit below command

killall Finder

2. Hide Hidden files
1.1 Launch the Terminal (you can find Terminal this way)

      Go --> Utilities - > Terminal

1.2  paste this command

defaults write com.apple.Finder AppleShowAllFiles FALSE

1.3 Hit Enter
1.4 now for restart Finder hit below command

killall Finder

How to Create Button programatically

1. add below code in viewDidLoad method of ViewController

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
    [button setTitle:@"My Button" forState:UIControlStateNormal];
    button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [self.view addSubview:button];

2. add this method and Go for Execute the code for desired output.

-(void) buttonPressed{
    NSLog(@"My Button pressed.");
}


--------------------------------------------------------------------------------------------------------------

How to get Current Device Orientation (like It's Portrait or Landscape etc.)

Just use the Below code and you will get result

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
UIInterfaceOrientation orientationType = [UIDevice currentDevice].orientation;
    
    NSLog(@"Device's Current Orientation is : %d",orientationType);
    
    if(orientationType == 0){
        //this is really a just a failsafe.
        NSLog(@"Orientation is in :Default Portrait :");
    }else if(orientationType == 1){        
        NSLog(@"Orientation is in : Portrait :");
        
    }else if(orientationType == 2){        
        NSLog(@"Orientation is in : Portrait UpsideDown :");
        
    }else if(orientationType == 3){        
        NSLog(@"Orientation is in : Landscape Left :");
        
    }else if(orientationType == 4){        
        NSLog(@"Orientation is in : Landscape Right :");
    }   

--------------------------------------------------------------------------------------------------------------


How to change Navigation Bar's Background colour and Title programatically

add below code in viewDidLoad :
1. update tint color
//set Green Colour
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:34/255.0f green:139/255.0f blue:34/255.0f alpha:1];

2. add title
self.title = @"Welcome Screen";


How to add Bar-Button on Navigation Bar

1. Make sure you have already added Navigation Bar controller in application

2. Create UIBarButton

UIBarButtonItem *signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:@" SIGN IN " style:UIBarButtonItemStyleBordered target:self action:@selector(signInUser)];
UIColor *button_color = [UIColor colorWithRed:109.0/255.0 green:152.0/255.0 blue:136.0/255.0 alpha:1.0];
signIn_BarButton.tintColor =button_color;

3. Add it to Navigation Bar Controller (on right side)

self.navigationItem.rightBarButtonItem =signIn_BarButton;

4. Add the method for Button click

-(void) signInUser{
    
    signInViewController = [[SignInViewController alloc] init];
    [self.navigationController pushViewController:signInViewController animated:YES];
}

5. here is the result screen




--------------------------------------------------------------------------------------------------------------

Handle Screen Orientations in iOS 5 and iOS 6 

//For up-to iOS 5.0
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported all orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


//For iOS 6.0
-(NSInteger)supportedInterfaceOrientations
{
    //Supporting only portrait orientation.
    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return NO;
}

--------------------------------------------------------------------------------------------------------------


How to disable NSLog in Xcode for Production stage

Add #define NSLog  in appName-Prefix.pch file in Supporting Files Folder of your project
and result file code look like..

//
// Prefix header for all source files of the 'NSLog' target in the 'NSLog' project
//

#import <Availability.h>

#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

//Add this to disable NSLog
#define NSLog

--------------------------------------------------------------------------------------------------------------



How to Get Current Device Width and Hieght

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;

--------------------------------------------------------------------------------------------------------------