Change UIToolbar Button Image from Code - objective-c

is there any way to change the button image inside UIToolbar?
I am actually using this way but it doesn't work.
[myButton setImage:[UIImage imageNamed:#"blue.png"]];
For your information, "myButton" is an IBOulet UIBarButtonItem.
#property (nonatomic, strong) IBOutlet UIBarButtonItem *myButton;

Use the following cole. i hope it will use to you.
UIImage *image = [UIImage imageNamed:#"buttonImage.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[button setImage:image forState:UIControlStateNormal];
[button addTarget:myTarget action:#selector(myAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Thanks,.

// add images to bar button item of toolbar in iphone
to set button type as custum:
button=[UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 40, 40)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
// add the image on toolbar:
[toolbar insertSubview:[[[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"image.png"]] autorelease] atIndex:1];

Related

Go back to previous ViewController in xcode 5 storyboard

I am trying to create a back button on my iPhone app and I would like to go back to my landing view .. I am using this code but it did not work
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setFrame:CGRectMake(0, 0, 24, 24)];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
navEng.leftBarButtonItem = btnBack;
-(void)goback{ [self.navigationController popToRootViewControllerAnimated:YES]; }
try this .... just changed last line code
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom]; [back setFrame:CGRectMake(0, 0, 24, 24)]; [back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
self.navigationItem.leftBarButtonItem = btnBack;
-(void)goback
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
navEng is declared as IBoutlet UINavigationItem in header file and the back button appears and the action handled correctly but the navigation to the previous ViewController did not worked
#property (nonatomic, strong) IBOutlet UINavigationItem *navEng;
UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
[back setFrame:CGRectMake(0, 0, 24, 24)];
[back setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[back addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btnBack = [[UIBarButtonItem alloc] initWithCustomView:back];
self.navigationItem.hidesBackButton= YES;
navEng.leftBarButtonItem = btnBack;
-(void)goback{
NSLog(#"back");
[self.navigationController popViewControllerAnimated:YES];
}

Objective-c : action:#selector(xxxxxx) not called

I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}

Position UIBarbuttonItem to the VERY left

I have a UIBarButtonItem that I want hugging the very left of the screen. I've tried putting a fixed barbutton with width=0 before the button and other configurations but there's always several pixels between the button and x=0.
I've also tried changing the imageInset of the UIButton
UIButton *moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
moreButton.frame = CGRectMake(0, 0, 60, 35);
[moreButton setImage:[UIImage imageNamed:#"Morebutton"] forState:UIControlStateNormal];
moreButton.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8);
[moreButton addTarget:self action:#selector(pressedMoreButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *moreBar = [[[UIBarButtonItem alloc] initWithCustomView:moreButton] autorelease];
But tapping near the edge of the screen doesn't trigger the selector (probably because only the image is moved but not the button itself) and increasing the size of the button doesn't affect the gap between the button and the left side.
Thanks
try to put your button to the container view and change position of button on the view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.contentMode = UIViewContentModeScaleAspectFit;
button.frame= CGRectMake(-4.0, 0.0, 30, 30);
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, cellTextSize.width+10, buttonImage.size.height)] autorelease];
v.backgroundColor = [UIColor clearColor];
[v addSubview:button];
return [[[UIBarButtonItem alloc] initWithCustomView:v] autorelease];

UIBarButtonItem target-action does not invoke

- (void)viewDidLoad
{
[super viewDidLoad];
//Load the image
UIImage *buttonImage = [UIImage imageNamed:#"1.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//sets the frame of the button to the size of the image
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
//creates a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
customBarItem.target = self;
customBarItem.action = #selector(click:);
self.toolbarItems = [NSArray arrayWithObjects:customBarItem, nil];
}
- (void)click:(id)sender {
NSLog(#"action");
}
I think my console will print "action" when I push down the barItem.
However "action" is not printed.
Did I miss anything?
Thanks for advance!
Try this
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"Click ME!" style:UIBarButtonItemStyleBordered target:self action:#selector(click)];
.
.
.
-(void)click{
NSLog("hello");
}
I believe the problem is that you are using a UIButton for the [[UIBarButtonItem alloc] initWithCustomView when you should simply pass a UIView, the action is probably going to the UIButton and not the UIBarButtonItem.
Instead do this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1.png"];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:myImageView];
I think you're trying to do something more like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setFrame:CGRectMake(0.0f, 0.0f, 25.0f, 25.0f)];
[button addTarget:self action:#selector(click) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];

How to add an image to UIBarButtonItem?

I created a custom button and applied it to the UIbarbuttonitem.
There is no error but nothing is shown on the navigation bar:(
This is my code-
//create a custom button
UIImage *image = [UIImage imageNamed:#"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[myCustomButton release];
[image release];
[navID release];
Anybody who can fix my code? :)
From the docs:
initWithImage:style:target:action:
Try that.
To set ANY image with right coloring, use UIButton, set "image for state" and then create UIBarButtonItem with customView
UIImage *btnImage = [UIImage imageNamed:#"button"];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.bounds = CGRectMake( 0, 0, btnImage.size.width, btnImage.size.height );
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchDown];
[btn setImage:btnImage forState:UIControlStateNormal];
_buttonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
One of the Simplest Way is :
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backk.png"] style:UIBarButtonItemStylePlain target:self action:#selector(backBtn:)];
self.navigationItem.leftBarButtonItem = button2;
Action Method:
- (IBAction)backBtn:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}