In my detail view controller (part of navigation controller application) I've added custom "Back" button, like this:
- (void)loadView
{
[super loadView];
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 10.0f, 24.0f)];
UIImage *backImage = [UIImage imageNamed:#"left_arrow_icon"];
[backButton setBackgroundImage:backImage forState:UIControlStateNormal];
[backButton setTitle:#"" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(popBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backButtonItem;
}
-(void) popBack {
[self.navigationController popViewControllerAnimated:YES];
}
As you can see I added left bar button and assign "popBack" action. Basically left_arrow_icon is silver but when I press it, iOS change it darker grey.
My question is, can I (and how) change initial colour to white? Is that possible?
Edit. I use xcode5
Edit2:
That does't work too
Try this code. It works perfectly for me -
UIImageView *navigationBarImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Navigation.png"]];
navigationBarImage.frame = CGRectMake(0, 0, 320, 44);
navigationBarImage.userInteractionEnabled = YES;
navigationBarImage.backgroundColor = [UIColor colorWithRed:28.0/255.0 green:53.0/255.0 blue:102.0/255.0 alpha:1.0];
[self.view navigationBarImage];
[navigationBarImage release];
UIButton *backBarBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
backBarBtn1.frame = CGRectMake(13, 12, 20, 20);
[backBarBtn1 setImage:[UIImage imageNamed:#"ic_back3.png"] forState:UIControlStateNormal];
backBarBtn1.backgroundColor = [UIColor clearColor];
[backBarBtn1 addTarget:self action:#selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:backBarBtn1];
Try this code -
[backbutton setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], UITextAttributeTextColor,
[UIColor blackColor], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:HELVETICA_REGULAR_FONT size:17.0], UITextAttributeFont, nil] forState:UIControlStateNormal];
Related
I have implement custom back Navigation bar button.
Codes:
-(UIBarButtonItem*) logicToAddBackButton {
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"UiNavigationBack"]];
UILabel *label=[[UILabel alloc] init];
[label setTextColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0]];
[label setText:#"Home"];
[label sizeToFit];
int space=6;
label.frame=CGRectMake(imageView.frame.origin.x+imageView.frame.size.width+space, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, label.frame.size.width+imageView.frame.size.width+space, imageView.frame.size.height)];
view.bounds=CGRectMake(view.bounds.origin.x+8, view.bounds.origin.y-1, view.bounds.size.width, view.bounds.size.height);
[view addSubview:imageView];
[view addSubview:label];
UIButton *button=[[UIButton alloc] initWithFrame:view.frame];
[button addTarget:self action:#selector(eventBack) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:button];
[UIView animateWithDuration:0.33 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
label.alpha = 0.0;
CGRect orig=label.frame;
label.frame=CGRectMake(label.frame.origin.x+25, label.frame.origin.y, label.frame.size.width, label.frame.size.height);
label.alpha = 1.0;
label.frame=orig;
} completion:nil];
UIBarButtonItem *backButton =[[UIBarButtonItem alloc] initWithCustomView:view];
return backButton;
}
self.navigationItem.leftBarButtonItem = [self logicToAddBackButton];
This is how it look and work fine according to the logic.
Issue: If we click on first half of arrow, the back button do not respond.
Please suggest on this.
Try to set to your button:
button.contentEdgeInsets = UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
I think you miscalculating frame somewhere.
Why don't you add UIButton instead? It will be much easier, but without label animation.
UIButton *button = [[UIButton alloc] init];
[button addTarget:self action:#selector(eventBack:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Home" forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"UiNavigationBack"] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:0.0 green:122.0/255.0 blue:1.0 alpha:1.0] forState:UIControlStateNormal];
[button sizeToFit];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button];
return backButton;
I m trying to setting image for UIBarButtonItem using the below code. But it appears as a blank image in the navigation bar. please help me to resolve the problem. i m entirely new to ios.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];
thank u in advance.......
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(popToBack)]; //create the left bar button
self.navigationItem.leftBarButtonItem = leftBarButton; //assign into navigation item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; //set the tint color
[leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //text color
-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
in another choice
UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:#selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;
-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
You do not need to use the navigation bar directly. Use the current view controller's navigation item property to set the UIBarButtomItems. Make sure you are actually using a navigationController to set these properties.
UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:#"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:#selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;
I want to add a back arrow to a leftBarButtonItem, to make it appear visually as if it was a regular back button, although it's functionality is slightly different.
Is there a way to do this?
You can use custom button and hide the back button if you are using navigation controller.
To hide back button use this:
self.navigationItem.hidesBackButton = YES;
And for Custom Button:
UIButton * customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundColor:[UIColor colorWithRed:197.0/255.0 green:190.0/255.0 blue:157.0/255.0 alpha:1.0]];
[customButton setTitle:#"Do Something" forState:UIControlStateNormal];
customButton.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:11.0f];
[customButton.layer setCornerRadius:3.0f];
[customButton.layer setMasksToBounds:YES];
[customButton.layer setBorderWidth:1.0f];
[customButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
customButton.frame=CGRectMake(0.0, 100.0, 50.0, 25.0);
[customButton addTarget:self action:#selector(customMethod:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * customItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
customItem.tintColor=[UIColor blackColor];
self.navigationItem.leftBarButtonItem = customItem;
This will work for you.
Happy Coding
If you are using UINavigationController and then push new viewcontroller it will automatically show back button.
If you are Not Using UINavigationController than
Use Custom UIButton for that with back image
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 48, 37);
[backButton addTarget:self action:#selector(backButtonTapped) forControlEvents:UIControlEventTouchUpInside];
backButton.showsTouchWhenHighlighted = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"back-button.png"];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = backBarButtonItem;
You can change the value for the frame and the imageEdgeInsets as per your requirements.
Refer this SO Answer.
iPad app:I've add some customized button to navigationBar as follows:
- (void)addEditButton{
UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.frame = CGRectMake(0, 0, 50, 30);
[editButton setBackgroundImage:[[UIImage imageNamed:#"images_iPad.bundle/NavigationBar_Btn_iPad.png"] stretchableImageWithLeftCapWidth:15/2.0 topCapHeight:32/2.0] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editAction:) forControlEvents:UIControlEventTouchUpInside];
[editButton setTitle:#"Edit" forState:UIControlStateNormal];
editButton.titleLabel.font = [UIFont fontWithName:kAPPBOLDFONT size:12];
editButton.titleLabel.shadowColor = [UIColor blackColor];
editButton.titleLabel.shadowOffset = CGSizeMake(0, -1);
UIBarButtonItem *editItem = [[[UIBarButtonItem alloc] initWithCustomView:editButton] autorelease];
self.navigationItem.rightBarButtonItem = editItem;
}
My problem is that:I clicked this button,it will push to another viewController, after popModelViewController:animate,I can't see this customized button, although this, it does work.and not just here ,others are the same,please help me.
I have a problem. I use a Button as a BarButtonItem. It works fine so far, but my backgroundcolor works only if I click on my button. How can I make it so that my backgroundcolor will be set every time ?
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:#"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:#selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];
You can use this line of code for your navigation bar
[self.navigationcontroller.navigationbar setTintColor:[UIColor bluecolor]];
for button use
navigation button there.
Try to use this button instead:
UIBarButtonItem* emergencyButton = [[UIBarButtonItem alloc]initWithTitle:#"Emergency" style:UIBarButtonItemStyleBordered target:self action:#selector(doEmergency)];
[emergencyButton setTintColor: [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
UIButtonTypeRoundedRect is deprecated in iOS7, Use UIButtonTypeCustom instead for your custom bgColor.
UIButton *redEmergencyButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
redEmergencyButton.frame = CGRectMake(100, 100, 100, 50);
redEmergencyButton.backgroundColor = [UIColor colorWithRed:0.8
green:0.898039215686274509803
blue:1.0
alpha:1.0];
[redEmergencyButton setTitle:#"Emergency"
forState:UIControlStateNormal];
[redEmergencyButton addTarget:self
action:#selector(doEmergency)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rButton = [[UIBarButtonItem alloc]
initWithCustomView:redEmergencyButton];