Adding leftbarbuttons and custom title to navigation bar in iOS - 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).

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!

Setting image for UINavigationitem (Leftbarbutton item) in ios

I m trying to setting image for UIBarButtonItem using the below code. But it appears as a blank image in the navigation bar. please help me to resolve the problem. i m entirely new to ios.
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
[self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:38.0/255.0 green:126.0/255.0 blue:202.0/255.0 alpha:0.5]];
UIBarButtonItem *lftbarbtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"Cross_Icon.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(additem)];
[self.navigationItem setLeftBarButtonItem:lftbarbtn];
thank u in advance.......
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(popToBack)]; //create the left bar button
self.navigationItem.leftBarButtonItem = leftBarButton; //assign into navigation item
self.navigationItem.leftBarButtonItem.tintColor=[UIColor colorWithRed:125.0/255.0 green:90.0/255.0 blue:146.0/255.0 alpha:1]; //set the tint color
[leftBarButton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor purpleColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal]; //text color
-(void)popToBack
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
in another choice
UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[leftbutton setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[leftbutton addTarget:target action:#selector(buttonAction)forControlEvents:UIControlEventTouchUpInside];
[leftbutton setFrame:CGRectMake(0, 0, 53, 31)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(3, 5, 50, 20)];
[label setFont:[UIFont fontWithName:#"Arial-BoldMT" size:13]];
[label setText:title];
label.textAlignment = UITextAlignmentCenter;
[label setTextColor:[UIColor whiteColor]];
[label setBackgroundColor:[UIColor clearColor]];
[leftbutton addSubview:label];
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView: leftbutton];
self.navigationItem.leftBarButtonItem = barButton;
-(void) buttonAction
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
You do not need to use the navigation bar directly. Use the current view controller's navigation item property to set the UIBarButtomItems. Make sure you are actually using a navigationController to set these properties.
UIButton *ReqMenuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
ReqMenuBtn.frame = CGRectMake(0,0,32,32);
[ReqMenuBtn setBackgroundImage:[UIImage imageNamed:#"reselect.png"] forState:UIControlStateNormal];
[ReqMenuBtn addTarget:self action:#selector(somemethodName:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *MenuButton = [[UIBarButtonItem alloc]initWithCustomView:ReqMenuBtn];
[self.navigationItem setLeftBarButtonItem:MenuButton];
MenuButton = nil;
ReqMenuBtn = nil;

Why my UIButton doesn't display in my subview? iPad App

Could somebody help me to understand why my UIButton doesn't display in my custom UIView?
In the code below I posted initWithFrame method:
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor =[UIColor whiteColor];
UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,frame.size.width, 50)];
toolbar.backgroundColor = [UIColor grayColor];
UIBarButtonItem *cancelBarButton = [[UIBarButtonItem alloc]initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissNewInvoiceView:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *saveBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Save" style:UIBarButtonItemStylePlain target:self action:#selector(saveNewInvoice:)];
[saveBarButton setEnabled:NO];
NSArray *arrayOfItems = [[NSArray alloc]initWithObjects:cancelBarButton,flexibleSpace, saveBarButton, nil];
[toolbar setItems:arrayOfItems];
UILabel *headerViewLabel = [[UILabel alloc]initWithFrame:CGRectMake(335, 15, 200, 20)];
headerViewLabel.text = #"New Invoice";
[toolbar addSubview:headerViewLabel];
[self addSubview:toolbar];
UIButton *selectCustomerButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 200, 60, 60)];
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"];
[self addSubview:selectCustomerButton];
[self bringSubviewToFront:selectCustomerButton];
UILabel *invoiceNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(25, 55, 150, 25)];
[invoiceNumberLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
invoiceNumberLabel.text = #"Invoice number:";
[self addSubview:invoiceNumberLabel];
UITextField *invoiceNumberField = [[UITextField alloc]initWithFrame:CGRectMake(140, 53, 150, 30)];
[invoiceNumberField setFont:[UIFont fontWithName:#"Verdana" size:25]];
invoiceNumberField.placeholder = #"25/13";
[self addSubview:invoiceNumberField];
UILabel *dateOfCreationLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 55, 150, 25)];
[dateOfCreationLabel setFont:[UIFont fontWithName:#"Verdana" size:14]];
dateOfCreationLabel.text = #"Date of creation:";
[self addSubview:dateOfCreationLabel];
}
return self;}
Why UILabels are displaying properly by implement addSubview method while UIButtons are not?
Regards
Don't do this:
selectCustomerButton.imageView.image = [UIImage imageNamed:#"Farmer.png"]
Use
[selectCustomerButton setImage:[UIImage imageNamed:#"Farmer.png"] forState:UIControlStateNormal];
The button sets its own image views image depending on its state, you don't access and set the image view's image yourself.
The button will be just invisible in case the image is not loaded/shown properly. As jrturton already said, you should use another method to set the image.
To figure out if the button is 'there', you can give the button a backgroundcolor like this:
selectCustomerButton.backgroundColor = [UIColor redColor];
If you see a red box, you know the image is not properly loaded or maybe not even found.

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

How To add buttons to UINavigationBar

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

Resources