0

Some iOS development tricks

-

1. How to create transparent UIBarButtonItem

  1. UIImage *reloadImage = [UIImageimageNamed:@"refresh_button88.png"];
  2. UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
  3. [reloadButton setBackgroundImage:reloadImage forState:UIControlStateNormal];
  4. [reloadButton setFrame:CGRectMake(0, 0, 44, 44)];
  5. UIBarButtonItem *reloadBarButton = [[UIBarButtonItemalloc] initWithCustomView:reloadButton];
  6.  
  7. self.navigationItemsetLeftBarButtonItem:reloadBarButton];
UIImage *reloadImage = [UIImageimageNamed:@"refresh_button88.png"];
UIButton *reloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
[reloadButton setBackgroundImage:reloadImage forState:UIControlStateNormal]; 
[reloadButton setFrame:CGRectMake(0, 0, 44, 44)];
UIBarButtonItem *reloadBarButton = [[UIBarButtonItemalloc] initWithCustomView:reloadButton];

self.navigationItemsetLeftBarButtonItem:reloadBarButton];

2. How to display transparent image in UIBarButtonItem

  1. UIImageView *brosurkuLogo = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"brosurku.jpg"]];
  2. [brosurkuLogo setFrame:CGRectMake(0, 0, 64, 36)];
  3. UIBarButtonItem *brosurkuBarButton = [[UIBarButtonItemalloc] initWithCustomView:brosurkuLogo];
  4. [self.navigationItemsetRightBarButtonItem:brosurkuBarButton];
UIImageView *brosurkuLogo = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"brosurku.jpg"]];
[brosurkuLogo setFrame:CGRectMake(0, 0, 64, 36)];
UIBarButtonItem *brosurkuBarButton = [[UIBarButtonItemalloc] initWithCustomView:brosurkuLogo];
[self.navigationItemsetRightBarButtonItem:brosurkuBarButton];

3. How to change background on UINavigationBar

  1. [navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bar.png"] forBarMetrics:UIBarMetricsDefault];
[navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header_bar.png"] forBarMetrics:UIBarMetricsDefault];

4. How to change statusbar color

  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  2. {
  3. [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
  4. }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
}

5. How to change window pattern

  1. + (UIWindow *) customizeWindow: (UIWindow *) window withTexture: (NSString *) filename {
  2. [window setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: filename]]];
  3. return window;
  4. }
+ (UIWindow *) customizeWindow: (UIWindow *) window withTexture: (NSString *) filename {
[window setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: filename]]];
return window;
}

6. How to create custom searchbar

  1. searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  2. [searchBar setPlaceholder:@"Search in Plaza Lapiazza, Jakarta Raya, Indonesia"];
  3.  
  4. //to remove searchbar default background layer
  5. if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UIImageViewclass]]){
  6. [[[searchBar subviews] objectAtIndex:0] removeFromSuperview];
  7. }
  8.  
  9. //to remove search icon. Note that objectAtIndex = 0 because background layer is already removed
  10. UITextField *textField;
  11. if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UITextFieldclass]]){
  12. textField = [[searchBarsubviews] objectAtIndex:0];
  13. }
  14. textField.leftView = nil;
  15.  
  16. //to change textField frame
  17. UIImage *searchBarImage = [[UIImage imageNamed:@"search_bar_fit.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
  18. [textField setBackground:searchBarImage];
  19.  
  20. //to change textfield font and size
  21. [textField setFont:[UIFontfontWithName:@"HelveticaNeue-Light" size:15]];\
  22.  
  23. //to add transparent background on searchbar
  24. [searchBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"search_bottom.png"]]];
  25. [[searchBar layer] setOpaque:NO];
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[searchBar setPlaceholder:@"Search in Plaza Lapiazza, Jakarta Raya, Indonesia"];

//to remove searchbar default background layer
if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UIImageViewclass]]){
[[[searchBar subviews] objectAtIndex:0] removeFromSuperview];
}

//to remove search icon. Note that objectAtIndex = 0 because background layer is already removed
UITextField *textField;
if ([[[searchBar subviews] objectAtIndex:0] isKindOfClass:[UITextFieldclass]]){
textField = [[searchBarsubviews] objectAtIndex:0];
}
textField.leftView = nil;

//to change textField frame
UIImage *searchBarImage = [[UIImage imageNamed:@"search_bar_fit.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 0, 10)];
[textField setBackground:searchBarImage];

//to change textfield font and size
[textField setFont:[UIFontfontWithName:@"HelveticaNeue-Light" size:15]];\

//to add transparent background on searchbar
[searchBar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"search_bottom.png"]]];
[[searchBar layer] setOpaque:NO];

