Wednesday, July 25, 2012

How to add left padding to the UITextField, when add background Image


#import "QuartzCore/QuartzCore.h"    // First import this Framework
......



UITextField *_emailText = [[UITextField alloc]initWithFrame:CGRectMake(40, 8, 250, 34)];
//    _emailText.borderStyle = UITe;  // Please set borderStyle to default value, otherwise it is not working.
    _emailText.delegate = self;
    _emailText.placeholder = @"Email";
    [_emailText setFont:[UIFont boldSystemFontOfSize:12]];
    _emailText.tag = 1;
    _emailText.returnKeyType = UIReturnKeyNext;
    _emailText.textAlignment = UITextAlignmentLeft;
    _emailText.keyboardType = UIKeyboardTypeEmailAddress;
    _emailText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    _emailText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 12, 20)];
    _emailText.leftViewMode = UITextFieldViewModeAlways;
    _emailText.background = [[UIImage imageNamed:@"textBox.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:17];
    [self.view addSubview:_emailText];

Monday, April 23, 2012

Remove current view in Scroll View


    for(UIView *subview in [stageScrollView subviews])
    {
        [subview removeFromSuperview];
        NSLog(@"Subviews Count=%d",stageScrollView.subviews.count);
    }

    [stageScrollView removeFromSuperview];

Adjust font size to the UILable


        nameLable_.numberOfLines = 1;
        nameLable_.minimumFontSize = 15;
        nameLable_.adjustsFontSizeToFitWidth = YES;

If you want to adjust font size this way, label.numberOfLines = 1 is mandatory.

* If numberOfLines != 1, You can get frame hight using this:


        CGSize labelSize = [@"" sizeWithFont:nameLable_.font forWidth:nameLable_.frame.size.width lineBreakMode:nameLable_.lineBreakMode];
       
        nameLable_.frame = CGRectMake(nameLable_.frame.origin.x, nameLable_.frame.origin.y, nameLable_.frame.size.width, labelSize.height);

Add rounded Corners and Border to UIImageView


    First you have to import 

Add rounded Corners:

        imageView_.layer.cornerRadius = 5.0;
        imageView _.layer.masksToBounds = YES;

Add Border :
        imageView _.layer.borderColor = [UIColor whiteColor].CGColor;
        imageView _.layer.borderWidth = 3.0;

Costom Back Button in iPhone / iPad


  UIButton*  backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 52, 31)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"BackButton.png"] forState:UIControlStateNormal];
    //[backButton setTitle:@"CLOSE" forState:UIControlStateNormal];
    [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backButton.titleLabel setFont:[UIFont boldSystemFontOfSize:14.0f]];
    [backButton addTarget:self action:@selector(getBackView) forControlEvents:UIControlStateHighlighted];
   
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:backButton];
   
    self.navigationItem.leftBarButtonItem = item;
    [item release];

Wednesday, January 4, 2012

iPhone UIView Animation Best Practice


You can use this method to animate a view, when changing 'yValue'.  And you can change animation curve, when changing '  [UIView setAnimationCurve:UIViewAnimationCurve..................];'



- (void)animationView:(int)yValue
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:0.50];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
   
    CGRect frame = veriableNameOfView.frame;
    frame.origin = CGPointMake(0, yValue);
    veriableNameOfView.frame = frame;
   
    [UIView commitAnimations];
}

Saturday, October 15, 2011

Control auto rotation only for 'Landscape' or 'Portrait' in iOS.



When other view controller is used both views. You want to use one view controller only use as 'Landscape' or 'Portrait'.

Call this 'setupOneRotation' method in viewWillAppear: (as [self setupOneRotation];)




BOOL            startWithLandscape;



#pragma mark autoRotation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return NO;
}

- (void)setupOneRotation
{
    if (self.view.frame.size.width < self.view.frame.size.height) {
       
        nameOfAppDelegate* sharedApplication = (nameOfAppDelegate *)[[UIApplication  sharedApplication] delegate];       // More details for this, use this link
       
       
        startWithLandscape = YES;
       
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration: 0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
        sharedApplication.window.transform = CGAffineTransformIdentity;
       
       
        UIInterfaceOrientation toInterfaceOrientation = self.interfaceOrientation; // get current interface orientation
        //

        if (toInterfaceOrientation == UIInterfaceOrientationPortrait){
            sharedApplication.window.transform = CGAffineTransformMakeRotation(-M_PI/2);
           
            [[UIApplication sharedApplication]
             setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
        }else if (toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            sharedApplication.window.transform = CGAffineTransformMakeRotation(M_PI/2);
           
            [[UIApplication sharedApplication]
             setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
           
        }
       
        sharedApplication.window.bounds = CGRectMake(0.0f, 0.0f, 1024.0f, 768.0f);
        sharedApplication.window.center  = CGPointMake (384.0, 512.0);

        [UIView commitAnimations];
    }
}

- (void) viewWillDisappear:(BOOL)animated
{
    if (startWithLandscape) {
       
       
        nameOfAppDelegate* sharedApplication = (nameOfAppDelegate *)[[UIApplication  sharedApplication] delegate];
        [UIView beginAnimations:@"View Flip" context:nil];
        [UIView setAnimationDuration: 0.5f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
       
        sharedApplication.window.transform = CGAffineTransformIdentity;
            //sharedApplication.window.transform = CGAffineTransformMakeRotation(M_PI/2);
        sharedApplication.window.bounds = CGRectMake(0.0f, 0.0f, 768.0f, 1024.0f);
        sharedApplication.window.center  = CGPointMake (384.0, 512.0);
       
        [UIView commitAnimations];
    }
   
    [super viewWillDisappear: NO];
}