Programmatically add UILabel to toolbar - iphone

I am trying to add a UILabel programmatically into my UIToolBar but it dose not seem to be appearing. This is what I am doing with my code.
- (void) viewWillAppear:(BOOL)animated
{
// Create custom toolbar at top of screen under navigation controller
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
matchingSeriesInfoToolBar = [UIToolbar new];
[matchingSeriesInfoToolBar sizeToFit];
CGFloat toolbarHeight = 30;
CGRect mainViewBounds = [[UIScreen mainScreen] applicationFrame];
[matchingSeriesInfoToolBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds), 0, CGRectGetWidth(mainViewBounds), toolbarHeight)];
matchingSeriesInfoToolBar.tintColor = [UIColor darkGrayColor];
[self.view addSubview:matchingSeriesInfoToolBar];
// Create size of uitableview (to fit toolbar.
[matchingSeriesTableView setFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30)];
[self.view addSubview:matchingSeriesTableView];
// Ad UILabel to the toolbar
UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:manufSelectionLabel];
matchingSeriesInfoToolBar.items = [NSArray arrayWithObject:textFieldItem];
manufSelectionLabel.text = #"Hello World!";
[super viewWillAppear:animated];
}
So pretty much I have created a custom toolbar which I have changed the usual location from the bottom of the screen, to appear under the UINavigationController, this is also added to the view like this so it animated properly in the view transitions..
After which I create the size of the tableview so that it appears after the custom toolbar..
then from there I am trying to add a UILabel to the toolbar.. but for some reason its not working out.
Any help would be greatly appreciated.

You must actually create the label somewhere. This code works just fine.
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolbar];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
label.backgroundColor = [UIColor clearColor];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:label];
toolbar.items = [NSArray arrayWithObject:item];
label.text = #"Hello World";

In the code you posted you don't ever make a UILabel. Your comment says Ad UILabel to the toolbar but you then proceed to make a UIBarButtonItem with a custom view manufSectionLabel.
Where is the code which creates manufSectionLabel?
PS This line does nothing :
[matchingSeriesInfoToolBar setFrame:CGRectMake(0, 60, 320, 30)];
because at that point matchingSeriesInfoToolbar is nil - you haven't made it yet!

Related

UISearchBar with UINavigationController

I added a UISearchBar and UISearchDisplayController with a UINavigationController. Because we needed a few buttons in the navigationBar, I created a custom view that contains a few buttons, and put that as the rightBarButtonItem. I notice when I click on the search bar, the keyboard pops up from the bottom, and also moves the UINavigationController's navigationBar off the screen. So now I cannot see what I enter in my UISearchBar. Is there a way to get around this? I've seen other apps that it looks like they use a UINavigationBar + multiple buttons on the LHS or RHS of the navigationBar, so I'm assuming this is possible. Thanks.
UIButton *homeButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
UIImage *house = [UIImage imageNamed:#"icon_house.png"];
[homeButton setImage:house forState:UIControlStateNormal];
[homeButton addTarget:self action:#selector(homePressed:) forControlEvents:UIControlEventTouchUpInside];
UIButton *filterButton = [[UIButton alloc] initWithFrame:CGRectMake(49, 0, 44, 44)];
[filterButton setTitle:#"Filter" forState:UIControlStateNormal];
[filterButton addTarget:self action:#selector(FilterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
self.SearchEntry = [[[UISearchBar alloc] initWithFrame:CGRectMake(98, 0, 150, 44)] autorelease];
self.SearchEntry.delegate = self;
UISearchDisplayController *searchDisplayCtlr = [[UISearchDisplayController alloc] initWithSearchBar:_searchEntry contentsController:self];
searchDisplayCtlr.searchResultsDataSource = self;
searchDisplayCtlr.searchResultsDelegate = self;
searchDisplayCtlr.delegate = self;
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 248, 44)];
[containerView addSubview:homeButton];
[containerView addSubview:filterButton];
[containerView addSubview:_searchEntry];
Try to adjust the contentSize of the UIScrollView. Also please take care of the Hierarchy of the controls you have taken. Dont keep UINavigationBar and UISearchView in ScrollView, then it will not move and remains fixed in their position

Background showing when adding UISearchBar to UINavogationBar

I am using the code posted below to add Search bar to Navigation bar.
I am getting everything to show up correctly but there is a background (mostly of the UIBarButtonItem that I am not able to get rid of). - Please check the screenshot for iPad.
Is there a way to get rid of blue backgorund showing behind the search bar?
Thanks
Dev.
- (void) viewDidLoad {
[super viewDidLoad];
UIView *hackView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 30)];
hackView.backgroundColor = [UIColor clearColor];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, 250, 30)];
//[searchBar sizeToFit];
[searchBar setBackgroundColor:[UIColor clearColor]];
[hackView addSubview:searchBar];
[searchBar release];
UIBarButtonItem *hackItem = [[UIBarButtonItem alloc] initWithCustomView:hackView];
[hackItem setWidth:250];
self.navigationItem.rightBarButtonItem = hackItem;
[hackView release];
[hackItem release];
}
I believe the problem is that you are adding that extra UIView. Even though it has clearColor background, it is capturing that CGRect created for the UIView *hackView. Get rid of it and simply add the UISearchBar to the UIBarButtonItem and it should solve the display issue.
Also, don't forget to implement the UISearchBarDelegate for proper functionality.
I have solved this problem using following code.
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
UIBarButtonItem *navRight = [[UIBarButtonItem alloc] initWithCustomView:searchBar];
[[self navigationItem] setRightBarButtonItem:navRight];
[searchBar release];
[navRight release];

