UISearchBar in UIView in UIBarButtonItem does not work as expected - objective-c

Im trying to add UIsearchBar to my navigation controller. Also i wish to use cancel button of UISearchBar.
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 300, searchBar.frame.size.height);
searchBar.translucent = NO;
searchBar.barStyle = UIBarStyleBlack;
searchBar.delegate = self;
UIView* myView = [[UIView alloc] initWithFrame:searchBar.frame];
[myView addSubview:searchBar];
UIBarButtonItem* sbItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:chart,sbItem,nil]];
trouble that searchBar is inactive. it appears in navigation controller. But when i tap on it it does not response at any way. If i show cancel button it appears but also not clickable.
If i doesnt wrap UIsearchBar at UIView it work fine except it doesnt want to show me a cancel button.
UPD
IF i call
[searchBar becomeFirstResponder]
it goes to edit mode and shows me a cancel button and keyboard, but looks like view in which searchBar wrapped changes it frame params and goes on 10px down from its original appearence. And change its background from trnsparent to blackopaque
Looks like a some kind of Voodoo... So simple action takes so much time...

i think you have mistaken at search bar frame,you took search bar height as searchBar.frame.size.height,but there was no height at that time,just change it like:
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 300, 40);
searchBar.translucent = NO;
searchBar.barStyle = UIBarStyleBlack;
searchBar.delegate = self;
UIView* myView = [[UIView alloc] initWithFrame:searchBar.frame];
[myView addSubview:searchBar];
UIBarButtonItem* sbItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:chart,sbItem,nil]];
i am sorry,i don't know what the chart is here,hope it is what you are looking for.

Related

UIToolbarItem not selecting when inserted as subview of UIPickerView

I have a UIPickerView which has a UIToolbar placed as a subview. The problem is when I click on the UIbarviewItem nothing happens. I am not sure why, but if I insert the UIToolbar as a subview to the main view ([self.view addSubview:toolbar]) the button works fine, but not as a subview for the uIPickerView?
Here is my code I used to create the uipickerview and the uitoolabr:
dateRangePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.view.frame)-200, CGRectGetWidth(self.view.frame), 220)];
dateRangePickerView.showsSelectionIndicator = YES;
dateRangePickerView.hidden = NO;
[dateRangePickerView reloadAllComponents];
dateRangePickerView.showsSelectionIndicator = YES;
dateRangePickerView.delegate = self;
dateRangePickerView.dataSource = self;
[dateRangePickerView setBackgroundColor:[UIColor whiteColor]];
[self.view addSubview:dateRangePickerView];
// Add toolbar
toolBar= [[UIToolbar alloc] initWithFrame:CGRectMake(0, -44, CGRectGetWidth(self.view.frame), 44)];
[toolBar setBarStyle:UIBarStyleDefault];
UIBarButtonItem *barButtonDone = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(selectDateRange)];
toolBar.items = [[NSArray alloc] initWithObjects:barButtonDone,nil];
barButtonDone.tintColor=[UIColor blackColor];
[dateRangePickerView addSubview:toolBar];
Could anybody tell me where am I going wrong?
Thanks,
Peter
I decided instead of adding a subview to the UIPicker, I placed the UIPicker and UIToolbar in a UIView to keep everything together.
Thanks all for your help :)

How display UISearchBar in UINavigation?

