How to change distance between UINavigationBarButtons - ios

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.

Related

How to disable programmatically created UIBarButtonItem in objective c?

I create a toolbar programmatically and add four UIBarButtonItem also programmatically in the toolbar. This button will appear when text view begin editing, if no text in textview clear button and translate button will disable. Here is my four button creating code.
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.backgroundColor = [UIColor lightGrayColor];
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleDone target:self action:#selector(cancelKeyboard)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleDone target:self action:#selector(clearTextView)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Paste" style:UIBarButtonItemStyleDone target:self action:#selector(paste)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:#"Translate" style:UIBarButtonItemStyleDone target:self action:#selector(translate)],
nil];
[numberToolbar sizeToFit];
_sorceTextview.inputAccessoryView = numberToolbar;
_sorceTextview.autocorrectionType = UITextAutocorrectionTypeNo;
now how can i disable clear and translate button in:
-(BOOL)textViewShouldBeginEditing:(UITextView *)textView {}
method? plz help.
Use instance variables for those buttons. Then you can set the enabled property as needed.
#implementation MyViewController {
UIBarButtonItem *_btnClear;
UIBarButtonItem *_btnTranslate;
}
Then in your toolbar setup code:
_btnClear = [[UIBarButtonItem alloc]initWithTitle:#"Clear" style:UIBarButtonItemStyleDone target:self action:#selector(clearTextView)];
_btnTranslate = [[UIBarButtonItem alloc]initWithTitle:#"Translate" style:UIBarButtonItemStyleDone target:self action:#selector(translate)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
numberToolbar.items = #[
[[UIBarButtonItem alloc]initWithTitle:#"Hide" style:UIBarButtonItemStyleDone target:self action:#selector(cancelKeyboard)],
flex,
_btnClear,
flex,
[[UIBarButtonItem alloc]initWithTitle:#"Paste" style:UIBarButtonItemStyleDone target:self action:#selector(paste)],
flex,
_btnTranslate
];
Then wherever you need to disable you can do:
_btnClear.enabled = NO;
and to enable:
_btnClear.enabled = YES;

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 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];

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