Add spacing between UIToolbar - objective-c

I have a toolbar that looks like the following:
The issue is that it is kind of cluttered and therefore I would like to add some spacing to it. I tried doing:
UIBarButtonItem *spacer =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
self.toolbar_array =
[[NSMutableArray alloc] initWithObjects:self.mention,
spacer,
self.picture,
spacer,
share,
spacer,
self.message, nil];
But it still gives me the same thing. How can I add a 10px between these UIBarButtonItems?

UIBarButtonItem *fixedSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
fixedSpace.width = 10;

You need to add space in between the items what u r looking for.
this can be done by..
UIBarButtonItem *fixedSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
fixedSpace.width = 10;
hope this will help u.

I use this code to generate UIBarButtonItems, it's some header file which I #import if needed.
static inline UIBarButtonItem *BarButtonWithText(NSString *text,
id target,
SEL action) {
NSString *localizedText = NSLocalizedString(text, nil);
return [[[UIBarButtonItem alloc] initWithTitle:localizedText
style:UIBarButtonItemStyleBordered
target:target
action:action] autorelease];
}
static inline UIBarButtonItem *BarButtonWithImage(NSString *imageName,
id target,
SEL action) {
UIImage *image = [UIImage imageNamed:imageName];
return [[[UIBarButtonItem alloc] initWithImage:image
style:UIBarButtonItemStylePlain
target:target
action:action] autorelease];
}
static inline UIBarButtonItem *BarButtonWithSystemStyle(UIBarButtonSystemItem style,
id target,
SEL action) {
return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:style
target:target
action:action] autorelease];
}
static inline UIBarButtonItem *BarButtonWithFlexibleWidth(id target, SEL action) {
return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:target
action:action] autorelease];
}
static inline UIBarButtonItem *BarButtonWithFixedWidth(CGFloat width,
id target,
SEL action) {
UIBarButtonItem *button =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:target
action:action];
button.width = width;
return [button autorelease];
}

Related

How to recognize a button press for programmatic UIButton Xcode