I have search icon in navigation bar, and UISearchBar in TableViewHeader.
I want to hide UISearchBar but show and hide when:
UISearchbar show: Clicked on the search icon, then eliminate navigation and show it.
UISearchBar hide: Press cancel to hide search and display navigation back.
Please help me!!
Thank.
This is what I have done so far.
- (void) viewDidload{
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
self.searchBar.placeholder = #"Search";
self.searchBar.delegate = self;
[self.searchBar sizeToFit];
self.strongSearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
_btnSearch = [UIButton buttonWithType:UIButtonTypeCustom];
_btnSearch.frame = CGRectMake(0, 0, 22, 22);
[_btnSearch setImage:[UIImage imageNamed:#"search_btn.png"] forState:UIControlStateNormal];
[_btnSearch addTarget:self action:#selector(showSearch:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:_btnSearch];
self.navigationItem.rightBarButtonItems = _btnSearch;
}
This is my action when click on the iCon search
- (IBAction)showSearch:(id)sender{
[self.searchBar becomeFirstResponder];
}
I don't want to place my search in tableView and how still I can display UISearchBar when click on search icon?
Another question: when I call [self.searchBar becomeFirstResponder]; it's doesn't work good. The cancel button doesn't work at all.
Please kindly help me.
to put searchBar into the center of navigationBar:
self.navigationItem.titleView = self.searchBar;
To put searchBar to the left/right side of navigationBar:
UIBarButtonItem *searchBarItem = [[UIBarButtonItem alloc] initWithCustomView:self.searchBar];
self.navigationItem.rightBarButtonItem = searchBarItem;
As of iOS 7, the UISearchDisplayController supports this by default. Set the UISearchDisplayController's
displaysSearchBarInNavigationBar = YES

Resizing UIToolbar when UISearchBar becomes active

I have an UIToolbarwith 5 items:
UIBarButtonItem with image
UIBarButtonItem flex width
UIBarButtonItem with custom view (UISearchBar)
UIBarButtonItem flex width
UIBarButtonItem with image
When I select the UISearchBar, I get it to keep its correct size and become active. However, I would like the image to the left and to the right to disappear and give the UISearchBar the full width of the UIToolbar. How do I do that?
This is what I have:
https://dl.dropbox.com/u/3077127/demo_1.mov
And this is what I want:
https://dl.dropbox.com/u/3077127/demo_2.mov
This is my code:
#pragma mark View lifecycle
- (void)viewDidLoad {
[...]
SearchBar *mySearchBar = [[SearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar setTintColor:[UIHelpers getSlColor]];
CGRect searchBarFrame = mySearchBar.frame;
searchBarFrame.size.width = 218;
[mySearchBar setFrame:searchBarFrame];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:mySearchBar.frame];
[searchBarContainer addSubview:mySearchBar];
[searchBarContainer setTag:99];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
NSArray *items = [[NSArray alloc] initWithObjects:left, flex, search, flex, right, nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolbar setItems:items];
[toolbar setTintColor:[UIHelpers getSlColor]];
self.tableView.tableHeaderView = toolbar;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectNull];
[...]
[super viewDidLoad];
}
#pragma mark -
Here is my answer to a similar question posted here
The key to a correct animation is to specify UIViewAnimationOptionLayoutSubviews as an option for the animation.
Not sure if you have tried this, but maybe you can use the UISearchBarDelegate methods to get notified when the user clicks on the search bar.
Example:
#pragma mark - UISearchBarDelegate Methods
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Remove the right and left buttons from the ToolBar
[self updateToolBarWithImages:NO];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
// Add back the right and left buttons to the ToolBar
[self updateToolBarWithImages:YES];
}
#pragma mark - Private Methods
// Custom method used to Update the ToolBar
- (void)updateToolBarWithImages:(BOOL)iShowImages {
// Logic to update ToolBar based on the BOOL value in showImages
// use "setItems:animated:" method of UIToolBar to set the ToolBar Items
[toolBar setItem:items animated:YES];
}
You shouldn't use a ToolBar for that.
Simply use a NavigationBar, with your 2 right and left ButtonItems, plus add to the NavigationBar's titleView a SearchBar with the appropriate delegates.
So you don't have to resize your NavigationBar. It's just about adding/removing the NavigationBar's right and left ButtonItems animated like so:
if (self.searchBar.isActive) {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setRightBarButtonItem:nil animated:YES];
}
else {
[self.navigationItem setLeftBarButtonItem:self.leftButton animated:YES];
[self.navigationItem setRightBarButtonItem:self.rightButton animated:YES];
}
Hope this helps!

UINavigationController rightbarbuttonitem not showing

