Saturday, March 3, 2012

Dismiss the keyboard when pressing the "Return" key

1. Except to use the IB to change the "Return" to be "Done" for the keyboard, it can be done in code as below.


 [myTextField setReturnKeyType:UIReturnKeyDone];

2. Dimiss the keyboard after pressing "Return" button for textfield, link this method to the "Did End on Exit" Event in the textfield. Remember to add UITextViewDelegate in .h file.

// dismisses the keyboard when a user selects the return key
- (IBAction)textFieldShouldReturn:(id)sender
{
    [sender resignFirstResponder];

    
}

3. If you want to dismiss the keyboard in the myTextView, please find the below code.

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if([text isEqualToString:@"\n"]) {
        [myTextView resignFirstResponder];
        return NO;
    }
    
    return YES;
}
Besides it, you need to add UITextViewDelegate at .h file, also link delegate to file owner in reference outlet.

Custom navigation bar and toolbar

The below code can custom the font type on the navigation bar, make both navigation bar and toolbar to be translucent style, set up the left button to be "Back" in the next view.


// set navigation bar to be translucent style
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    
// set custom font and size in the navigation bar
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
    label.font = [UIFont fontWithName:@"Marker Felt" size: 22.0];
    label.BackgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = self.title;
    [self.navigationController.navigationBar.topItem setTitleView:label];
    [label release];
    
// set the left button to be "Back"
    UIBarButtonItem *backBtn = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBtn;
    [backBtn release];

// set toolbar to be translucent style
    myToolbar.barStyle = UIBarStyleDefault;
    myToolbar.tintColor = [UIColor colorWithWhite:0.0 alpha:0.5];
    myToolbar.translucent = YES;



Saturday, February 25, 2012

Add effect to the rounded button; create the customed button

If you want to see the color change on the button in the normal stage and button-pressed stage, you may add the below code in the viewDidLoad.



 //create the button to clear Screen
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(clear:) forControlEvents:UIControlEventTouchUpInside];
    
    //set the position of the button
    button.frame = CGRectMake(0, 0, 150, 40);
    button.center = CGPointMake(160, 240);
    
    //set the button's title
    [button setTitle:@"Clear!" forState:UIControlStateNormal];
    
    //set button effect
    UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *stretchableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImageNormal forState:UIControlStateNormal];
    
    UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
    UIImage *stretchableButtonImagePressed = [buttonImagePressed   stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [button setBackgroundImage:stretchableButtonImagePressed forState:UIControlStateHighlighted];
    
    //add the button to the view
    [self.view addSubview:button];


 whiteButton.png

 blueButton.png
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++


The following code create the customed button with own image.



UIImage *buttonImage = [UIImage imageNamed:@"cancel.png"];

    UIbutton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(280.0, 10.0, 29.0, 29.0);
    [button setBackgroundImage:buttonImage forState:UIControlStateNormal];
    [button addTarget:self action:@selector(clearScreen:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];


Friday, February 24, 2012

Add border to the imageView and textView

Add the border and maker the rounded corner to the imageView and textView:


in .h file
         #import <QuartzCore/QuartzCore.h>
        

in .m file
        // adding border to the imageView
        [theImageView.layer setBorderColor: [[UIColor blackColor] CGColor]];
        [theImageView.layer setBorderWidth: 1.0];

         theImageView.layer.cornerRadius = 9.0;
         theImageView.layer.masksToBounds = YES;
        
        
        // resize the imageView to fit the image size
        CGSize size = [image size];
        float factor = size.width / self.frame.size.width;
        if (factor < size.height / self.frame.size.height) {
            factor = size.height / self.frame.size.height;
        }
        
        CGRect rect = CGRectMake(0, 0, size.width/factor, size.height/factor);
        theImageView.frame = rect;

Thursday, February 23, 2012


Even you had valid icon in PNG.format along with its size (57x57), when you upload the binary to iTunes Connect, you may receive the following error message.


iPhone/iPod Touch: XXX.png: icon dimensions (0 x 0) don't meet the size requirements. The icon file must be 57x57 pixels, in .png format.


The solution is as below for Xcode 4.2.


Edit Project -> Build setting -> Packaging -> set 'NO' to Compress PNG Files

MAC - access folders that contain space in Terminal, e.g. My Folder

1. go to particular directory, cd "My Folder" or

2. at the root directory, cd + space, then drag the folder icon from the Finder into the Terminal window.