7. How to add toolbar

  1. //create button with custom font
  2. UIButton *categoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
  3. [categoryButton setFrame:CGRectMake(10, 0, 300, 44)];
  4. [categoryButton.titleLabel setFont:[UIFontfontWithName:@"HelveticaNeue-Bold" size:15]];
  5. [categoryButton setTitle:@"CATEGORY" forState:UIControlStateNormal];
  6. [categoryButton addTarget:self action:@selector(toggleCategory) forControlEvents:UIControlEventTouchDown];
  7.  
  8. //to add item in toolbar in this viewcontroller
  9. UIBarButtonItem *toolBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:categoryButton];
  10. [selfsetToolbarItems:[NSArray arrayWithObject:toolBarButtonItem]];
  11.  
  12. //to display toolbar with custom height
  13. //you must called these two method in viewWillAppear and viewWillDisappear if you only want to display in some view controller only
  14. [self.navigationController setToolbarHidden:YES animated:YES];
  15.  
  16. [self.navigationController.toolbarsetFrame:CGRectMake(0, 450, 320, 30)];
  17.  
  18. //to display toolbar in some view controller only
  19. - (void)viewWillAppear:(BOOL)animated{
  20. [super viewWillAppear:animated];
  21. [self.navigationController setToolbarHidden:NOanimated:YES];
  22. [self.navigationController.toolbarsetFrame:CGRectMake(0, 450, 320, 30)];
  23. }
  24.  
  25. - (void)viewWillDisappear:(BOOL)animated{
  26. [super viewWillDisappear:animated];
  27. [self.navigationController setToolbarHidden:YESanimated:YES];
  28. }
//create button with custom font
UIButton *categoryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[categoryButton setFrame:CGRectMake(10, 0, 300, 44)];
[categoryButton.titleLabel setFont:[UIFontfontWithName:@"HelveticaNeue-Bold" size:15]];
[categoryButton setTitle:@"CATEGORY" forState:UIControlStateNormal];
[categoryButton addTarget:self action:@selector(toggleCategory) forControlEvents:UIControlEventTouchDown];

//to add item in toolbar in this viewcontroller
UIBarButtonItem *toolBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:categoryButton];
[selfsetToolbarItems:[NSArray arrayWithObject:toolBarButtonItem]];

//to display toolbar with custom height
//you must called these two method in viewWillAppear and viewWillDisappear if you only want to display in some view controller only
[self.navigationController setToolbarHidden:YES animated:YES];

[self.navigationController.toolbarsetFrame:CGRectMake(0, 450, 320, 30)];

//to display toolbar in some view controller only
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController setToolbarHidden:NOanimated:YES];
[self.navigationController.toolbarsetFrame:CGRectMake(0, 450, 320, 30)];
}

- (void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[self.navigationController setToolbarHidden:YESanimated:YES];
}