after going through the questions related to UINavigationController item i came to post my exact problem ..
firstly i am not add a UINavigationController in MainWindow.xib i created and adding UINavigationController in AppDelegate file
after that in a UIViewController (not in rootViewController) class I have to add a rightbarbutton of Done type I am
adding it in a searchbarDelegate Method showing as bold:-
- (void) searchBarTextDidBeginEditing:(UISearchBar *)theSearchBar {
//This method is called again when the user clicks back from teh detail view.
//So the overlay is displayed on the results, which is something we do not want to happen.
if(exhibDataObj.searching)
return;
//Add the overlay view.
if(ovController == nil)
ovController = [[OverlayViewController alloc] initWithNibName:#"OverlayView" bundle:[NSBundle mainBundle]];
CGFloat yaxis = self.navigationController.navigationBar.frame.size.height;
CGFloat width = self.view.frame.size.width;
CGFloat height = self.view.frame.size.height;
//Parameters x = origion on x-axis, y = origon on y-axis.
CGRect frame = CGRectMake(0, yaxis, width, height);
ovController.view.frame = frame;
ovController.view.backgroundColor = [UIColor grayColor];
ovController.view.alpha = 0.5;
ovController.rvController = self;
[self.theTableView insertSubview:ovController.view aboveSubview:self.parentViewController.view];
exhibDataObj.searching = true;
letUserSelectRow = NO;
self.theTableView.scrollEnabled = NO;
//Add the done button.
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self action:#selector(doneSearching_Clicked:)];
}
but this never works .
Any help will be more appreciable.
I've tested your code real quick and it seems to be working fine for me, however, I've had the same problem once back then it turned out I was setting the rightBarButtonItem to nil in my viewDidLoad method after setting it to a button. you should make sure your not making the same mistake somehow.
However if that does not work for you you can always try setting a custom button to it:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];
button.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"done.png"]];
[button addTarget:self action:#selector(doneButtonClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;

UINavBar seems to have an invisible layer over other buttons

I created an application with a side menu like facebooks. I have a root view controller which adds a view over itself, then when the user presses the 'Menu' button it will slide the view over so you can see the menu.
My issue is that when the view first loads it looks like the UINavBar is about 20 pixels or so lower than it should be. (The below image looks a bit off centered because it is a modal horizontal flip)
It then automatically adjusts itself and moves up just under that status bar when the modal transition is complete.
Here is a screenshot of what happens when I press the "Menu" button. It slides the top view over to the right
As you can see in the images above I have the left arrow and right arrow frame background set to red so that I can see where the clickable / tappable region is. I have another image which I drew a blue line on that shows if I click the blue line or above then it will not trigger the right arrow. If I click in that same general area on the left side the menu button will trigger. I am able to reproduce this in both the simulator and on a mobile device (so I am not fat fingering the button).
It seems to me that since the navigation bar is loading lower than it is supposed to the iPhone thinks it is still in that position so when I click inside the UIButton frame it is still triggering the Menu and not triggering the arrow selectors. If I tap DIRECTLY in the middle of the arrows, it triggers the arrows selectors but just a tiny bit outside the image, it will not trigger the arrow selector.
Here is my RootViewController viewDidLoad
AppointmentViewController *appointmentViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentViewController"];
NSLog(#"B: %#",NSStringFromCGRect(appointmentViewController.view.frame));
self.navController = [[UINavigationController alloc] initWithRootViewController:appointmentViewController];
NSLog(#"B2: %#",NSStringFromCGRect(appointmentViewController.view.frame));
NSLog(#"Nav Bounds: %#",NSStringFromCGRect(self.navController.view.frame));
NSLog(#"Bounds: %#",NSStringFromCGRect(self.contentView.bounds));
self.navController.view.frame = self.contentView.bounds;
NSLog(#"Nav Bounds2: %#",NSStringFromCGRect(self.navController.view.frame));
[self addShadowToMenu];
[self.contentView addSubview:self.navController.view];
RootViewController Logs
2012-04-30 12:24:44.533 My-App[19929:fb03] B: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] B2: {{0, 20}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds: {{0, 0}, {320, 480}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Bounds: {{0, 0}, {320, 460}}
2012-04-30 12:24:44.535 My-App[19929:fb03] Nav Bounds2: {{0, 0}, {320, 460}}
AppointmentViewController
self.title = [dataObj.preferences objectForKey:#"appts_name_plural"];
self.navigationItem.title = self.title;
dayView = [[APCalendarDayView alloc] initWithCalendarView:(APCalendarView *)self];
APListView = [self.storyboard instantiateViewControllerWithIdentifier:#"AppointmentListView"];
APListView.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
SideMenuView *myDelegate = [[SideMenuView alloc] init];
[self setSideMenuDelegate:myDelegate];
//set the delegate's currentViewController property so that we can add a subview to this View.
[sideMenuDelegate setCurrentViewController:self];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *today = [[UIBarButtonItem alloc] initWithTitle:#"Today" style:UIBarButtonItemStyleBordered target:nil action:#selector(todayPressed)];
NSArray *appointmentNavigationItems = [[NSArray alloc] initWithObjects:#"Day",#"List",#"Month", nil];
navcontrol = [[UISegmentedControl alloc] initWithItems:appointmentNavigationItems];
[navcontrol setSegmentedControlStyle:UISegmentedControlStyleBar];
[navcontrol addTarget:self action:#selector(appointmentNavigationClicked:) forControlEvents:UIControlEventValueChanged];
[navcontrol setSelectedSegmentIndex:0];
UIBarButtonItem *segmentItems = [[UIBarButtonItem alloc] initWithCustomView:navcontrol];
NSArray *items = [NSArray arrayWithObjects:today,flexSpace,segmentItems, flexSpace, nil];
[appointmentToolbar setItems:items animated:NO];
[appointmentToolbar setAutoresizesSubviews:YES];
[[self.view superview] addSubview:myDelegate.view];
[self setViewSizeOffset:(appointmentToolbar.bounds.size.height)];
CalendarDayView
- (void)addSubviewsToHeaderView:(UIView *)headerView
{
const CGFloat kChangeMonthButtonWidth = 46.0f;
const CGFloat kChangeMonthButtonHeight = 44.0f;
const CGFloat kMonthLabelWidth = 215.0f;
const CGFloat kHeaderVerticalAdjust = 7.f;
// Header background gradient
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Kal.bundle/kal_grid_background.png"]];
CGRect imageFrame = headerView.frame;
imageFrame.origin = CGPointZero;
backgroundView.frame = imageFrame;
[headerView addSubview:backgroundView];
[backgroundView setAutoresizesSubviews:YES];
[backgroundView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
// Create the previous month button on the left side of the view
CGRect previousMonthButtonFrame = CGRectMake(self.left, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *previousMonthButton = [[UIButton alloc] initWithFrame:previousMonthButtonFrame];
[previousMonthButton setBackgroundColor:[UIColor redColor]];
[previousMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_left_arrow.png"] forState:UIControlStateNormal];
previousMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
previousMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[previousMonthButton addTarget:self action:#selector(showPreviousDay) forControlEvents:UIControlEventTouchUpInside];
//[previousMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin];
[headerView addSubview:previousMonthButton];
// Draw the selected month name centered and at the top of the view
CGRect monthLabelFrame = CGRectMake((self.frame.size.width/2.0f) - (kMonthLabelWidth/2.0f), kHeaderVerticalAdjust, kMonthLabelWidth, kMonthLabelHeight);
headerTitleLabel = [[UILabel alloc] initWithFrame:monthLabelFrame];
headerTitleLabel.backgroundColor = [UIColor clearColor];
headerTitleLabel.font = [UIFont boldSystemFontOfSize:22.f];
headerTitleLabel.textAlignment = UITextAlignmentCenter;
headerTitleLabel.textColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Kal.bundle/kal_header_text_fill.png"]];
headerTitleLabel.shadowColor = [UIColor whiteColor];
headerTitleLabel.shadowOffset = CGSizeMake(0.f, 1.f);
//[self setHeaderTitleText:[logic selectedMonthNameAndYear]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"E, MMM dd, yyyy"];
headerTitleLabel.text = [dateFormatter stringFromDate:dataObj.activeDate];
[headerView addSubview:headerTitleLabel];
// Create the next month button on the right side of the view
CGRect nextMonthButtonFrame = CGRectMake(self.frame.size.width - kChangeMonthButtonWidth, 0, kChangeMonthButtonWidth, kChangeMonthButtonHeight);
UIButton *nextMonthButton = [[UIButton alloc] initWithFrame:nextMonthButtonFrame];
nextMonthButton.frame = nextMonthButtonFrame;
[nextMonthButton setImage:[UIImage imageNamed:#"Kal.bundle/kal_right_arrow.png"] forState:UIControlStateNormal];
[nextMonthButton setBackgroundColor:[UIColor redColor]];
nextMonthButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
nextMonthButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[nextMonthButton addTarget:self action:#selector(showFollowingDay) forControlEvents:UIControlEventTouchUpInside];
//[nextMonthButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[headerView addSubview:nextMonthButton];
}
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.frame.size.width, kHeaderHeight)];
headerView.backgroundColor = [UIColor grayColor];
[self addSubviewsToHeaderView:headerView];
[headerView setAutoresizesSubviews:YES];
[headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[self addSubview:headerView];
[self bringSubviewToFront:headerView];
If I move the headerView down farther it will allow me to select anywhere in the red area and have the arrow selector be triggered. However, I want it to match Apples calendar and be just under the navigation bar.
Anyone know why the navigation bar is blocking the top portion of the headerView from being tapped? To be clear the portion of the headerView that is being blocked is from the bottom of the blue line and up. The UIButton with the red background has an action tied to the frame so it should be triggering the event, however it appears the navbar is blocking half of the UIButton from receiving taps.
This behavior is due to the fact that UIWindow is not propagating touches that coincide with the nav bar frame. The only way to override this behavior is to subclass UIWindow and override the hitTest: method. This will allow you to detect touches in this area, and forward them to the appropriate responders.
Here is an example project showing a custom UIWindow subclass that will respond to touches any where on the screen.