Some iOS development tricks
1. How to create transparent UIBarButtonItem
- 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];
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
- UIImageView *brosurkuLogo = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"brosurku.jpg"]];
- [brosurkuLogo setFrame:CGRectMake(0, 0, 64, 36)];
- UIBarButtonItem *brosurkuBarButton = [[UIBarButtonItemalloc] initWithCustomView:brosurkuLogo];
- [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
- [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
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- [application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
- }
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[application setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
}5. How to change window pattern
- + (UIWindow *) customizeWindow: (UIWindow *) window withTexture: (NSString *) filename {
- [window setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: filename]]];
- return window;
- }
+ (UIWindow *) customizeWindow: (UIWindow *) window withTexture: (NSString *) filename {
[window setBackgroundColor: [UIColor colorWithPatternImage: [UIImage imageNamed: filename]]];
return window;
}6. How to create custom searchbar
- 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];
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
- //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];
- }
//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
- [navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
- [navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
[navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque]; [navigationController.navigationBar setBarStyle:UIBarStyleBlackOpaque];
9. How to create button with custom font
- //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];
//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];


Here is what the UITextField would look like after we update these properties.
Here are some screenshots of the text shrinking when typing in the UITextField.
Recent Comments