How to enable UISearchBar grey out view?

I am using a UISearchBar and once the user taps the bar (and the keyboard pops up) I want the everything besides the keyboard and UISearchBar to be greyed out. Similar to safari when the search is selected. I do not need past searches. See here:
Anyone have any ideas? I've looked through the questions here on StackOverflow and I can't find anything specific.
Thanks!
You can create a UIView that contains the UISearchBar. And then you can set UIView's backgroundColor to get the gray background.
And here's some sample code:
UIView *view1 = [[UIView alloc] init];
view1.frame = CGRectMake(0, 20, 320, 460);
view1.backgroundColor = [UIColor grayColor];
UISearchBar *searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 320, 50);
[view1 addSubview:searchBar];
[searchBar release];
[self.window addSubview:view1];
[view1 release];

Can you add a UIView above a UINavigationBar?

I have seen it done on some apps, where the navigation bar is actually smaller than the default 44px, and there is a UIView (which has functionality) above the nav bar...
I want more than a custom background image, which I did manage to figure out how to do, but I dont know where to start getting something like this done.
Any help is greatly appreciated :)
Mark
I found a sort of way to do this:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UIImageView *back = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"logo.png"]];
[back setFrame: CGRectMake(0, 0, 320, 20)];
[tempView addSubview: back];
[[self view] addSubview: tempView];
[[self view] addSubview: navBar];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle: #"Controls"];
[navBar pushNavigationItem:navItem animated:NO];
which seems to do the trick, although I cannot seem to figure out how to get this 'into' the navigationController so that back buttons work, at the moment I have to manually insert a leftBarButtonItem into the navItem, the back button never seems to show...
Yes you can,
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 20.0f, 320.0f, 32.0f)];
UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
navBar.navigationBar.layer.zPosition =-1;
self.view insertSubview:navBar atIndex:[[self.view subviews] count]];
[self.view insertSubview:tempView atIndex:[[self.view subviews] count]];

UITableView only taking up part of screen

Im trying to make my own calendar similar to iCal and all is going well except for one thing. I plan to have the calendar selection up the top then a table list down the bottom. I can do this but then the calendar is in the same subview as the table and scrolls with it. Although I am currently not using a nib if I build it in a nib then the table wont resize to take up whatever the calendar doesn't. ie it should be larger in say February where as December will be small (exactly like the apple iCal version) I have seen other apps do this. Any ideas on how I would go about this?
Add the tableview and the calendar view separately to the main view.
Example:
- (void)loadView {
[super loadView];
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
topView.backgroundColor = [UIColor darkGrayColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, 280, 60)];
label.text = #"Stationary top view";
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = UITextAlignmentCenter;
[topView addSubview:label];
[label release];
[self.view addSubview:topView];
[topView release];
UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, 320, 380) style:UITableViewStyleGrouped];
tableview.delegate = self;
tableview.dataSource = self;
[self.view addSubview:tableview];
[tableview release];
}
Screenshot:
alt text http://static.benford.name/tableview_with_topview.png