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];
Related
I have tried to add a UIBarButtonItem to my UINavigation bar but I can't get it to show up:
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:self.webView];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:NSLocalizedString(#"Back", #"")
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
[self.navigationItem setLeftBarButtonItem: backButton];
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
self.webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
[self.webView loadRequest:request];
}
- (void)remove:(id)sender
{
[self removeFromParentViewController];
[self.webView stopLoading];
self.webView.delegate = nil;
}
I can't figure out why the button isn't there. Any help would be much appreciated!
Thanks.
Before add UIBarButtonItem to UINavigationBar, First change your UIWebView frame,
self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width,self.view.frame.size.height - 50 )];
Here add (-50) to height of UIWebView.
And now, i just write code for how to add UIBarButtonItem to UINavigationBar:
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:navBar];
UIBarButtonItem *Button = [[UIBarButtonItem alloc]
initWithTitle:#"MyButton"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(buttonAction)];
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"This is My Title "];
item.rightBarButtonItem = Button;
item.hidesBackButton = YES;
[navBar pushNavigationItem:item animated:NO];
Here is Method name is buttonActionthat execute/call when you tapped on UIBarButtonItem.
-(void)buttonAction
{
//stuff of code;
}
remove these two lines from your code and check it,
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
or you can add like this,
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"remove"
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"remove"];
[item setLeftBarButtonItem:backButton];
[myBar setItems:[NSArray arrayWithObject:item]];
Richard, I understand what you want to achieve but I am not sure whether you are using a navigation controller already.
Case 1: using UINavigationController
If you are using Navigation Controller already then you don't need to add new Navigation bar.
If you are using NavigationController then show the use the bar from Navigation Controller and your code will work.
below two lines would not be required.
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
Case 2: Not using UINavigationController
If you are not using UINavigationController then you are required to add a Bar and add Bar button in the navigation item of Bar.
UINavigationBar *myBar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[self.view addSubview:myBar];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"remove"
style:UIBarButtonItemStyleDone
target:self
action:#selector(remove:)];
UINavigationItem *item = [[UINavigationItem alloc]initWithTitle:#"remove"];
[item setLeftBarButtonItem:backButton];
[myBar setItems:[NSArray arrayWithObject:item]];
and you need to remove following line
[self.navigationItem setLeftBarButtonItem:backButton];
It will work perfectly. Let me know if you find any issues.
How can i add the activity indicator beside the right bar button item (CreateNew button)?
One of the approaches that you can use is initializing the Bar Button using some custom view.
A bar minimum code which can help you get some hint is
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIView* aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, 40)];
[aView setBackgroundColor:[UIColor blackColor]];
[[aView layer] setCornerRadius:8.0f];
[[aView layer] setBorderColor:[UIColor whiteColor].CGColor];
UIActivityIndicatorView* loadView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[loadView startAnimating];
[aView addSubview:loadView];
[loadView setFrame:CGRectMake(5, 5, 30, 30)];
[loadView release];
UIButton* aButton = [[UIButton alloc] initWithFrame:CGRectMake(30, 2, 80, 35)];
[aButton setImage:[UIImage imageNamed:#"btnLogin.png"] forState:UIControlStateNormal];
[aView addSubview:aButton];
[aButton release];
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:aView];
[self.navigationItem setRightBarButtonItem:barButton];
[aView release];
[barButton release];
}
This is If you do it programmatically , else you can also make use of nib. Creating bar button this way will look something like this -
You can look for more option making using of [[UIBarButtonItem alloc] initWithCustomView:aView]; method.
Hope it helps!!
For this try to customize UINavigationController Class.
Add Activity Indicator on navigationBar and control it from your ViewController.
You can add the activity indicator using the following code:
//Create an instance of activity indicator view
UIActivityIndicatorView * activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
//set the initial property
[activityIndicator stopAnimating];
[activityIndicator hidesWhenStopped];
//Create an instance of Bar button item with custome view which is of activity indicator
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
//Set the bar button the navigation bar
[self navigationItem].rightBarButtonItem = barButton;
//Memory clean up
[activityIndicator release];
[barButton release];
Please refer this link for detailed code.
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!
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];
I am working on navigation based apps...firstly when I press alert button..its shows label,button,image and below tabbar items...how can we add tab bar items?
The most multipurpose way is to add transparent UIView and then add anything to it:
UIView *buttonsHandlerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonsHandlerView.backgroundColor = [UIColor clearColor];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonsHandlerView];
[self.navigationItem setRightBarButtonItem:backButtonItem animated:YES];
[backButtonItem release];
//[buttonsHandlerView addSubview:__anything__];
[buttonsHandlerView release];
Or init UIBarButtonItem with title/type and use it as button