8. How to change barStyle to black

  1. [navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
  2. [navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
[navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];

9. How to create button with custom font

  1. //create button with custom font
  2. //titleLabel is read only property, however you can still set font, shadowOffset, and lineBreakMode
  3. UIButton *categoryButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
  4. [categoryButton setFrame:CGRectMake(10, 0, 300, 44)];
  5. [categoryButton.titleLabel setFont:[UIFontfontWithName:@"HelveticaNeue-Bold" size:15]];
  6. [categoryButton setTitle:@"CATEGORY" forState:UIControlStateNormal];
  7. [categoryButton addTarget:selfaction:@selector(toggleCategory) forControlEvents:UIControlEventTouchDown];
//create button with custom font
//titleLabel is read only property, however you can still set font, shadowOffset, and lineBreakMode
UIButton *categoryButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
[categoryButton setFrame:CGRectMake(10, 0, 300, 44)];
[categoryButton.titleLabel setFont:[UIFontfontWithName:@"HelveticaNeue-Bold" size:15]];
[categoryButton setTitle:@"CATEGORY" forState:UIControlStateNormal];
[categoryButton addTarget:selfaction:@selector(toggleCategory) forControlEvents:UIControlEventTouchDown];
1

iOS Open Source : Custom Progress Indicator

-

DDProgressView, written by Damien DeVille, is an easy to implement control for showing an animated progress indicator. There are three customizable values in this control, the inner color, outer color and the color of the progress indicator when empty (essentially the background color). To move the indicator forward or backward, you update a value (a float) in the control, which affects how the control is drawn.

In the screenshot above, the top progess view has an outer color of cyan and an inner color that is blue. The second bar has an outer color that is defined as [UIColor clearColor]], an inner color that is light gray and empty color that is white.

The example included with DDProgressView – which is the basis of the screenshots above – demonstrates how to create two controls, set their color values and update the progress indicator using an NSTimer with a scheduled timer interval.

Download DDProgressView

You can download DDProgressView from github.

0

Installing 4.3 iPhone Simulator in Xcode 4.2 (iOS5 SDK)

-

After having used all the betas of Xcode 4.2 and the iOS5 SDK I faced several problems when finally I downloaded the official Xcode 4.2 from the App Store. I wanted to anyways get rid of Xcode 3.x, 4.1 and so on (I kept them all installed around my hard drive) so I deleted and installed a fresh copy of Xcode 4.2 (btw I’m very pleased with the progress Apple made on Xcode’s stability and feature list).

However a fresh install of Xcode 4.2 comes with only a 5.0 iPhone and iPad Simulator. Luckily you can now pretty easy install also older version of the SDK. Just follow the easy steps below:

1. Simulator 5.0 only, but how to test whether your app is compatible with iOS 4.3?

2. Aha!, click on “More Simulators…”

3. Click “Install” on the row where it says “iOS 4.3 Simulator”

4. After the 4.3 Simulator is downloaded you’ll most probably see this alert (if you have the Simulator app already running)

5. Immediately after the install is done have a look at the list of available schemes in your project and …

That’s all – you can now test in both iOS 5.0 and iOS 4.3

All in all it’s about safe to support only 5.x when there’s a 5.1 release, by that time usually enough people have upgraded their devices (not counting the jail breakers of course, they update when there’s the relevant jail-break, but they are less likely to buy your app anyway…)

That’s it for today, more iOS5 posts to come.

0

Share and Copy Files Between an App and iTunes

-

You can share files between an iOS app and your Mac using file sharing via iTunes. Using file sharing the contents of your application’s Documents directory is available in iTunes. This also works the other way, where you can place files from your Mac into the shared area of iTunes and make them available to your iOS app.

This feature works well if you need to log information from within your app and make the log available for offline viewing at another time. I’ve used idea this a number of times to save debugging information for later viewing.

UIFileSharingEnabled

The first step is to add the flag UIFileSharingEnabled to the application plist file. If you edit the plist as XML, you can add the flag as shown here:

<key>UIFileSharingEnabled</key>
<true/>

Using the plist editor, the entry is as shown below:

Write a file to Document Directory

With the property file updated, the next step is to write a file to the Documents directory within the app. The code below creates an array as well as a dictionary object and writes both containers to a file named DictionaryContents.text.

  1. - (void) writeFileToDocumentDirectory
  2. {
  3.   NSArray *grains = [NSArray arrayWithObjects:
  4.                     @"Caramunich", @"Hallertauer", @"Special B", nil];
  5.  
  6.   NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
  7.                              grains, @"arrayKey", @"Magnum", @"hopKey", @"Belgian Strong", @"yeastKey", nil];
  8.  
  9.   // Get path to documents directory
  10.   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
  11.                                                        NSUserDomainMask, YES);
  12.   if ([paths count] > 0)
  13.   {
  14.     // Path to save dictionary
  15.     NSString  *dictPath = [[paths objectAtIndex:0]
  16.                            stringByAppendingPathComponent:@"DictionaryContents.text"];
  17.     // Write dictionary
  18.     [dictionary writeToFile:dictPath atomically:YES];
  19.   }
  20. }
- (void) writeFileToDocumentDirectory
{
  NSArray *grains = [NSArray arrayWithObjects:
                    @"Caramunich", @"Hallertauer", @"Special B", nil];

  NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                             grains, @"arrayKey", @"Magnum", @"hopKey", @"Belgian Strong", @"yeastKey", nil];

  // Get path to documents directory
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
  if ([paths count] > 0)
  {
    // Path to save dictionary
    NSString  *dictPath = [[paths objectAtIndex:0]
                           stringByAppendingPathComponent:@"DictionaryContents.text"];
    // Write dictionary
    [dictionary writeToFile:dictPath atomically:YES];
  }
}

Call the method above somewhere in your application to write the file to the Documentsdirectory.

iTunes File Sharing

To view the file within iTunes, plugin your device and select your device in the DEVICES section on the left:

Choose the Apps button on the top of iTunes and scroll to the bottom of the screen, you will see a list of all the apps that can transfer files between your computer and your device.

Save the file DictionaryContents.txt to your system, the contents will look as follows:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5.   <key>arrayKey</key>
  6.   <array>
  7.     <string>Caramunich</string>
  8.     <string>Hallertauer</string>
  9.     <string>Special B</string>
  10.   </array>
  11.   <key>hopKey</key>
  12.   <string>Magnum</string>
  13.   <key>yeastKey</key>
  14.   <string>Belgian Strong</string>
  15. </dict>
  16. </plist>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>arrayKey</key>
  <array>
    <string>Caramunich</string>
    <string>Hallertauer</string>
    <string>Special B</string>
  </array>
  <key>hopKey</key>
  <string>Magnum</string>
  <key>yeastKey</key>
  <string>Belgian Strong</string>
</dict>
</plist>
Copy Files To An App

You can also copy files from your system to the Documents directory, making the files accessible to your app – drag/drop files onto the Sandbox Documents area.

Keeping Data Private

- If you need to keep application information private, do not store the files in the Documentsdirectory. Instead, store private information in the Library directory, or a folder within that directory. With one exception, the Library directory and all its sub-directories will be preserved across application backups and updates – /Library/Caches is not saved.

- With file sharing, you cannot share files between applications.