UIBarButtonItem has a setEnabled: method which (for all but the back button) works as expected, but it does not have a setUserInteractionEnabled: method. But while not enabled, UIBarButtonItems can receive touches which, when re-enabled, they attempt to process. I'd like to do the equivalent of setUserInteractionEnabled: NO for my navigation bar buttons (during an operation that can sometimes take a while).
Is there an easy way to do this that I am missing?
You need to do little customization and you can do it...
// Add this code in your viewDidLoad
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftButton setUserInteractionEnabled:NO];
[leftButton setImage:[UIImage imageNamed:#"navigationbutton.png"] forState:UIControlStateNormal];
leftButton.frame = CGRectMake(0, 0, 30, 30);
[leftButton addTarget:self action:#selector(yorrmethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
[leftButton release];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[rightButton setUserInteractionEnabled : NO];
[rightButton setImage:[UIImage imageNamed:#"navigationbutton.png"] forState:UIControlStateNormal];
rightButton.frame = CGRectMake(0, 0, 30, 30);
[rightButton addTarget:self action:#selector(yourmethod) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[rightButton release];
Hope,this will help you...
Related
I am using Split View Controller, which has 2 View controllers on the second view controller i am suppose add two buttons on the right side of navigation controller.
i have used the following code to add one button which works:
UIBarButtonItem *barButton=[[UIBarButtonItem alloc] init];
[barButton setCustomView:btnShare];
self.navigationItem.rightBarButtonItem=barButton;
tried this link http://osmorphis.blogspot.in/2009/05/multiple-buttons-on-navigation-bar.html
but could not succeed.Please help me fix this.
Try this
NSMutableArray *arrRightBarItems = [[NSMutableArray alloc] init];
UIButton *btnSetting = [UIButton buttonWithType:UIButtonTypeCustom];
[btnSetting setImage:[UIImage imageNamed:#"settings.png"] forState:UIControlStateNormal];
btnSetting.frame = CGRectMake(0, 0, 32, 32);
btnSetting.showsTouchWhenHighlighted=YES;
[btnSetting addTarget:self action:#selector(onSettings:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnSetting];
[arrRightBarItems addObject:barButtonItem];
UIButton *btnLib = [UIButton buttonWithType:UIButtonTypeCustom];
[btnLib setImage:[UIImage imageNamed:#"library.png"] forState:UIControlStateNormal];
btnLib.frame = CGRectMake(0, 0, 32, 32);
btnLib.showsTouchWhenHighlighted=YES;
[btnLib addTarget:self action:#selector(onMyLibrary:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem2 = [[UIBarButtonItem alloc] initWithCustomView:btnLib];
[arrRightBarItems addObject:barButtonItem2];
UIButton *btnRefresh = [UIButton buttonWithType:UIButtonTypeCustom];
[btnRefresh setImage:[UIImage imageNamed:#"refresh.png"] forState:UIControlStateNormal];
btnRefresh.frame = CGRectMake(0, 0, 32, 32);
btnRefresh.showsTouchWhenHighlighted=YES;
[btnRefresh addTarget:self action:#selector(onRefreshBtn:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem1 = [[UIBarButtonItem alloc] initWithCustomView:btnRefresh];
[arrRightBarItems addObject:barButtonItem1];
self.navigationItem.rightBarButtonItems=arrRightBarItems;
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"btn_home" heighlightImageName:#"btn_home_h" buttonFrame:CGRectMake(2, 1, 34, 34) selectorName:#selector(buttonHomeClicked:) target:self]]];
UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
[tempButton setFrame:CGRectMake(240, 5, 34, 34)];
[tempButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[tempButton setImage:[self getImageFromResource:normalImageName] forState:UIControlStateNormal];
[tempButton setImage:[self getImageFromResource:heighlightImageName] forState:UIControlStateHighlighted];
[tempButton setImage:[self getImageFromResource:#"btn_fav_h"] forState:UIControlStateSelected];
[self.navigationController.navigationBar addSubview:self.buttonFavorite];
create two uibarbuttonitem and add both in an array then add whole array in navigation bar
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addButton,sendButton];
try thi code
UIButton *btnLogOut = [UIButton buttonWithType:UIButtonTypeCustom];
btnLogOut.frame = CGRectMake(0, 0, 62, 31);
[btnLogOut setImage:[UIImage imageNamed:#"logout_new.png"] forState:UIControlStateNormal];
[btnLogOut addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:btnLogOut];
UIButton *btnError = [UIButton buttonWithType:UIButtonTypeCustom];
btnError.frame = CGRectMake(0, 0, 62, 31);
[btnError setImage:[UIImage imageNamed:#"list-icon.png"] forState:UIControlStateNormal];
[btnError addTarget:self action:#selector(logoutButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *errorButton = [[UIBarButtonItem alloc] initWithCustomView:btnError];
NSArray *navigationBarBtnArray=[NSArray arrayWithObjects:errorButton,logOutBarButton, nil];
self.navigationItem.rightBarButtonItems=navigationBarBtnArray;
Try this
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
make a UIView adding as many buttons you want on it. then use
UIBarButtonItem *myBarbtn=[[UIBarButtonItem alloc] initWithCustomView:buttonsView];
self.navigationItem.rightBarButtonItem = myBarbtn;
Use segmented control for this
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl setMomentary:YES];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample1.png"] atIndex:0 animated:NO];
[segmentedControl insertSegmentWithImage:[UIImage imageNamed:#"sample2.png"] atIndex:1 animated:NO];
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem * segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
self.navigationItem.rightBarButtonItem = segmentBarItem;
Make an array of UIBarButtonItems and pass it to
- (void)setRightBarButtonItems:(NSArray *)items animated:(BOOL)animated
method of UINavigationItem.
Simply use an UISegmentedControl instead of a UIButton.
Then change the mode to "momentary" and add an action on change.
When the action is triggered, check the selectedSegmentIndex to know what segment was chosen.
I have a leftbar button item. But I want it to move it more to the right. At the moment it sticks to the left hand side. Any body has an idea how I can achieve this?
Here is my code for the button itself.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"task_status.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(changeStatus:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(80, 0,100, 18)];
self.tabBarController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Hope anyone kan help me with this.
Kind regards
this is what I have done it worked for me
UIImage *image = [UIImage imageNamed:#"btn_bar_back.png"] ;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10.0, -15.0, image.size.width, image.size.height);//set your button frame
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:#"BACK" forState:UIControlStateNormal];
button.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);// if want to move text more towards right
[button addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width+10, 50)];
button.center = CGPointMake(view.center.x, view.center.y);
[view addSubview:button];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:view] autorelease];
[view release];
Why not Adding Directly rightbar Button?
self.tabBarController.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(changeStatus:)] autorelease];
I have the following to add a custom image as a back button. The problem is that it overrides the default navigation controller back method.
How can I correct this?
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"button-back-arrow.png"] forState:UIControlStateNormal];
//[button addTarget:self action:#selector(favouriteButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(280, 25, 40, 29)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
thanks for any help
just add this line and method..
[button addTarget:self
action:#selector(BtnBack_Clicked:)
forControlEvents:UIControlEventTouchDown];
and call this method
-(IBAction)BtnBack_Clicked:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
UIView *btnView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 84, 31)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 84, 31);
[btn setBackgroundImage:[UIImage imageNamed:#"yourImage.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(action) forControlEvents:UIControlEventTouchUpInside];
[btnView addSubview:btn];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btnView];
[btnView release];
I'm trying for hours to make a left button work properly and mimic a back button.
My code to create the button:
UIBarButtonItem *backButton = [self customBarButton:#"back_button" imageHiglighted:#"settings_button_highlighted" x:20 y:0 widthDivider:2.6 heightDivider:2.6];
backButton.target = self;
backButton.action = #selector(buttonPressed:);
self.navigationItem.leftBarButtonItem = backButton;
Here the method called to create custom button:
- (UIBarButtonItem *)customBarButton:(NSString *)imageDefault imageHiglighted:(NSString *)imageHighlighted x:(float)x y:(float)y widthDivider:(float)widthDivider heightDivider:(float)heightDivider {
UIImage *customImageDefault = [UIImage imageNamed:imageDefault];
UIImage *customImageHighlighted = [UIImage imageNamed:imageHighlighted];
CGRect frameCustomButton = CGRectMake(x, y, customImageDefault.size.width/widthDivider, customImageDefault.size.height/heightDivider);
UIButton *customButton = [[UIButton alloc] initWithFrame:frameCustomButton];
[customButton setBackgroundImage:customImageDefault forState:UIControlStateNormal];
[customButton setBackgroundImage:customImageHighlighted forState:UIControlStateHighlighted];
UIBarButtonItem *barCustomButton =[[UIBarButtonItem alloc] initWithCustomView:customButton];
return barCustomButton;
}
And the action:
-(void)buttonPressed:(id) sender{
NSLog(#"Entered");
SearchViewController *ViewController = [[SearchViewController alloc] init];
[self.navigationController pushViewController:ViewController animated:YES];
}
So I was able to make it with a simple UIButton but not with a UIButtonBarItem and I really don't know what's going on with it.
If you could help me I'd be very grateful.
Thanks.
Do this add selector to custom button as it is view of bar buttom:
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
EDIT : Note : the target and action of the UIBarButtonItem apply to custom views.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(//HERE ! i don't know put what thing !) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
TRY THIS
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[GlobalMethods buttonWithImage:#"top-nav-back" heighlightImageName:nil buttonFrame:CGRectMake(102, 1, 50, 30) selectorName:#selector(btnBackClicked:) target:self]];
+ (UIButton*)buttonWithImage:(NSString*)normalImageName heighlightImageName:(NSString*)heighlightImageName buttonFrame:(CGRect)buttonFrame selectorName:(SEL)selectorName target:(id)target
{
UIButton *objButton = [UIButton buttonWithType:UIButtonTypeCustom];
[objButton setFrame:buttonFrame];
[objButton addTarget:target action:selectorName forControlEvents:UIControlEventTouchUpInside];
[objButton setImage:[GlobalMethods getImageFromResourceBundle:normalImageName] forState:UIControlStateNormal];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateDisabled];
if(heighlightImageName)
[objButton setImage:[GlobalMethods getImageFromResourceBundle:heighlightImageName] forState:UIControlStateSelected];
return objButton;
}
Try this..
[customButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
in your customBarButton method..
I mean to say give selector to your button instead of bar button item..
Check and reply..
Just put this line in your code..
[backButton addTarget:self action:#selector(backBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
This worked for me
UIButton * tmpButton = [self generateButtonwithImageName:#"Back.png" andSize:CGRectMake(0, 0, 55, 30)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithCustomView:tmpButton];
[tmpButton addTarget:self action:#selector(reload) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem=backButton;
-(UIButton*)generateButtonwithImageName :(NSString*)imageName andSize:(CGRect)rect{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=rect;
[button setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
return button;
}
//working for me
if([version floatValue] < 7.0)
{
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:#"navi-bar.png"] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.opaque = YES;
closeButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 35, 30)];
[closeButton setImage:[UIImage imageNamed:#"btn_end.png"] forState:UIControlStateNormal];
[closeButton setImage:[UIImage imageNamed:#"btn_end_on.png"] forState:UIControlStateHighlighted];
[closeButton addTarget:self action:#selector(actionClose) forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:closeButton];
}
else{
/*add button back to navigation*//*add buttom back to navigation*/
UIBarButtonItem *itemBack = [[UIBarButtonItem alloc]initWithTitle:#"完了" style:UIBarButtonItemStylePlain target:self action:#selector(actionClose)];
self.navigationItem.rightBarButtonItem = itemBack;
}
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;