NOTE: I can get this to work by switching leftbarbuttonitem to rightbarbutton item in my barbutton and barbutton2 methods. I'm just curious to know why it won't work normally!
For some strange reason, my leftbarbuttonitem is showing, but my rightbarbuttonitem just never pops up! Here's my code
-(void)barButton {
image = [UIImage imageNamed:#"BBut.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
[button setBackgroundImage: [[UIImage imageNamed: #"BBut.png"] stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
[button addTarget:self action:#selector(showlocations:) forControlEvents:UIControlEventTouchUpInside];
button.frame= CGRectMake(3.0, 0.0, image.size.width+1, image.size.height+0);
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(3.0, 0.0, image.size.width+1, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *modal = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigation.leftBarButtonItem = modal;
-(void)barButton2 {
image2 = [UIImage imageNamed:#"Refresh.png"];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundImage: [image2 stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateNormal];
[button2 setBackgroundImage: [[UIImage imageNamed: #"Refresh.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
// [button2 addTarget:self action:#selector(viewDidLoad) forControlEvents:UIControlEventTouchUpInside];
button2.frame= CGRectMake(281.0, 5.0, image2.size.width, image2.size.height);
UIView *v2=[[UIView alloc] initWithFrame:CGRectMake(281.0, 5.0, image2.size.width, image2.size.height) ];
[v2 addSubview:button2];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithCustomView:v2]; self.navigation.rightBarButtonItem = refresh;
NSLog(#"THE ACTION HAS BEEN RECIEVED!"); }
In my ViewDidLoad Method, I have this:
[self barButton2];
[self barButton];
The action does go through and I do receive my NSLog method. Does anyone know why this is happening? The Left Bar Button always shows up and if I change the the first barbutton to rightbarbuttonitem and change the barbutton2 to leftbarbutton item, and then change the CGRect coords, it works, and that's what I'm using right now to get it to work, but why is this happening? Thanks in advance.
You don't see the right button because you are drawing it outside the screen.
The button2 X coordinate is set to 281.0 and its parent view X coordinate is also set at 281.0. As a result your button is in x = 562, meaning it is outside the screen.
Set your button2 X coordinate to 0 and see if that helps:
button2.frame= CGRectMake(0.0, 5.0, image2.size.width, image2.size.height);
You seem to be creating a new view with each action and adding a button. So, you never see both buttons. You will need to access the same view in both the actions to make sure you add two button subviews.
Related
I have made at least 10 buttons for my navigation bar, but it never seems to work right.
The rounded edges get pixelated. I cant have that in an app, so can anyone tell me how to make a good icon that looks like an apple one? Also what is the proper size? The code in the app for the button is
UIButton *backbtn = [UIButton buttonWithType:UIButtonTypeCustom];
backbtn.frame = CGRectMake(0, 0, 55, 30);
[backbtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
[backbtn addTarget:self action:#selector(goBackOne) forControlEvents:UIControlEventTouchUpInside]; forState:UIControlStateNormal ];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbtn];//set new button
self.navigationItem.hidesBackButton = YES;//hide original back button
Try this code:
UIImage *backButtonImage = [UIImage imageNamed:#"backButton.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
[backButton addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = customBackBarItem;
and, the back method:
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
The code above just set the image for normal state. You can also set highlighted state for a better appearance. Add some codes Like:
UIImage *backButtonImageHighlighted = [UIImage imageNamed:#"backButtonHighlighted.png"];
[backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];
I have a custom bar button item that I want to nudge down 5 pixels. How would I go about doing that witht the following code. I tried to set the "y" point but keep getting syntax errors.
[self.navigationController setNavigationBarHidden:NO animated:YES];
UIImage *image = [UIImage imageNamed:#"arrow_back.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[self.navigationController.navigationBar addSubview:imageView];
[imageView release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
thanks for any help
A UIBarButton item doesn't inherit from UIView so doesn't have a "frame" property that you can adjust directly so you need to use a different method.
Try:
[self.navigationItem.leftBarButtonItem setBackgroundVerticalPositionAdjustment:5 forBarMetrics:UIBarMetricsDefault];
I have this code that changes the back button of my UINavigationBar
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"backag.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"selback.png"] forState:UIControlStateHighlighted];
button.adjustsImageWhenDisabled = NO;
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, 30, 30);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
If I put it in the viewDidLoad method it works fine. However, when I load the next view the old style button shows up. To fix this I tried putting this code in the next view's viewDidLoad method, but then no button is visible.
Any ideas as to what might be causing this?
Thanks!
Apply same code in
-(void)viewDidAppear:(BOOL)animated
{
}
I used tis code in both view and it's worked fine...
// add following code in your both view's viewDidLoad method...
UIButton *leftButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton1 setImage:[UIImage imageNamed:#"Viewone.png"] forState:UIControlStateNormal];
leftButton1.frame = CGRectMake(0, 0, 30, 30);
[leftButton1 addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton1];
[leftButton1 release];
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setImage:[UIImage imageNamed:#"ViewSecond.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(yourclickevent) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftButton release];
Hope,this will help you...enjoy..
You need to ensure you set the hidesBackButton property on the navigationItem:
// Setup custom back button
self.navigationItem.hidesBackButton = YES;
I'm trying to change a UIImage in a UIButton, when highlighted. The UIButton is situated in a UINavigationController.
I have the following code:
UIView *containingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 28, 28)];
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
barUIButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
barUIButton.frame = CGRectMake(-9, 0, 28, 28);
[barUIButton addTarget:self action:#selector(addButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[barUIButton setImage:[UIImage imageNamed:#"AddHighlighted.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
[containingView addSubview:barUIButton];
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];
self.navigationItem.rightBarButtonItem = containingBarButton;
Instead of the new image showing when highlighted, there is just a black shadow around the existing image.
Why is this?
It looks like the UIButton instance isn't getting highlighted or selected on the touch down event. This is probably because UIBarButtonItem instances don't act like normal buttons; in fact, they're not even UIButton subclasses.
There is a workaround. If you keep a reference to your UIButton in an instance variable, you can add code to change the button's image:
[barUIButton addTarget:self action:#selector(pressDown:) forControlEvents:UIControlEventTouchDown];
[barUIButton addTarget:self action:#selector(pressUp:) forControlEvents:UIControlEventTouchUp];
In pressDown: and pressUp:, you can set the
-(void)pressDown:(id)sender
{
[barUIButton setImage:[UIImage imageNamed:#"Add.png"] forState:UIControlStateNormal];
}
And similarly for pressUp:.
I want to put a custom button as right navigation bar button item. Here is the code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(filterResult) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 50, 50)];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
The problem is that I can see only the image and not the button around it like it comes for UIBarButtonSystemItemAdd or other types.
What should I do?
[button setImage:[UIImage imageNamed:#"my.png"] forState:UIControlStateNormal];
replaces the entire image for the specified state. You have to draw the full own image and not only the icon on it (or whatever you want).