Customizing the add button in UITableView - ios

I wonder if there is a method to customize the normal "+" button in the initWithBarButtonSystemItem:UIBarButtonSystemItemAdd method.
This is my current code:
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
self.navigationItem.leftBarButtonItem = addButtonItem;
[addButtonItem release];
Thanks for any pointers.

Except for the style property, you can't alter a bar button created in such fashion. If you are looking for other customizations, your best bet is initWithImage:style:target:action:.

What i did to a 3rd party TableView package was creating my own UIBarButtonItem and settig the target & action properties to the ones of the original button. In some cases this will work and in some cases it won't (like if the target checks the source-object for a specific object) - but it's worth a try!

Thanks again to Martin. I thought it might be useful to show the code.
Instead of :
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
self.navigationItem.leftBarButtonItem = addButtonItem;
[addButtonItem release];
I simply used this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithTitle:#"Add" style:UIBarButtonItemStyleBordered
target:nil action:nil];
self.navigationItem.leftBarButtonItem = addButton;
[buttons addObject:addButton];
[addButton release];

Related

How to create Previous/Next buttons above keyboard like in Safari

I'm trying to add Previous/Next buttons above the keyboard like in mobile Safari. There are a lot of questions about that here on StackOverflow, but most answers are to use inputAccessoryView.
I tried that and it looks like this:
Is there anyway to have the buttons in the toolbar bellow, like it works in mobile Safari??
This is how it looks like in Safari:
It seems what I was looking for is inputAssistantItem.
It allows you to add and change buttons in the keyboard's shortcut bar.
UIBarButtonItem* nextButton = [[UIBarButtonItem alloc]
initWithImage:nextImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(nextField:)];
UIBarButtonItem* previousButton = [[UIBarButtonItem alloc]
initWithImage:previousImage
style:UIBarButtonItemStylePlain
target:self
action:#selector(previousField:)];
NSArray *buttonGroups = #[[[UIBarButtonItemGroup alloc]
initWithBarButtonItems:#[previousButton, nextButton]
representativeItem:nil]];
textField.inputAssistantItem.trailingBarButtonGroups = buttonGroups;
UIBarButtonItem *previous = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:103 target:self action:#selector(previousButtonTapped:)];
UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[fixedSpace setWidth:6.0];
UIBarButtonItem *next = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:104 target:self action:#selector(nextButtonTapped:)];

How to change distance between UINavigationBarButtons

I set two navigation bar buttons, and there is a space between it, how can I change this space to set buttons closer to each other?
I've tried to add third button with minus width, Here the code
self.editButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Edit.png"] style:UIBarButtonItemStylePlain target:self action:#selector(editAction:)];
self.callButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"call_icon.png"] style:UIBarButtonItemStylePlain target:self action:#selector(editAction:)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:nil] style:UIBarButtonItemStylePlain target:self action:nil];
spacer.width = -30;
NSArray *buttons = #[self.editButton, spacer, self.callButton];
self.navigationItem.rightBarButtonItems = buttons;
But it is not working; Any suggessions?
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil action:nil];
negativeSpacer.width = -16;// it was -6 in iOS 6
[self.navigationItem rightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer, requriedButton/*this will be the button which u actually need*/, nil] animated:NO];
hope this will work for you...
You can create a UIBarButtonItem with the style UIBarButtonSystemItemFixedSpace and add it to the buttons array.

Abnormal behaviour in ios7 uibarbuttonitem

I have used a UIBarButtonItem in navigation controller. It look like as
and my code was
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneImagePicker)];
[self.navigationItem setRightBarButtonItem:doneButton];
[doneButton release];
I dont need white coloured box over Done button. How to fix it.
update:
For some reason it's style is being set incorrectly.
You should set it to UIBarButtonItemStylePlain like this:
donButton.style = UIBarButtonItemStylePlain;

Grabbed the action of a UIBarButtonItem

I have code like this in my navigation bar:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(self)];
self.navigationItem.rightBarButtonItem = myButton;
What's the next step to grab when this is actually pressed?
You mad a mistake here by setting #selector(self).
You need to put here the actual function you need to fire:
UIBarButtonItem *myButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self action:#selector(doSomeThing)];
And of course:
- (void) doSomeThing{
}
Do like this
UIBarButtonItem *Button=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(myAction:)];
self.navigationItem.leftBarButtonItem=Button;
- (void)myAction:(id)sender
{
//do ur actions
}

iOS - UIToolBar as inputAccessoryView for UITextView

I've added a UIToolBar with a UIBarButtonItem as inputAccessoryView for a UITextView. It works fine but the UIBarButtonItem is touchable outside it's frame, perhaps 50 pixels outside to the right. It's no big deal but it annoys me. Anyone know why?
This is my code (ARC):
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneWriting:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];
self.messageTextView.inputAccessoryView = toolBar;
In iOS 6 it seems to behave as expected.
Nice tip: If you want the button to appear on the right instead of the left, use one of these:
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
Then initialise the toolbar with:
[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
The toolbar seems to expand the active area of the buttons beyond their bounds if there are no other nearby buttons in the toolbar. Apple engineers must think it is better to try to guess where the user intended to press rather than not react at all.
I hope it helps you...
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];
UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:105 target:nil action:nil]; //<
UIBarButtonItem* NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106 target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(doneClicked:)];
UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:nil action:nil] ;
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];
Use Fake Item to get exact pinch location on Button...

Resources