Changing UINavigationBar later in View Controller - ios

I have a view controller that does not have a NavigationController (and can't). I define a navigation bar like this.
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.backgroundColor = UIColorFromRGB(0x72CE97);
navBar.barTintColor = UIColorFromRGB(0x72CE97);
//Keep the container for Navigation Items
UINavigationItem *navItem = [[UINavigationItem alloc] init];
UIImage* logoImage = [UIImage imageNamed:#"nav_logo_small.png"];
navItem.titleView = [[UIImageView alloc] initWithImage:logoImage];
//Add the left button as an Item to Navigation Items
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(backButton:)];
navItem.leftBarButtonItem = leftButton;
//Add Navigation Items Container to Navigation Bar
navBar.items = #[ navItem ];
//Display the Navigation Bar
[self.view addSubview:navBar];
[navBar release];
How do I change the background later in the View Controller?
Below doesn't work. I think it is because I don't have a NavigationController:
NavigationBar *navigationBar = self.navigationController.navigationBar;
navigationBar.backgroundColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];
navigationBar.barTintColor = [UIColor colorWithRed:197.0f/255.0f green:174.0f/255.0f blue:135.0f/255.0f alpha:1.0];

Try this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 20, [UIScreen mainScreen].bounds.size.width, 41)];
navBar.tag = 1;
...
NavigationBar *navigationBar = (NavigationBar *)[self.view viewWithTag:1];

Related

How to add multiple bar buttons to navigation bar

Initially i have created a dynamic view called 'mainView'.In that,i have added Navigation bar.After that, i Want to add 2 right bar buttons to the navigation bar.If i code like this,its shows view with navigation bar.but its not showing right bar button.
1.Is it possible to add navigation bar without navigation controller?
2.Why the right bar button is not showing?
//mainView Creation
UIView *mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
mainView.backgroundColor =[UIColor grayColor];
[self.view addSubview:mainView];
//NavigationBar
UINavigationBar *NavigationBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 45)];
NavigationBar.backgroundColor = [UIColor orangeColor];
[mainView addSubview:NavigationBar];
//rightbarView Creation
UIView *rightbarView = [[UIView alloc] init];
rightbarView.backgroundColor = [UIColor orangeColor];
//powerCut Button
UIButton *powerCutbutton = [[UIButton alloc] initWithFrame:CGRectMake(150, 20, 50, 14.01)];
[rightbarView addSubview:powerCutbutton];
UIImage *powercutimage = [UIImage imageNamed:#"powercut.jpg"];
[powerCutbutton setImage:powercutimage forState:UIControlStateNormal];
// addConnection button
UIButton *addConnectionbutton = [[UIButton alloc] initWithFrame:CGRectMake(210, 20, 50, 14.01)];
[rightbarView addSubview:addConnectionbutton];
UIImage *addimage = [UIImage imageNamed:#"add.png"];
[addConnectionbutton setImage:addimage forState:UIControlStateNormal];
// Barbutton item
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:rightbarView];
//[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:powerCutbutton, addConnectionbutton, nil]];
// setting rightbar button item
self.navigationItem.rightBarButtonItem = item;
create an array of bar buttons. and setLeftBarButtonItems not setLeftBarButtonItem as :
UIBarButtonItem * barbtn1;
UIBarButtonItem * barbtn2;
UIBarButtonItem * barbtn3;
[self.navigationItem setLeftBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
or
[self.navigationItem setRightBarButtonItems:#[barbtn1,barbtn2,barbtn3]];
hope it will helpful for you

Search Bar in Navigation Bar titleView pops out of UINavigationItem on orientation change?

I have added a search bar to the titleView of my Navigation Bar.
It looks good except for when the orientation changes. For whatever reason the search bar is changing position. It's as if the search bar is popping out or is too big for the Navigation Bar all of a sudden.
Has anyone else had this problem and knows how to fix it? I am using storyboards and was wondering if there is a setting that I am missing or something. Here is my code:
-(void) viewDidAppear:(BOOL)animated
{
UIImage *addImage = [UIImage imageNamed:#"reveal-icon.png"];
UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
[addButton setFrame:CGRectMake(0, 0, addImage.size.width, addImage.size.height)];
[addButton setBackgroundImage:addImage forState:UIControlStateNormal];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:addButton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
UIView *searchBarView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 260.0, 44.0)];
searchBarView.autoresizingMask = 0;
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:searchBarView.frame];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchBar.delegate = self;
[searchBarView addSubview:searchBar];
self.navigationItem.titleView = searchBar;
}

putting labels, buttons on the navigation bar iOS

I have created custom navigation controller,
I want to be added, a date at the left, a back button on the right and the title next to back button.
I tried to add one label, but it does not work. Please show me a way
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(20,8,280,30)];
navLabel.text = #"My Text";
[self.navigationController.navigationBar addSubview:navLabel];
[self.view addSubview:naviBarObj];
UINavigationBar *naviBarObj = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 1024, 66)];
UILabel *navLabel = [[UILabel alloc] initWithFrame:CGRectMake(200,8,200,30)];
navLabel.text = #"My Text";
navLabel.textColor = [UIColor redColor];
[naviBarObj addSubview:navLabel];
[navLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:naviBarObj];
try this it will work .It works for me :)
add custom view in UIToolbar
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f,190.0f, 44.01f)];
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] init];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
bi.width =10.0f;
[buttons addObject:bi];
[buttons addObject:add your view];
[tools setItems:buttons animated:NO];
UIBarButtonItem *buttons_ = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = buttons_; //same to leftBarButtonItem
You can also achieve this from Interface Builder
1. First add a UIView which will look like following
2. Then add a UILabel inside the UIView which will look like following
3. In the Center It will look like following
Now add necessary constraint to meet your requirement.
You need to add navigation item to the navigation bar. One per each navigation level. So you should have at least one of them.
** update **
UINavigationItem * myNavItem = [[UINavigationItem alloc] initWithTitle:#"Some title"];
/*
* add buttons and labels
*/
[self.navigationController.navigationBar pushNavigationItem:myNavItem animated:NO];
You need to change the titleView of the topItem. Like this:
naviBarObj.topItem.titleView = navLabel;
** update: Complete code (tested and working) **
UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(.0, .0, 320.0, 44.0)];
UINavigationItem *navItem = [[UINavigationItem alloc] init];
navItem.titleView = titleLbl;
[naviBar pushNavigationItem: navItem animated: NO];
[navItem release]; // you don't need this for ARC
[self.view addSubview: naviBar];
[naviBar release]; // you don't need this for ARC

