Custom navigation bar back button hit area - ios

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;

Related

UINavigationController centered button using obj-c

I'm trying to add a centered button in my navbar header, confused as to how I can do it
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIBarButtonItem *meButton = [[UIBarButtonItem alloc] initWithImage:meImage style:UIBarButtonItemStylePlain target:self action:#selector(twitterButton)];
[self.titleView //i read this can be used to like set it but i havent been able to figure it out
help would be appreciated, pretty new
check this code
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
container.backgroundColor = [UIColor clearColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(0, 0, 44, 44)];
[btn setBackgroundImage:[UIImage imageNamed:#"button0.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
[btn setShowsTouchWhenHighlighted:YES];
[buttonContainer addSubview:button0];
//add your spacer images and button1 and button2...
self.navigationItem.titleView = container;
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender {
NSLog(#"button clicked");
}
I think its pretty straight, you can use titleView property of UINavigationItem :
-(void)addCenterButton {
UIImage *meImage = [[UIImage alloc] initWithContentsOfFile:[NSString stringWithFormat:#"%#/me.png", kSelfBundlePath]];
UIButton *button = [[UIButton alloc] initWithFrame::CGRectMake(0, 0, 40, 20)];
[button setImage:meImage forState:UIControlStateNormal];
[button addTarget:self action:#selector(twitterButton) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.titleView = button;
}

Button action not working [duplicate]

This question already has an answer here:
UIButton action not working ios
(1 answer)
Closed 6 years ago.
UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, tf1.frame.origin.y+55, 200, 30)];
tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf2.backgroundColor=[UIColor whiteColor];
tf2.borderStyle=UITextBorderStyleLine;
tf2.placeholder=#"Capture Invoice";
tf2.textAlignment=UITextAlignmentCenter;
UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(250, tf1.frame.origin.y+55,50,30)];
UIImage *buttonImage = [UIImage imageNamed:#"camera-app-icon_1x.png"];
[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
[invoiceBtn addTarget:self action:#selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];
view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];
[view1 addSubview:tf2];
[view1 addSubview:invoiceBtn];
firstly increase frame size of your view1 and add this line to your code
[self.view addSubView: view1];
like this:
UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 55, 200, 30)];
tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//[tf2 setPlaceholder:#"hello"];
//tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf2.backgroundColor=[UIColor whiteColor];
tf2.borderStyle=UITextBorderStyleLine;
tf2.placeholder=#"Capture Invoice";
tf2.textAlignment=UITextAlignmentCenter;
UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(25, 100,50,30)];
//UIImage *buttonImage = [UIImage imageNamed:#"camera-app-icon_1x.png"];
[invoiceBtn setBackgroundColor:[UIColor blueColor]];
[invoiceBtn setTitle:#"Click" forState:UIControlStateNormal];
//[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
[invoiceBtn addTarget:self action:#selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0,170, 300, 400)];
[view1 setBackgroundColor:[UIColor grayColor]];
[self.view addSubview:view1];
[view1 addSubview:tf2];
[view1 addSubview:invoiceBtn];
it gives output in simulator as and button is clickable
UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf2.backgroundColor=[UIColor whiteColor];
tf2.borderStyle=UITextBorderStyleLine;
tf2.placeholder=#"Capture Invoice";
tf2.textAlignment=UITextAlignmentCenter;
UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(250, tf2.frame.origin.y+55,50,30)];
UIImage *buttonImage = [UIImage imageNamed:#"camera-app-icon_1x.png"];
[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
[invoiceBtn addTarget:self action:#selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];
[view1 addSubview:tf2];
[view1 addSubview:invoiceBtn];
[self.view addSubview:view1];
Apply like this,
UIButton * invoiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *buttonImage = [UIImage imageNamed:#"camera-app-icon_1x.png"];
[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
[invoiceBtn addTarget:self
action:#selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];
invoiceBtn.frame = CGRectMake(250, tf2.frame.origin.y+55,50,30);
[view1 addSubview: invoiceBtn];
Then create action like,
- (void)invoiceClicked:(UIButton*)button
{
NSLog(#"Button clicked.");
}
Hope this works for you :)

How to get backBarButtonItem width?

I try to get backBarButtonItem width:
UIBarButtonItem *leftBtn = self.navigationItem.backBarButtonItem;
UIView *view = [leftBtn valueForKey:#"view"];
CGFloat width;
if(view){
width=view.frame.size.width;
}
NSLog(#"width %f", width);
but in console: width 0.000000
Any ideas what I might be doing wrong? With self.navigationItem.rightBarButtonItem it's work.
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
CALayer *btnLayer = [addButton layer];
[btnLayer setMasksToBounds:YES];
[btnLayer setCornerRadius:1.0f];
[addButton setFrame:CGRectMake(0, 0, 45, 30)];
[addButton setBackgroundColor:[UIColor orangeColor]];
[addButton setTitle:#"Title" forState:UIControlStateNormal];
addButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
[addButton addTarget:self action:#selector(Onclickevent:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setRightBarButtonItem:barButtonItem];

How to change Bar Button Item colour?

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];

Change backgroundcolor for UIButton IPhone

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];

Resources