How To add buttons to UINavigationBar - ios

I need something just like this
http://anishusite.appspot.com/hbimgs/Bills220.png
to be able to change the date from the navigation bar ... How is this done?
I tried adding a toolbar with thous buttons and a view with the date label but it's not working very well
http://osmorphis.blogspot.ro/2009/05/multiple-buttons-on-navigation-bar.html
Here is the code:
//first create the view
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 6, 157, 33)];
[titleView setBackgroundColor:[UIColor clearColor]];
//add the buttons to the view
UIButton *prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
[prevButton setFrame:CGRectMake(0, 1, 32, 32)];
[prevButton setBackgroundImage:[UIImage imageNamed:#"36-circle-west"] forState:UIControlStateNormal];
[prevButton addTarget:self action:#selector(prevMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:prevButton];
UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setFrame:CGRectMake(145, 1, 32, 32)];
[nextButton setBackgroundImage:[UIImage imageNamed:#"20-circle-east"] forState:UIControlStateNormal];
[nextButton addTarget:self action:#selector(nextMonth) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:nextButton];
[self.dateLabel setFrame:CGRectMake(33, 1, 90, 33)];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setTextAlignment:UITextAlignmentCenter];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:#"MMMM,YYYY"];
NSString *dateString = [dateFormatter stringFromDate:self.beginDate];
NSLog(#"the title date string: %#",dateString);
[self.dateLabel setText:dateString];
[self.dateLabel setTextColor:[UIColor whiteColor]];
[self.dateLabel setBackgroundColor:[UIColor clearColor]];
[self.dateLabel setFont:[UIFont fontWithName:#"System" size:17.0]];
//create the toolbar itself
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 240, 44)];
[toolbar setBackgroundColor:[UIColor clearColor]];
NSMutableArray *items = [NSMutableArray arrayWithCapacity:3];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addTransaction)];
addButton.style = UIBarButtonItemStyleBordered;
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *theView = [[UIBarButtonItem alloc] initWithCustomView:titleView];
[items addObject:theView];
[items addObject:spacer];
[items addObject:addButton];
[toolbar setItems:items animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
http://www.shareimages.com/image.php?61601-qpyWmpSfk5.kmpyWmak-screen.png
As you can see it doesn't look good. The Label is not showing... The buttons seem to work only if tapped on the upper half. (when tapped the methods are called ok ) ... any ideeas? BTW the navigation bar has a background image, however the toolbar covers it up.. why ? I made the toolbar with a clear color background ....

UINavigationItem has a titleView property. you can set this to any View you create. So create a UIToolbar subclass with transparent background, add your buttons and text labels to that, then:
self.navigationItem.titleView = myCustomView;
where myCustomView is the view containing your arrow buttons and labels. This view could be a UIToolbar subclass, read Couldn't UIToolBar be transparent? for tricks on creating a UIToolbar subclass that is transparent, then add your buttons:
flex spacer, left arrow, custom view with stacked UILabels, right arrow, flex spacer

Related

How to remove NavigationBar Right space?

I'm playing with Navigation Bar Buttons I need a button from right edge with 0 space, after google search i found below code
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
negativeSpacer.width = -25;
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
But unfortunatly in iOS 13 negativeSpacer not working. can anybody suggest me solutions.
Attached screenshot here
Thanks.
Let's try this easy way, Let's define the width of space BarButtonItem to be width of self.view.frame.size.width - width of the button.
self.filterButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 80, 45)];
[self.filterButton setBackgroundColor:[UIColor redColor]];
UIImage *buttnImage=[UIImage imageNamed:#"chemistry-filter"];
[self.filterButton setImage:buttnImage forState:UIControlStateNormal];
[self.filterButton addTarget:self action:#selector(filterAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc]
initWithCustomView:self.filterButton];
UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil
action:nil];
negativeSpacer.width = self.view.frame.size.width - 45; // As 45 is the frame width of your filter button
[self.navigationItem setRightBarButtonItems:[NSArray
arrayWithObjects:negativeSpacer,btn2, nil] animated:YES];
Hope this helps!

Navigation doesn't respond on iPad with iOS 8

Idea is very simple. I have navigation bar in universal storyboards with custom controls.
UIButton *menu = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 35)];
[menu setImage:[UIImage imageNamed:#"menu"] forState:UIControlStateNormal];
[menu addTarget:self.viewDeckController action:#selector(toggleLeftView) forControlEvents:UIControlEventTouchUpInside];
UIButton *refresh = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[refresh setImage:[UIImage imageNamed:#"refresh"] forState:UIControlStateNormal];
[refresh addTarget:self action:#selector(reloadPosts) forControlEvents:UIControlEventTouchUpInside];
UIButton *share = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[share setImage:[UIImage imageNamed:#"share"] forState:UIControlStateNormal];
[share addTarget:self action:#selector(showActionView) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *menuBtn = [[UIBarButtonItem alloc] initWithCustomView:menu];
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithCustomView:refresh];
UIBarButtonItem *shareBtn = [[UIBarButtonItem alloc] initWithCustomView:share];
UIBarButtonItem *negativeSpacerLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerLeft setWidth:-10];
UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacerRight setWidth:-10];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerLeft, menuBtn, nil];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight, shareBtn, refreshBtn, nil];
Everything works correctly - navigation bar has implemented style and buttons/icons. But there is a problem with iPad and iOS 8 (works on iOS7). When I tap on any button, it does not respond. What I did wrong? It works as well on iPhone with iOS 7/8.

Adding leftbarbuttons and custom title to navigation bar in iOS

I am adding Custom title and leftbar buttons to navigation bar using following code:-
UIBarButtonItem *button1 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img1.png"] style:UIBarButtonItemStylePlain target:self action:#selector(popBack)];
UIBarButtonItem *button2 =[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"img2.png"] style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWith:button1, button2, nil];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 360, 30)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:14]];
[label setBackgroundColor:[UIColor clearColor]];
[label setTextColor:[UIColor whiteColor]];
[label setText:#"Detail Controller"];
label.textAlignment = UITextAlignmentCenter; //NSTextAlignmentCenter
self.navigationItem.titleView=label;
Here, left bar buttons are not been placed properly and title alignment not coming properly(left bar buttons are pushing title to right side of the navigation bar).

Making UIBarButtonItem act like UILabels on a UIToolbar, not dimmed and not glowing on touch

I have added a UIBarButtonItem to a UIToolbar, and made it emulate a UILabel as per this post.
Here is some sample code:
UIToolbar * toolBar = [[UIToolbar alloc] initWithFrame:toolbarInitialFrame];
UIBarButtonItem * labelEmu = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStylePlain target:self action:nil];
[labelEmu setEnabled:NO];
/* some more buttons */
[toolBar setItems:[NSArray arrayWithObjects:labelEmu, spacer, doneButton, nil]];
[container addSubview:toolBar];
My problem is that with setEnabled:NO the button is inactive but dimmed, and with setEnabled:YES the button is not dimmed, but glows when pressed.
How can I get this button to be not dimmed AND inactive (so it doesnt glow when pressed)?
Try using the UIBarButtonItem with a custom view like this:
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 25)];
[label setText:#"Settings"];
[label sizeToFit];
UIBarButtonItem * labelEmu=[[UIBarButtonItem alloc] initWithCustomView:label];

UINavigationitem custom rightBarButtonItems

i have 2 custom right bar button items, and in portrait mode they overlays each others and only one of them is visible, but on landscape mode both are visible. items are created with custom view which is the UIButton with background image.
optionsBUtton=[UIButton buttonWithType:UIButtonTypeCustom];
[optionsBUtton setImage:[UIImage imageNamed:#"optionsIcon.png"] forState:UIControlStateNormal];
[optionsBUtton setBackgroundImage:[UIImage imageNamed:#"optionsBtn.png"] forState:UIControlStateNormal];
[optionsBUtton sizeToFit];
UIBarButtonItem* btnOptions=[[UIBarButtonItem alloc] initWithCustomView:optionsBUtton];
searchButton=[UIButton buttonWithType:UIButtonTypeCustom];
[searchButton setImage:[UIImage imageNamed:#"searchIcon.png"] forState:UIControlStateNormal];
[searchButton setBackgroundImage:[UIImage imageNamed:#"optionsBtn.png"] forState:UIControlStateNormal];
[searchButton sizeToFit];
UIBarButtonItem* btnSearch=[[UIBarButtonItem alloc] initWithCustomView:searchButton];
rightButtonItems=[[NSArray alloc] initWithObjects:btnOptions,btnSearch, nil];
navItem.rightBarButtonItems=rightButtonItems;
you must user tool bar and set the toolbar with buttons here is example code
// create a toolbar where we can place some buttons
UIToolbar* toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(saveAction:)];
saveButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:saveButton];
[saveButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemTrash
target:self
action:#selector(deleteAction:)];
deleteButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:deleteButton];
[deleteButton release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar] autorelease];
[toolbar release];
Thanks..!

Resources