Im using this code to create UIBarButtons programmatically on my Navigation Bar, how can I set the Buttons to send IBActions programmatically. I.O.W. how can I programmatically place a view to recognize the UITapGestureRecognizer(set the touchUpInside event) and connect it to an IBAction?
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.title = #"Feed";
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
UIBarButtonItem *postItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon2.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *cameraAndPostItem = #[cameraItem, postItem];
NSArray *addPlusAndOptionsItem = #[optionsItem, addPlusItem];
self.navigationItem.leftBarButtonItems = cameraAndPostItem;
self.navigationItem.rightBarButtonItems = addPlusAndOptionsItem;
EDIT: I am trying to perform a segue when the user presses the UIBarButtonItem, which I have implemented programmatically.
set the target and the action of the buttons
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
cameraItem.target = self;
cameraItem.action = #selector(cameraItemButtonInvoked);
//or short
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(cameraItemButtonInvoked)];
...
- (IBAction)cameraItemButtonInvoked {
.... // e.g.
[self performSegueWithIdentifier:#"ShowDetail" sender:sender];
}
You'll be setting the action to UIBarButton like this:
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addSomething:)];
Note the action part, there you are passing the selector to be called. Write that selector in your ViewController like this:
- (void) addSomething:(id)sender{
NSLog(#"I'm adding something..");
}
Do the same for rest of the buttons.

UIBarButtonItem target/action not working

I am implementing a view with a date picker and a toolbar.
I have the following code:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(didSelectCancelButton)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(didSelectDoneButton)];
NSArray *buttons = #[cancelButton, flexible, doneButton];
[self.toolbar setItems:buttons
animated:NO];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), 216)];
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
}
return self;
}
- (IBAction)didSelectCancelButton {
if ([self.delegate respondsToSelector:#selector(datePickerDidCancelDateSelection:)]) {
[self.delegate datePickerDidCancelDateSelection:self];
}
}
- (IBAction)didSelectDoneButton {
NSLog(#"");
}
But when I click the buttons, no action is performed. The methods are not invoked.
Can you tell what am I doing wrong?
Thanks
EDIT:
Turned out there was a gesture recognizer that was capturing the touch events.
Fixing that resolved this problem.
I suspect your date picker (which is as wide as the toolbar is) is somehow inhibiting your buttons from getting user touches.
Try reversing these two lines to this:
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
Do you have any UIGestureRecognizer objects, if you do try to disable them and test.

how do you add a right button to a uinavigationcontrol

How can I add a button to a UINavigationControl that I added to a ViewController? (The project isn't UINavigationControler based.)
Here's the code I've added to my viewDidLoad method:
navigation = [[UINavigationController alloc] init];
navigation.view.frame = CGRectMake(0, 0, 1024, 748);
navigation.navigationBar.tintColor = [UIColor blackColor];
navigation.navigationBar.alpha = 1.0f;
navigation.navigationBar.translucent = NO;
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
[navigation.navigationItem setRightBarButtonItem:cancelButton animated:YES];
[cancelButton release];
[self.view addSubview:navigation.view];
[self pushPresentationList];
Can someone help me fix lines 7-9 please?
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Button"
style:UIBarButtonSystemItemDone target:nil action:nil];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Awesome"];
item.rightBarButtonItem = rightButton;
item.hidesBackButton = YES;
[navigationBar pushNavigationItem:item animated:NO];
Do you need an UINavigationController? Why not just use a simple UINavigationBar. Here is some sample code:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:
CGRectMake(0,0,self.view.frame.size.width, 49.0f)];
[self.view addSubview:navBar];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStylePlain target:self action:#selector(cancel)];
UINavigationItem *rightBarButtonItem = [[UINavigationItem alloc] init];
[rightBarButtonItem setRightBarButtonItem:cancelButton];
[navBar pushNavigationItem:rightBarButtonItem animated:NO];

How can I add multiple UIBarButtonItem's to a UINavigationBar?

I want to add many UIBarButtonItem's to a UINavigationbar, not just right and left buttons:
logoButton = [[UIBarButtonItem alloc] initWithTitle:#"A Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonAClicked:)];
logoButton2 = [[UIBarButtonItem alloc] initWithTitle:#"B Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonBClicked:)];
logoButto3 = [[UIBarButtonItem alloc] initWithTitle:#"C Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonCClicked:)];
self.navigationController.navigationBarHidden = NO;
self.title = #"Title";
NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;
I get a SIGBRT on self.navigationController.navigationBar.items = items;
How can I add multiple UIBarButtonItems to a UINavigationBar?
You need to add UIBarButtonItem instance to a UINavigationItem, not to a UINavigationBar. So you can do this as:
NSArray *buttonArray = [NSArray arrayWithObjects:logoButton, logoButton2, logoButton3, nil];
self.navigationItem.leftBarButtonItems = buttonArray;
If you want your buttons on the right, use rightBarButtonItems.
You should use
self.navigationItem.leftBarButtonItems = items;
use addSubView in navigationbar.

Navigation bar button is not clickable

I add this navigation bar code but i am not able to click on the left side lat item button "Edit" and when i replace with textbox even that is also not work (not take value), if you can figure that out what is the wrong with this code i will be very help full to you.
Thanks,
Aashish
find the screen shot
http://www.flickr.com/photos/71234685#N02/6440926473/in/photostream
-(void) initializeNavigationalBar {
UIToolbar* _leftNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _leftNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem* _editScenarioProduct = self.editButtonItem;
UIBarButtonItem* _addTurftype = [[UIBarButtonItem alloc] initWithTitle:#"Turftype" style:UIBarButtonItemStyleBordered target:self action:#selector(getTurfType)];
UIBarButtonItem *_addTargetN = [[UIBarButtonItem alloc] initWithTitle:#"Target N" style:UIBarButtonItemStyleBordered target:self action:#selector(getTargetNperWeek)];
UIBarButtonItem *_addMapRegion = [[UIBarButtonItem alloc] initWithTitle:#"Map Region" style:UIBarButtonItemStyleBordered target:self action:#selector(getMapRegion)];
CGRect _textFrame = CGRectMake(0, 0, 80, 30 );
_turfacresTextField = [[UITextField alloc] initWithFrame:_textFrame];
_turfacresTextField.borderStyle = UITextBorderStyleRoundedRect;
_turfacresTextField.font = [UIFont systemFontOfSize:12.0];
_turfacresTextField.placeholder = #"Turf Acres";
_turfacresTextField.textAlignment = UITextAlignmentCenter;
_turfacresTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_turfacresTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
_turfacresTextField.keyboardType = UIKeyboardTypeDefault;
_turfacresTextField.returnKeyType = UIReturnKeyDone;
UIBarButtonItem *_addTurfacres = [[UIBarButtonItem alloc] initWithCustomView:_turfacresTextField];
[_leftNavBarButtons addObject:_addTurftype];
[_leftNavBarButtons addObject:_addTargetN];
[_leftNavBarButtons addObject:_addMapRegion];
[_leftNavBarButtons addObject:_addTurfacres];
[_leftNavBarButtons addObject:_editScenarioProduct];
[_leftNavBarTools setItems:_leftNavBarButtons animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftNavBarTools];
UIToolbar* _rightNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _rightNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem *_addSoilSetting = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(setSoilandCarryover)];
[_rightNavBarButtons addObject:_addSoilSetting];
UIBarButtonItem* _addApplication = [[UIBarButtonItem alloc] initWithTitle:#"Add Appplication" style:UIBarButtonItemStyleBordered target:self action:#selector(addApplication)];
[_rightNavBarButtons addObject:_addApplication];
[_rightNavBarTools setItems:_rightNavBarButtons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightNavBarTools];
}
Try [self editButtonItem] instead of self.editButtonItem.