How To Customize UINavBar? - iphone

I am using the following code to add a custom navbar to my app. But I want to be able to change the navbar depending on the view, is this possible?
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
UIImage *img = [UIImage imageNamed: #"navbar.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
If not, is it possible to just add an image to the navbar instead of title text?

I would strongly discourage using a category to override -drawRect on a UINavBar as this design will break on a soon to be released iOS version.
It is very easy to create custom images and buttons to the navBar title though. Here's some code snippets I pulled directly out of production code for one of my apps. It creates a custom image button in the navBar's title area, but it's just as easy to create an image instead:
- (void) viewDidLoad {
UIButton *titleButton = [self buttonForTitleView];
[titleButton setTitle:newTitle forState:UIControlStateNormal];
[titleButton sizeToFit];
self.navigationBar.topItem.titleView = titleButton;
}
- (UIButton *) buttonForTitleView {
if (!_buttonForTitleView) {
UIImage *buttonImage = [UIImage imageNamed:#"button_collection_name2"];
UIImage *pressedButtonImage = [UIImage imageNamed:#"button_collection_name2_pressed"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: buttonImage forState : UIControlStateNormal];
[button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
[button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumFontSize = 10.0;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
[button addTarget:self action:#selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
_buttonForTitleView = [button retain];
}
return _buttonForTitleView;
}
And this is what the button looks like in the app VideoBot:
Here's an example of using an image for the navbar title:
UIImage *titleImage = [UIImage imageNamed:#"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];
UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];
// add the view to the title
self.navigationBar.topItem.titleView= container;

Related

Back Button can't be seen on navigation controller in IOS6

I have an customised UINavigationcontroller created by me programatically.
The Problem is with the back button which comes as a default in UINavigationBar is not seen in IOS6 but when i press it the action can be done.
NOTE: The back button is seen in IOS5.
Here is my code that i had used
- (void)customizeNavigationController:(UINavigationController *)navController
{
UINavigationBar *navBar = [navController navigationBar];
[navBar setTintColor:keyNavBarTintColor];
UIImageView *myImageView = (UIImageView *)[navBar viewWithTag:keyNavBarBackgroundImageTag];
if (myImageView == nil)
{
UIImage *img = [UIImage imageNamed:#"image.png"];
CGRect rect = CGRectMake(0, 0, navBar.frame.size.width, navBar.frame.size.height);
myImageView = [[UIImageView alloc] initWithFrame:rect];
[myImageView setContentMode:UIViewContentModeScaleAspectFill];
[myImageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
myImageView.image = img;
[myImageView setTag:keyNavBarBackgroundImageTag];
[navBar addSubview:myImageView];
[myImageView release];
}
self.navImageView = myImageView;
}
Try this:
UIImage *image = [UIImage imageNamed:kButtonBackInActive];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 39.0f, 44.0f)];
button.contentEdgeInsets = (UIEdgeInsets){.left=-8};
button.showsTouchWhenHighlighted = NO;
UIBarButtonItem * backbutton = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
[self.navigationItem setLeftBarButtonItem:backbutton];

How to create a constructor for a custom UIButton class?

I have created a subclass of the UIButton class. I want to change its background color upon the creation. How do I write the constructor method?
-(id)initWithFrame :(CGRect)frame{
if(self = [super init]) {
// The default size for the save button is 49x30 pixels
self.frame = frame;//CGRectMake(0, 0, 60.0, 34.0);
// Center the text vertically and horizontally
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
UIImage *image = [UIImage imageNamed:#"image.png"];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setBackgroundColor:[UIColor blueColor]];
// Set the font properties
// [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:12];
}
return self;
}

UIButton is not responding when added as footerview

My button is not responding to touch events, please find the code snippet as below.
I have added UIButton to UIView and set the UIView to UITableView's footerview.
Please let me know and thanks.
CGRect tblViewFrame = CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, 300);
self.tblViewSettings = [[[UITableView alloc]initWithFrame:tblViewFrame style:UITableViewStyleGrouped]autorelease];
self.tblViewSettings.backgroundColor = [UIColor clearColor];
self.tblViewSettings.showsVerticalScrollIndicator = FALSE;
self.tblViewSettings.delegate = self;
self.tblViewSettings.dataSource = self;
self.tblViewSettings.scrollEnabled = YES;
// Set the background color
self.view.backgroundColor = [UIColor clearColor];
UIView *buttonView = [[[UIView alloc]initWithFrame:CGRectMake(10,10, 320, 60)]autorelease];
self.signoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.signoutButton.frame = CGRectMake(10,10, 300, 40);
UIImage *image = [[UIImage imageNamed:#"btn-large1.png"] stretchableImageWithLeftCapWidth:22 topCapHeight:0];
NSString *strSignOut = NSLocalizedString(#"Sign Out", #"SignOut Button");
[self.signoutButton setTitle:strSignOut forState:UIControlStateNormal];
[self.signoutButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
self.signoutButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
[self.signoutButton setBackgroundImage:image forState:UIControlStateNormal];
self.signoutButton.titleLabel.font = [UIFont boldSystemFontOfSize:20.0];
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
buttonView.backgroundColor = [UIColor clearColor];
[buttonView addSubview:self.signoutButton];
buttonView.userInteractionEnabled = YES;
//[self.view addSubview:buttonView];
self.tblViewSettings.tableFooterView = buttonView;
self.tblViewSettings.tableFooterView.userInteractionEnabled = YES;
[self.view addSubview:self.tblViewSettings];
-(IBAction)signOutBtnTapped:(id)sender{
}
I have looked at your code and applied it to my project. actually it works. i did not change anything from your code. i guess u must check out the frames of table view and the bottom view as everything else is working fine.
Try capturing the event for UIControlEventTouchDown instead of UIControlEventTouchUpInside in
[self.signoutButton addTarget:self action:#selector(signOutBtnTapped:) forControlEvents: UIControlEventTouchUpInside];
Everything else seems OK.
for Adding button in Footer view Section
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView* customView;
customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 60.0)];
UIButton *Login_Btn = [UIButton buttonWithType:UIButtonTypeCustom];
[Login_Btn setImage:[UIImage imageNamed:#"btn_login.png"] forState:UIControlStateNormal];
Login_Btn.frame = kiPhoneButtonFrame;
[customView addSubview:Login_Btn];
[Login_Btn addTarget:self
action:#selector(Login_Btn_Clicked:)
forControlEvents:UIControlEventTouchUpInside];
return customView;
}
And Button Click Method
-(IBAction)Login_Btn_Clicked:(id)sender
{
// Code for button CLick which you want to do.
}

combining UISegmentedControl and UIBarButtonItem in navigation controller to customized barbuttonitem background image

I have a barbuttonitem that made programmatically above a navigation controller. I want to display it's title and background also a highlighted effect when i press that bar button.
Here's the code I use :
NSArray *segmentText = [segmentTextMutable copy];
UIImage *image = [[[UIImage alloc] init] autorelease];
image = [UIImage imageNamed:#"bunga.jpg"];
_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0;
_docSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;
[_docSegmentedControl addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];
[_docSegmentedControl setBackgroundColor:[UIColor colorWithPatternImage:image]];
UIView *barBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"button.png"];
UIImage *buttonPressedImage = [UIImage imageNamed:#"buttonPressed.png"];
[[barButton titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[barButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[barButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[barButton setTitleShadowColor:[UIColor colorWithWhite:1.0 alpha:0.7] forState:UIControlStateNormal];
[barButton setTitleShadowColor:[UIColor clearColor] forState:UIControlStateHighlighted];
[[barButton titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];
CGRect buttonFrame = CGRectMake(0, 0, 110.0, 40);
//buttonFrame.size.width = 110.0;
//buttonFrame.size.height = buttonFrame.size.height;
[barButton setFrame:buttonFrame];
[barButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[barButton setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];
[barButton setTitle:docSegmentFileName forState:UIControlStateNormal];
[barButton addTarget:self action:#selector(docSegmentAction:) forControlEvents:UIControlEventTouchUpInside];
[barBackground addSubview:barButton];
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = #"";
[self.navItem.leftBarButtonItem setCustomView:barBackground];
Unfortunately, this doesn't work. Instead of showing the UIBarButtonItem, it simply vanishes (it becomes 100% transparant). When I leave out the setCustomView method, the UIBarButtonItem appears, but is not customized. How can i solve this problem?
thank's...
You are removing the UISegmentedControl from the UIBarButtonItem when you call [self.navItem.leftBarButtonItem setCustomView:barBackground];. You need to modify the UISegmentedControl not the UIBarButtonItem.

Custom UIButton for Iphone

I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons and clicking on each button should call a different person.
For the above purpose I created a custom button class deriving from UIButton.
#implementation HopitalButton
#synthesize index;
#synthesize button_type;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImage* img = [UIImage imageNamed:#"dr_btn.png"];
[img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[self setBackgroundImage:img forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
[self setFont:[UIFont fontWithName:#"Helvetica Bold" size:13]];
self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#end
Now the problem with the above code is that it does not create buttons that look similar to the buttons created by default in Interface builder. The borders are missing.
And I create buttons of the above type by the following code:
HopitalButton* hb = [[HopitalButton alloc] init];
hb.button_type = #"call";
hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
[self.scroll_view addSubview:hb];
[hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:#"Staff" ]objectForKey:#"FullName"] forState:UIControlStateNormal];
hb.index = [NSNumber numberWithInt:[self.button_items count]];
[self.button_items insertObject:hb atIndex:[self.button_items count]];
[hb addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
I am not finding a way to set the button type for this custom button.
Is there a way i can do it ? Or is there a better way to design the code.
You first start with a stretchable image with a border:
alt text http://grab.by/4lP
Then you make a button with the stretched image as the background and apply text.
INDEX_OFFSET = 82753; // random
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setTag:<INDEX>+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title. And
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
setFont is now deprecated, use titleLabel.font property instead
sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
you can use individual class for custom Roundrect button which can be useful in whole project with your specific frame style as below
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface CustomRoundRectButton : UIButton
#end
#import "CustomRoundRectButton.h"
#implementation CustomRoundRectButton
- (void)drawRect:(CGRect)rect
{
[[self layer] setMasksToBounds:YES];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor grayColor].CGColor];
[self.layer setBorderWidth:1.0];
}
#end
In this you have to select button type custom and select its class as CustomRoundRectButton.
For Simple custom button we can use as below
-(UIBarButtonItem*)BackButton
{
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 30, 30)];
[btn addTarget:self action:#selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
return barBtn;
}
Shouldn't you be calling initWithFrame: rect instead of:
HopitalButton* hb = [[HopitalButton alloc] init];