UIBarButton not showing in UINavigationBar

I need help with my UIBarButtonItem not showing in my UINavigationBar.
I am trying to put a UINavigationBar in my third view. As I am not quite sure if UINavigationController will work, I decided to manually initialized a UINavigationBar, together with its UINavigationItem initwithTitle. The code works, but my problem is adding a UIBarButtonItem because it won't show in the UINavigationBar.
- (void)viewDidLoad
{
[super viewDidLoad];
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:#"Title"];
[navigationBar pushNavigationItem:navigationItem animated:NO];
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(showPI:)];
self.navigationItem.rightBarButtonItem = button;
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0.0, 44.0, self.view.frame.size.width, self.view.frame.size.height - navigationBar.frame.size.height) style:UITableViewStylePlain];
self.tableView = tableView;
[self.view addSubview:tableView];
[self.view addSubview:navigationBar];
[self showCurrencies];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
I'm not sure what's missing.
In your case, you're creating your own navigationBar. Try not to alloc init your navigationItem, instead use
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
UINavigationItem *item = [navigationBar.items lastObject];
Then set your created barbuttonItem to the item's rightBarButtonItem,
item.rightBarButtonItem = button;
It's works for me.

Can't click bar button on Tool bar?

In my iPad app, i add navigation bar and tool bar manually.
I also add scroll view and image view by coding.
My problem is i can see both navigation bar and tool bar.
I can click button on navigation bar.
But, i can't click bar button on tool bar.
How can i do it?
This is my code.
- (void)viewDidLoad
{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,nil];
UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)];
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:ScrollView];
}
I dont Know what is your exact problem, Because your displayed code is not related to UIToolBar.
But Following code i added two UIBarButton with flexSpace.
you can add UIBarButton as you want
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
Toolbar = UIBarStyleBlackTranslucent;
[Toolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(Cancel)];
[barItems addObject:btnCancel];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(done)];
[barItems addObject:btnDone];
[Toolbar setItems:barItems animated:YES];
Following Method is call when tapped on bar Button
-(void)Cancel
{
// Write Code for Cancel Method
}
-(void)done
{
// Write Code for Done Method
}
This code might helpful in your own case.
Thanks :)
Thanks everybody. I can solve my problem.
I change my code.
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
To this.
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];

Resources