iOS - UIToolBar as inputAccessoryView for UITextView - ios

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...

Related

UIToolBar is black for a split second on load

Every time I load up the UIView that holds the UIToolbar below (which is attached to the keyboard), it turns black for a second until the view fully loads, and then turns back to the standard white color. I'm stumped as to why this keeps happening.
Here's how I'm forming the UIToolbar:
[self.answerField becomeFirstResponder];
UIToolbar *toolbar = [UIToolbar new];
UIBarButtonItem *sectionButton = [[UIBarButtonItem alloc] initWithTitle:#"§" style:UIBarButtonItemStylePlain target:nil
action:#selector(addSectionSymbol:)];
UIBarButtonItem *paraButton = [[UIBarButtonItem alloc] initWithTitle:#"¶" style:UIBarButtonItemStylePlain target:nil
action:#selector(addParaSymbol:)];
UIBarButtonItem *flexSpacing = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSString *infoLabel = #"Select text to italicize. Always use italics.";
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithTitle:infoLabel style:UIBarButtonItemStylePlain target:nil action:nil];
[infoButton setEnabled:NO];
[toolbar setItems:[NSArray arrayWithObjects:sectionButton, paraButton, flexSpacing, infoButton, nil]];
self.answerField.inputAccessoryView = toolbar;
[toolbar sizeToFit];
Any ideas?

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.

UIPickerView with UIToolBar issue

I'm having an issue with a UIPickerView with a UIToolBar that has a Done button. The thing is that the button Done sometimes is showed in blue (normal behavior) and other times the button appear in a sheer grey that is difficult to see (See picture attached).
The pickerview and the toolbar are generated this way:
mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
[mypickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(pickerDoneClicked:)];
[barItems addObject:doneBtn];
[mypickerToolbar setItems:barItems animated:YES];
statusTextField.inputAccessoryView = mypickerToolbar;
I've searched about it with no luck.

How to get rid of the line on top of my button in a UIToolBar

I've created a UIToolBar and set two UIBarButtonItems and then assigned this toolbar to self.navigationItem.rightBarButtonItem.
Everything works perfectly except there is a line on top of my buttons.
The code:
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(editRow:)];
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
[toolBar setItems:#[addItem, editItem]];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolBar];
Please see my pic:
You get rid of the line by getting rid of the toolbar.
UIBarButtonItem *addItem = [[UIBarButtonItem alloc] initWithTitle:#"Add" style:UIBarButtonItemStylePlain target:self action:#selector(addNewRow:)];
UIBarButtonItem *editItem = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(editRow:)];
self.navigationItem.rightBarButtonItems = #[ addItem, editItem ];

how to add two button in navigation bar with some space

I am adding two button in navigation bar they are working fine but i want space between them they are both combined i want a bit space between them
UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIBarButtonItem *btnEdit = [[UIBarButtonItem alloc] initWithTitle:#"Add"
style:UIBarButtonItemStylePlain
target:self
action:#selector(Add)];
UIToolbar *rightToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
rightToolBar.backgroundColor = [UIColor clearColor];
rightToolBar.tintColor = [UIColor colorWithRed:40.0/255.0 green:48.0/255.0 blue:51.0/255.0 alpha:0.0];
NSArray *buttonsRight = [NSArray arrayWithObjects:btnEdit, btnAdd, nil];
[rightToolBar setItems:buttonsRight];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightToolBar];
You can add any of these two between your UIBarButtonItem
UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]
Note that to set the width of a Fixed Space UIBarButtonItem, you need to set the .width property
[fixed setWidth:455.0f];

Resources