Monday, February 3, 2014

Swipe

Web Hosting


In this tutorial, I will show you how to swipe left to right and vice versa in a tab bar. See the code below.

In the class that set on a tab index: 0, we just name it FirstClass.h and FirstClass.m on its viewdidload method. Add this code below.

 UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rates)];
    
    swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeLeft];

And add this method

- (void)rates{
    self.tabBarController.selectedIndex = 1;
    [self.tabBarController.selectedViewController.navigationController popViewControllerAnimated:YES];
}

In the class that set on a tab index: 1, we just name it SecondClass.h and SecondClass.m on its viewdidload method. Add this code below.

 UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rates)];
    
    swipeRight.direction = UISwipeGestureRecognizerDirectionLeft;
    [self.view addGestureRecognizer:swipeRight];

And add this method

- (void)rates{
    self.tabBarController.selectedIndex = 0;
    [self.tabBarController.selectedViewController.navigationController popViewControllerAnimated:YES];
}


Assuming that we have only 2 tabs in our tab bar. Note this will work on the class that extends UIViewController and not working on UITableViewCell.


Web Hosting

That's it.

Hide Keyboard

Web Hosting


In this tutorial, I will show you how to hide a keyboard in IOS with the keyboard without return button. See the code below.

//add this code at the top under implementation
UITapGestureRecognizer *tapRecognizer;

//In viewdidload add this code below

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    
    [nc addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [nc addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];


//add the following methods. Note _tf_amount is the name of the textfield.

- (void)keyboardWillShow:(NSNotification *) note{
    [self.view addGestureRecognizer:tapRecognizer];
}

- (void)keyboardWillHide:(NSNotification *) note{
    [self.view removeGestureRecognizer:tapRecognizer];
}

- (void)didTapAnywhere: (UITapGestureRecognizer *) recognizer{
    [_tf_amount resignFirstResponder];
}



Web Hosting

That's it.

Sample Delegate

Web Hosting


In this tutorial, I will show you how to create a simple delegate that passing a data in any class. In your first class, we just name it FirstClass.h and FirstClass.m. See the code below.


FirstClass.h File

//  Created by mm20 on 1/28/14.
//  Copyright (c) 2014 mm20. All rights reserved.
//

#import <UIKit/UIKit.h>


@class FirstClass;
@protocol passDelegate <NSObject>

-(void)didPass:(FirstClass *)controller nname:(NSString *)nname rname:(NSString *)rname amount:(NSString *)amount charge:(NSString *)charge total:(NSString *)total;

@end
@interface FirstClass : UIViewController

- (IBAction)btn_preview:(id)sender;
@property(nonatomic,weak)id<passDelegate>pdelegate;
@end

FirstClass.m File

//Apply this code were when you want to set the delegate. In my side, I set it inside the action button.

Note you need to import the SecondClass.h in this class. SecondClass.h is the class were you need to pass the data that you set from this class.

- (IBAction)btn_preview:(id)sender {
    

        SecondClass *sc = [self.storyboard instantiateViewControllerWithIdentifier:@"ss"];
        sc.hidesBottomBarWhenPushed = YES;
        sc.title = @"Preview";
        [self.navigationController pushViewController:sc animated:YES];
        
        self.pdelegate = sc;
        [self.pdelegate didPass:self nname:_senderName.text rname:_receiverName.text amount:_tf_amount.text charge:_lbl_charge.text total:_lbl_total.text];
    
}


SecondClass.h

#import <UIKit/UIKit.h>
#import "FirstClass.h"

@interface SecondClass : UIViewController<passDelegate>

@end


SecondClass.m

Note: Declare assigning variables at the top under implementation

- (void)didPass:(FirstClass *)controller nname:(NSString *)nname rname:(NSString *)rname amount:(NSString *)amount charge:(NSString *)charge total:(NSString *)total{
    
    
    get_sname = nname;
    get_rname = rname;
    get_amount = amount;
    get_charge = charge;
    get_total = total;
}


Web Hosting

That's it.

Hide Status Bar

Web Hosting


In this tutorial, I will show you how to hide status bar and apply animation on it, In your AppDelegate.m file attach the code. See the code below.

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

    [self hideStatusBar];
    
    return YES;

}

- (void)hideStatusBar{
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    [UIView beginAnimations:@"Statusbar hide" context:nil];
    [UIView setAnimationDuration:0.5];
    [self.view setFrame:CGRectMake(0, 0, 320, 480)];
    [UIView commitAnimations];
}


Web Hosting


That's it. Happy Coding.

Customized Tab Bar

Web Hosting


In this tutorial I will show you how to customized tab bar. In your AppDelegate.m file, add this code below.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    //customized tab bar
    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UITabBar *tabBar = tabBarController.tabBar;
    UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
    
    tabBarItem1.title = @"Send Money";
    tabBarItem2.title = @"Rates";
    
    [tabBarItem1 setFinishedSelectedImage:[UIImage imageNamed:@"tabitem_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"tabitem_default.png"]];
    [tabBarItem2 setFinishedSelectedImage:[UIImage imageNamed:@"ratestab_selected.png"] withFinishedUnselectedImage:[UIImage imageNamed:@"ratestab_default.png"]];
    
    UIImage *tabBarBackground = [UIImage imageNamed:@"tabbar_bg.png"];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];
    [[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_item.png"]];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal];
    
    UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, UITextAttributeTextColor, nil] forState:UIControlStateHighlighted];
    end customizing tabBar
    
    
    return YES;

}



Web Hosting

That's it. Hope this tutorial help you.