Make navigation bar title clickable Objective C iOS 10.2 - ios

Is there any way to make the navigation bar title clickable? I don't have any specific class for navigation bar. I control everything from ViewController. If there's a way, I would love to know. I have been searching it for quite a white but couldn't find a suitable answer.

Make button on Navigation bar
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(btnclick:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 64.0);
self.navigationItem.titleView =button;
-(void)btnclick:(id)sender
{ NSLog(#"button click");
}

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");
}

Related

How I can add action to title of NavigationBar?

I want to give/add action to the title of navigation title.
(Example "Explore")
I want to perform some activity using this approach.
I tried following code, but it's not working.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 100, 100);
//[view addSubview:button];
[self.navigationItem.titleView addSubview:button];
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
ViewController name is came from presentViewController type not from push.navigationController stack.
How I can achieve this using objective-C code?
Replace your code with the following:
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(touchedOnNavigationTitle:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Explore" forState:UIControlStateNormal];
self.navigationItem.titleView = button;
-(void)touchedOnNavigationTitle:(UIButton*)sender {
NSLog(#"Clicked on Navigation Title");
}
titleView is nil by default.
https://developer.apple.com/documentation/uikit/uinavigationitem/1624935-titleview
You should set your title view with your button. Not add it as a subview.
self.navigationItem.titleView = button

UIButton Image is not changed in Hightlight or Selected state

I wrote the following code of a button for an iOS8 app:
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted|UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnBack];
And the method when click:
-(void) btnBackClick:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
However, the background of the image does not change when the button is being click. The only effect is the grayout.
If I draw that button on a xib file, the image change works like a charm.
Anything wrong? Please help.
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted|UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[btnBack setSelected:YES];
[self.view addSubview:btnBack];
//**********Updated*******
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack setFrame:CGRectMake(40, 30, 30, 30)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close.png"]forState:UIControlStateNormal];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateHighlighted];
[btnBack setBackgroundImage:[UIImage imageNamed:#"btn_close_on.png"] forState:UIControlStateSelected];
[btnBack addTarget:self action:#selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnBack];
-(void)btnBackClick:(UIButton *)btn
{
if ([btn isSelected])
{
[btn setSelected:NO];
}
else
[btn setSelected:YES];
}
-(void) btnBackClick:(id)sender{
UIButton *myButton = (UIButton *)sender;
[myButton setSelected: YES]; // Add this line
[self.navigationController popToRootViewControllerAnimated:YES];
}
Also in viewWillAppear method set the same as NO
[myButton setSelected: NO]; // Add this line

Set permanent button image after tap

i've programmatically created a button with an image for normalState.
But I want to set a new button image when the button is pressed (the new image should be displayed for the rest of the time). I tried something, but it only works during tapping. So the button shows its old image after tapping again.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
[cell addSubview:button];
Thanks for your help.
Try to use this it will helps you.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateHighlighted];
button.frame = CGRectMake(15.0f, 32.0f, 24.0f, 20.0f);
// ----------- Add this line in your code -------
[button addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
- (void)btnPressed:(UIButton *)sender
{
[sender setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:UIControlStateNormal];
}
You have to do the setting in the target of the button. Add the following code:
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
and set the image in that method for UIControlStateNormal:
- (void)buttonTapped:(UIButton *)sender {
[button setBackgroundImage:[UIImage imageNamed:#"image2png"] forState:UIControlStateNormal];
}

xcode invisible button with text

I know I could do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor clearColor];
but that leaves a border around the button.
I could also do this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
then put a UILabel behind the button.
Is there a way to accomplish this ^ without needing two objects?
UPDATE:
To clarify my question:
I want the have text, only text, that when pressed performs a method call. For reasons that are complicated to explain I need to do this by way of a button. So, how do I make the text of a button the only visible part of the button. No border. No background. Just text.
UIButton *btnLikeUserCount = [[[UIButton alloc] init] autorelease];
[btnLikeUserCount.titleLabel setFont:[UIFont boldSystemFontOfSize:12]];
btnLikeUserCount.frame = CGRectMake(posx,posy,width,height);
[btnLikeUserCount setTitle:text forState:UIControlStateNormal];
[btnLikeUserCount setTitleColor:[UIColor colorWithRed:50/255.0 green:79/255.0 blue:133/255.0 alpha:1.0] forState:UIControlStateNormal];
[btnLikeUserCount addTarget:self action:#selector(btnLikeUserCountClick:) forControlEvents:UIControlEventTouchUpInside];
no need to take label object.
I can't get what you want exactly but you might be missed [self.view addSubview:button] in your code. so you can not display button with text.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
[button setTitle:text forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Your second code is working fine. In the below image hello is a button.
My code is:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 100, 100, 50);
[button setTitle:#"Hello" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
in this way u can do this
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 400, 100, 50);
[button setTitle:#"Button text" forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button.layer setBorderColor: [UIColor clearColor]];
[button.layer setBorderWidth: 0.0];
[self.view addSubview:button];

UIButton set image and title

I use the following code to load an image is on the upper, lower half is the title of the button, but only the image, is this why? Have any good Suggestions please let us know, thank you
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 44)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets: UIEdgeInsetsMake(0,0,22,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,10,0,10)];
[self.view addSubview:myButton];
you can achieve this effect by this way also.. Try this ::
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 73, 55)];
UIImage *img = [UIImage imageNamed:#"icon.png"];
UIImageView *imgVw = [[UIImageView alloc ] init];
imgVw.image = img;
imgVw.frame = CGRectMake(myButton.frame.origin.x, myButton.frame.origin.y, myButton.frame.size.width, (myButton.frame.size.height-18));
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(34,10,0,10)];
[myButton addTarget:self action:#selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:imgVw];
[self.view addSubview:myButton];
Your button method
-(void) btnClick:(id)sender
{
NSLog(#"working");
}
You can achieve from your logic also :: Here i've edited some code in your logic
UIButton *myButton = [[UIButton alloc]initWithFrame:CGRectMake(160, 400, 40, 35)];
[myButton setImage:[UIImage imageNamed:#"icon.png"] forState:UIControlStateNormal];
[myButton setImageEdgeInsets:UIEdgeInsetsMake(0,(myButton.frame.size.width-52),17,0)];
[myButton setTitle:#"test" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton setTitleEdgeInsets: UIEdgeInsetsMake(22,(myButton.frame.size.width-78),0,22)];
[self.view addSubview:myButton];
but, in this logic you have to resize your image to 23*23 pixels. according to your image size UIEdgeInsetsMake's parameter will be changed.
Hope this helps. Happy coding.

Resources