UISearchBar with UINavigationController - iphone

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

Related

How to add a UIBarButtonItem of custom width to a UINavigationItem programmatically without using an image

I'm using iOS 6.1 and want to add a UIBarButtonItem of custom width with title "Info" to the UINavigationItem programmatically when the view loads. I don't want to use an image to achieve this.
I read several threads about how to change the width of a UIBarButtonItem and none seem to work for me.
I tried this but the button does not appear:
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 44)];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
[infoButton2 setStyle:UIBarButtonItemStyleBordered];
[infoButton2 setTitle:#"Info"];
self.navigationItem.rightBarButtonItem = infoButton2;
}
What's wrong with this code?
//This worked for me
UIButton *searchButton =[[UIButton alloc]init];
searchButton.frame=CGRectMake(0, 0, 200, 40);
[searchButton setTitle:#"Info" forState:UIControlStateNormal];
searchButton.backgroundColor= [UIColor greenColor];
[searchButton addTarget:self action:#selector(onFilter) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchBarButton=[[UIBarButtonItem alloc] initWithCustomView:searchButton] ;
self.navigationItem.rightBarButtonItem = searchBarButton;
you can also add button just like this..
- (void)viewDidLoad
{
UIView *infoButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 50, 44)];
[infoButtonView setBackgroundColor:[UIColor clearColor]];
UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0,15,45,21)];
[button1 setTitle:#"Info" forState:UIControlStateNormal];
button1.backgroundColor=[UIColor redColor];
[infoButtonView addSubview:button1];
UIBarButtonItem *infoButton2 = [[UIBarButtonItem alloc] initWithCustomView:infoButtonView];
self.navigationItem.rightBarButtonItem = infoButton2;
}

Programmatically add UILabel to toolbar

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!

Show popover from CGRect of button

I created a button programmatically to be added to the rightBarButtonItem. I had to create a few buttons in the upper right with the UINavigationController, so I put them in a containerView, and then, put that as the right barButtonItem customView in the init method. If my button and containerView are declared like this:
UIButton *filterButton = [[UIButton alloc] initWithFrame:CGRectMake(49, 0, 44, 44)];
[filterButton setTitle:#"Filter" forState:UIControlStateNormal];
[filterButton addTarget:self action:#selector(FilterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 248, 44)];
How do I get the popover to present from the filterButton rect? My method looks like this so far, but it is not correct.
- (IBAction)FilterButtonPressed:(id)sender {
// other code
[self.FilterPopover presentPopoverFromRect:button.superview.bounds inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I display popover usually like this:
[self.FilterPopover presentPopoverFromRect:button.bounds inView:button permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

UIScrollView with UITapGestureRecognizer and UIPageControl

I'm creating a horizontal scrolling tableview containing images. I have the swiping functionality working great, and am able to add the images to the scrollView as so in cellForRowAtIndexPath:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(700, 78)];
UIImage *footImage = [UIImage imageNamed:#"Foot.png"];
UIImageView *footImageView = [[UIImageView alloc] initWithImage:footImage];
footImageView.frame = CGRectMake(0, 0, 80, 78);
footImageView.userInteractionEnabled = YES;
[scrollView addSubview: footImageView];
UIImage *handImage = [UIImage imageNamed:#"Hand.png"];
UIImageView *handImageView = [[UIImageView alloc] initWithImage:handImage];
handImageView.frame = CGRectMake(90, 0, 80, 78);
handImageView.userInteractionEnabled = YES;
[scrollView addSubview: handImageView];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(highlightImage)];
[scrollView addGestureRecognizer:tapGesture];
NSLog(#"tapGesture added to scrollView");
[[cell contentView] addSubview:scrollView];
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 50, tv.frame.size.width, 50)];
[pageControl setNumberOfPages:4];
[[cell contentView] addSubview:pageControl];
My tapGesture is registering with the selector, hightlightImage, and just logging that its calling this method correctly.
- (void) highlightImage
{
NSLog(#"tapping tapping");
}
What I REALLY am working for is the ability to highlight/select these images if tapped, and highlight/deselect them if tapped again. I will have another button on screen that will navigate to the next page (irrelevant here).
Am I going down the right path here? Obviously I will populate an NSArray of UIImages and populate the scrollView that way, but just spiking it out for now. If someone could give me some direction or an example of how to make each button separately selectable/de-selectable, that would be GREAT:)
Use UIButton instead of UIImageView.
It's got built-in selected functionality.
get rid of your gestures and add highlightImage as the action to every button.
make your highlightImage into highlightImage:(id)sender
sender should be a button so you can just do
[sender setSelected:YES];

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