IOS 7 hide tabbar issue - uiview

Hide tabbar in IOS7 shows informal behaviour
When I use
self.tabBarController.tabBar.hidden = YES;
Above code hides the tabBar, but my view from bottom does not remain interactive
But when I uses this just before pushing viewController in navigation
someViewController.hidesBottomBarWhenPushed = YES
[self.navigationController pushViewController:someViewController animated:YES];
It hides tabbar as well as view from bottom is also interactive.
but problem in this case is, when we pop viewController it shows black bar just above tabbar for few seconds.

I hope you got the solution. Just to make sure, did you try
self.edgesForExtendedLayout = UIRectEdgeBottom;

Try this, if you want to hide/show the UITabBarController of view:
For hide the tabbar:
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
}
for show Tabbar:
- (void)showTabBar:(UITabBarController *) tabbarcontroller
{
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 431, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 431)];
}
}
}
may be it will help.

I think that both of you problems are due to not well defined/missing autoresizingMask's or missing auto-layout constraints (whichever you're using).
What UITabBarController does when hiding the tab bar is to stretch its view enough to get the its tabBar outside the screen. Your contained view controllers' views in turn should properly stretch to use the new space or you'll get empty spaces and/or non interactive zones.
Edit:
Just realized that hiding the tab bar is not in the default SDK but in a category I made long time ago.
Anyway stretching UITabBarController's view frame seems to me the most elegant way to "hide" the tab bar (actually move it away from the screen) as you don't have to deal with subviews or hunt down the tab bar frame directly.

So I've rewritten some of the answers written in Objective-C into Swift 3.0 thinking it would work. Here's the code:
func hideTabBar() {
let tabBarControllerView = self.tabBarController?.view
if let tabBarControllerSubviews = tabBarControllerView?.subviews {
for view in tabBarControllerSubviews {
if view is UITabBar {
view.frame = CGRect(
x: view.frame.origin.x,
y: (UIScreen.main.bounds.size.height == 568 ? 568 : 480) + 20,
width: self.view.frame.size.width,
height: self.view.frame.size.height
)
} else {
view.frame = CGRect(
x: view.frame.origin.x,
y: view.frame.origin.y,
width: self.view.frame.size.width,
height: UIScreen.main.bounds.size.height == 568 ? 568 : 480
)
}
}
}
}
func showTabBar() {
let tabBarControllerView = self.tabBarController?.view
if let tabBarControllerSubviews = tabBarControllerView?.subviews {
for view in tabBarControllerSubviews {
if view is UITabBar {
view.frame = CGRect(
x: view.frame.origin.x,
y: (UIScreen.main.bounds.size.height == 568 ? 519 : 431),
width: self.view.frame.size.width,
height: self.view.frame.size.height
)
} else {
view.frame = CGRect(
x: view.frame.origin.x,
y: view.frame.origin.y,
width: self.view.frame.size.width,
height: UIScreen.main.bounds.size.height == 568 ? 519 : 431
)
}
}
}
}

Use following code to resolve your problem
Hide :
-(void)hideTabBar:(UITabBarController *)tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height;
if( UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width;
}
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
}
Show :
-(void)showTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
float fHeight = screenRect.size.height - 49.0;
if( UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) )
{
fHeight = screenRect.size.width - 49.0;
}
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
}
}
}
Use this methods in viewWillAppear and on device rotation methods as per your requirement

Related

How to shift all the views down except for the tab bar in tabViewController?

In order to fix the overlapping of status bar and navigation bar in iOS 7+, i'm using this code inside didFinishLaunchingWithOptions in AppDelegate.m :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//some codes
//.
//.
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIView *FakeNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
FakeNavBar.backgroundColor = UIColorFromRGB(0x55BCAFF);
float navBarHeight = 20.0;
for (UIView *subView in self.window.subviews) {
if ([subView isKindOfClass:[UIScrollView class]]) {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height - navBarHeight);
} else {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height);
}
}
[self.window addSubview:FakeNavBar];
}
}
It pushes all my controllers and views 20 pixels down and and the overlapping problem gets fixed but when i reach my tab view controller scene, then the tab bar on the bottom goes out of view by 20 pixels.
So how can i keep the tab bar in its place while shifting everything else down?
It would also work if i could just shift up only the tab bar by 20 pixels.
I was able to shift only the tab bar 20 pixels up but this may put some views behind the tab bar which is unwanted.
here is the code written inside viewDidAppear of my UITabBarController class :
-(void)viewDidAppear:(BOOL)animated{
CGRect newFrame = self.tabBar.frame;
newFrame.origin.y -= 20;
self.tabBar.frame = newFrame;
}
Please use the code below -
if([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
{
UIView *FakeNavBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
FakeNavBar.backgroundColor = UIColorFromRGB(0x55BCAFF);
float navBarHeight = 20.0;
for (UIView *subView in self.window.subviews) {
for (int index=0; index<[tabBarController.viewControllers count]; index++)
{
if ([subView isKindOfClass:[[(UIViewController*)[tabBarController.viewControllers objectAtIndex:index] view] class]])
{
continue;
}
}
if ([subView isKindOfClass:[UIScrollView class]]) {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height - navBarHeight);
} else {
subView.frame = CGRectMake(subView.frame.origin.x, subView.frame.origin.y + navBarHeight, subView.frame.size.width, subView.frame.size.height);
}
}
[self.window addSubview:FakeNavBar];
}
I assume that you're not using a navigation controller and you've manually added the navigation bar in your view. Is that right?
You should be able to achieve what you're after by adding a view with a size of the status bar at the top of the views of your view controllers in the storyboard.
Are you using auto-layout constraints? You could use them to make these views stick to the top of your views, have a fixed height of 20 pixels and a width equal to the width of the view controller's view.

Unable to tap on UITabBar Frame area

In one of the view controller UITabBar has been set hidden and in the same place one CustomView with UITextField is added, But the entire CustomView is not taking any action.
If custom view is placed above the UITabBar it works fine. But I want to hide the Tab Bar in and place CustomView in the same frame.
I am using the below code to hide the Tab Bar
[self.tabBarController.tabBar setHidden:YES];
TaB Bar is added like this
[self.window addSubview:self.tabBarController.view];
Hiding the UITabBar does not still allow view below it to work. You will have to manually move the UITabBar when you want to hide and manually bring it back when you want to unhide if you want this functionality. The following code would work:
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 480, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 480)];
}
}
[UIView commitAnimations];
}
If you want to move a particular tab bar button instead of the complete controller, you may have to do a little tweaking in the code.
Another option is to use hideBottomBarWhenPushed option like this
self.hidesBottomBarWhenPushed = true;
There is a property in UIViewController exactly for this reason - hidesBottomBarWhenPushed;
If you use storyboard then you can set this in view controller's attributes inspector in the storyboard - this is the best solution IMHO.
Like this:
If you don't use storyboard then you can set this property to YES in in viewDidLoad or in 'init' of the view controller that should hide the tab bar.
Something like this:
- (void)viewDidLoad {
[super viewDidLoad];
self.hidesBottomBarWhenPushed = YES;
}
I use the following code resize views when hiding/unhiding tabBar :
- (void)hideTabBar{
for (UIView *view in self.tabBarController.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
view.frame = CGRectMake(view.frame.origin.x,
[UIScreen mainScreen].bounds.size.height,
view.frame.size.width,
view.frame.size.height);
} else {
view.frame = CGRectMake(view.frame.origin.x,
view.frame.origin.y,
view.frame.size.width,
[UIScreen mainScreen].bounds.size.height);
}
}
}
- (void) showTabBar {
for (UIView *view in self.tabBarController.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
view.frame = CGRectMake(view.frame.origin.x,
[UIScreen mainScreen].bounds.size.height - 49,
view.frame.size.width,
view.frame.size.height);
} else {
view.frame = CGRectMake(view.frame.origin.x,
view.frame.origin.y,
view.frame.size.width,
[UIScreen mainScreen].bounds.size.height - 49);
}
}
}
Where 49 is the height of the tabBar.

Hide and Add UITabbarController

I'm new in ios Development. I have some question.
Now, in my project i have UITabBarController1 with 2 viewControllers(ViewController1, ViewController2).
ViewController1 is start Page.
When I run Every Page(ViewController1, ViewController2, ViewController3, ...) have UITabBarController.
But in some page(Example : ViewController4) i want to hide and add New UITabBarController2
in ViewController4
if I use command "hideButtomBar" in ViewController4 : UITabBarController is not appear.
if I don't use command "hideButtomBar" in ViewController4 : UITabBarController1 and UITabBarController2 is appear(both)
how to fixed it
thank for help and sorry for my mistake about english. ^^
Try using this to hide tabbar
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
float fHeight = screenRect.size.height;
if( UIDeviceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation) ){
fHeight = screenRect.size.width;
}
for(UIView *view in tabbarcontroller.view.subviews){
if([view isKindOfClass:[UITabBar class]]){
[view setFrame:CGRectMake(view.frame.origin.x, fHeight, view.frame.size.width, view.frame.size.height)];
}else{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, fHeight)];
view.backgroundColor = [UIColor blackColor];
}
}
[UIView commitAnimations];
}
and
[self hideTabBar: UITabBarController1];
app.tabBarController.tabBar.hidden=TRUE;
app.tabBarController.tabBar.hidden=TRUE;
//Set some tab
[app.tabBarController setSelectedIndex:0];

Hiding tabBar showing black bar in Ios7

I am using this code to hide the TabBar:
self.tabBarController.tabBar.hidden=YES;
I am hiding tabBarController in my project.but it showing black bar in bottom of the view in Ios7.When i go back to the same view it is looking good.any help will be appreciated.
NOTE: It is solution for iOS6 and 7 only.
In iOS 7 to extend clickable area and hide black bar on place of hidden UITabBar you should enable 'Extend Edges - Under Opaque Bars' option for you UIViewController.
Or you can set this property programmatically:
[self setExtendedLayoutIncludesOpaqueBars:YES]
Here is example of code that hide or move TabBar for iOS 6/7:
UITabBarController *bar = [self tabBarController];
if ([self respondsToSelector:#selector(setExtendedLayoutIncludesOpaqueBars:)]) {
//iOS 7 - hide by property
NSLog(#"iOS 7");
[self setExtendedLayoutIncludesOpaqueBars:YES];
bar.tabBar.hidden = YES;
} else {
//iOS 6 - move TabBar off screen
NSLog(#"iOS 6");
CGRect screenRect = [[UIScreen mainScreen] bounds];
float height = screenRect.size.height;
[self moveTabBarToPosition:height];
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
self.tabBarController.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBarController.tabBar.frame.size.width, self.tabBarController.tabBar.frame.size.height);
for(UIView *view in self.tabBarController.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
} else {
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
view.backgroundColor = [UIColor blackColor];
}
}
}
Function to moving the Tab Bar offscreen got from this post.
Next code works for me
- (void)showTabBar {
[self.tabBar setTranslucent:NO];
[self.tabBar setHidden:NO];
}
- (void)hideTabBar {
[self.tabBar setTranslucent:YES];
[self.tabBar setHidden:YES];
}
Try this:
- (BOOL)hidesBottomBarWhenPushed {
return YES;
}
I had some trouble while using a UINavigationController:
Here's my solution that works for iOS 7 AND UINavigationControllers:
HeaderFile
#interface UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated;
#end
Implementation
#import "UITabBarController+HideTabBar.h"
#implementation UITabBarController (HideTabBar)
- (void)setHideTabBar:(BOOL)hide animated:(BOOL)animated {
UIViewController *selectedViewController = self.selectedViewController;
/**
* If the selectedViewController is a UINavigationController, get the visibleViewController.
* - setEdgesForExtendedLayout won't work with the UINavigationBarController itself.
* - setExtendedLayoutIncludesOpaqueBars won't work with the UINavigationBarController itself.
*/
if ([selectedViewController isKindOfClass:[UINavigationController class]])
selectedViewController = ((UINavigationController *)selectedViewController).visibleViewController;
__weak __typeof(self) weakSelf = self;
void (^animations)(void) = ^{
selectedViewController.edgesForExtendedLayout = UIRectEdgeAll;
[selectedViewController setExtendedLayoutIncludesOpaqueBars:hide];
weakSelf.tabBar.hidden = hide;
/**
* Just in case we have a navigationController, call layoutSubviews in order to resize the selectedViewController
*/
[selectedViewController.navigationController.view layoutSubviews];
};
[UIView animateWithDuration:animated ? UINavigationControllerHideShowBarDuration : 0 animations:animations];
}
#end
Thanks to Vadim Trulyaev for pointing out the Extend Edges - Under Opaque Bars flag!
One line Swift 3 answer.
Put the following in your UIViewController subclass:
override var hidesBottomBarWhenPushed: Bool { get { return true } set { self.hidesBottomBarWhenPushed = newValue }}
Set true the property hidesBottomBarWhenPushed in the controller to hide.
For hide, all your controllers put into prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
segue.destination.hidesBottomBarWhenPushed = true
}
To showTabbar:
- (void)showTabBar:(UITabBarController *) tabbarcontroller
{
//[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 521, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 521)];
}
}
// [UIView commitAnimations];
}
To hide Tabbar:
- (void)hideTabBar:(UITabBarController *) tabbarcontroller
{
//[UIView beginAnimations:nil context:NULL];
//[UIView setAnimationDuration:0.5];
for(UIView *view in tabbarcontroller.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, 568, view.frame.size.width, view.frame.size.height)];
}
else
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, 568)];
}
}
//[UIView commitAnimations];
}
I spent a long time battling this, trying to place a responsive button at the bottom of table view. I am not using auto-layout. I found two main differences between iOS 6 and 7:
On iOS7, when the tab bar is animated out, the view of the root view controller does not extend into the area where the tab bar was; it needs to be resized.
On iOS7, only the view of type UITabBar needs to be animated off and on the screen.
A further issue with point 1 is that if, in iOS7, you extend a child view of your visible view controllers main view over the space left behind by the tab view, it won't be interactable unless the main view is extended as well. With that in mind, I used the following code:
Hide tab bar (reverse the math so show it):
[UIView animateWithDuration:kHideTabBarAnimationDuration animations:^{
for(UIView *view in self.tabBarController.view.subviews)
{
if([view isKindOfClass:[UITabBar class]])
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y + view.frame.size.height, view.frame.size.width, view.frame.size.height)];
}
else
{
if (![MYDeviceUtility systemVersionGreaterThanOrEqualTo:#"7.0"])
{
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, view.frame.size.height + self.tabBarController.tabBar.frame.size.height)];
}
}
}
} completion:nil];
Adjust the main view when hiding tab bar:
// Expand view into the tab bar space
if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:#"7.0"])
{
CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height + tabBarHeight);
}
Adjust the main view when revealing tab bar:
[UIView animateWithDuration:kHideTabBarAnimationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
// Create space for the tab bar
if ([MYDeviceUtility systemVersionGreaterThanOrEqualTo:#"7.0"])
{
CGRect frame = self.view.frame;
self.view.frame = CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - tabBarHeight);
}
} completion:nil];
Note that I don't animate the main view expansion when hiding the tab bar, this looks natural since the expansion happens behind the tab bar.
Also note
In iOS 7, if you rotate from portrait to landscape while the tab bar is hidden, the black box reappears. I solved this by animating the tab bar back onto the screen before the rotation animation (which was good enough for what I'm working on).
Based on solution of #Vadim Trulyaev, i created a simple usage:
UITabBarController+HideTabBar.h
#interface UITabBarController (Additions)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass;
#end
UITabBarController+HideTabBar.m
#import "UITabBarController+HideTabBar.h"
#implementation UITabBarController (HideTabBar)
- (void)setTabBarHidden:(BOOL)hidden myClass:(UIViewController *)myClass{
if ([myClass respondsToSelector:#selector(setExtendedLayoutIncludesOpaqueBars:)]) {
//iOS 7 - hide by property
NSLog(#"iOS 7");
[myClass setExtendedLayoutIncludesOpaqueBars:hidden];
self.tabBar.hidden = hidden;
} else {
//iOS 6 - move TabBar off screen
NSLog(#"iOS 6");
CGRect screenRect = [[UIScreen mainScreen] bounds];
float height = screenRect.size.height;
if(hidden){
[self moveTabBarToPosition:height];
}else{
[self moveTabBarToPosition:height - self.tabBar.frame.size.height];
}
}
}
//Moving the tab bar and its subviews offscreen so that top is at position y
-(void)moveTabBarToPosition:(int)y {
self.tabBar.frame = CGRectMake(self.tabBarController.tabBar.frame.origin.x, y, self.tabBar.frame.size.width, self.tabBar.frame.size.height);
for(UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
[view setFrame:CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height)];
} else {
NSLog(#"%f",view.frame.size.height);
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, y)];
NSLog(#"%f",view.frame.size.height);
view.backgroundColor = [UIColor blackColor];
}
}
}
#end
How to use:
[[self tabBarController] setTabBarHidden:NO myClass:self];
BUT, in iOS6 i have some issue, when i go first time to ViewController1 where the tabbar is hidden everthing works fine, but if i go to a ViewController2 that show the tab bar and back to ViewController1 that the tab bar must be hidden, the black space show up. Anyone can help me?!
Thanks!
In addition to the other excellent suggestions the following suggestion might help someone out. Try setting your tabbar to hidden in awakeFromNib instead of later in the lifecycle. I found that the hidden tabbar was flashing black on segue and this fixed it for me.
- (void)awakeFromNib
{
[super awakeFromNib];
self.tabBarController.tabBar.hidden = YES;
}

Change frame of UISearchBar programatically when orientation changes

I have a search button and when user clicks it I am displaying UISearchBar programmatically. Width of Searchbar is (200) not full screen. I am searching like a MSWord and displaying the content in searchbar' tableview. But when user is in middle of search and changed orientation how should I control the search bar width, i.e., self.searchDisplayController.searchBar.frame and self.searchDisplayController.searchResultsTableView.frame.
Below is the piece of code that used to create the searchbar:
-(IBAction)searchBar:(id)sender
{
if(!searching)
{
searching = YES;
sBar.frame = CGRectMake(500, 50,250, 44);
[self.view addSubview:sBar];
[searchController setActive:YES animated:YES];
[sBar becomeFirstResponder];
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
}
else
{
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
}
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller{
/*
Bob: Because the searchResultsTableView will be released and allocated automatically, so each time we start to begin search, we set its delegate here.
*/
[self.searchDisplayController.searchResultsTableView setDelegate:self];
[self.searchDisplayController.searchResultsTableView setBackgroundColor:[UIColor colorWithRed:1 green:1 blue:204/255.0 alpha:1.0]];
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller{
/*
Hide the search bar
*/
float animateTime = 0.3;
CGRect viewFrame = sBar.frame;
viewFrame.origin.y -= (viewFrame.size.height);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animateTime];
sBar.frame = viewFrame;
[UIView commitAnimations];
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.3];
}
-(void)animationDone
{
[sBar removeFromSuperview];
searching = NO;
}
-(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView
{
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
tableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
tableView.frame = CGRectMake(500, 100, 250, 400);
}
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
if((currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
(currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchDisplayController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}
I am able to create and functionality which is working fine. But only issue is when user change the orinentation its not properly setting searchbar' width.
My application needs to be delivered this friday. Please answere me asap.
Answering my question to close this thread as well to help others.
(1) In willRotateToInterfaceOrientation its able to set UISearchBar origin. Width is coming as 768 (full screen width of iPad in Portrait) but origin seems to be set here.
(2) After orientation change finished we can set the search bar width. With small animation it will be smooth resize.
(3) I am calling resetFrame at both pre & post orientation APIs to avoid some sudden JURK kind for searchBar.
Below is the solution i used for my app..
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
currentOrientation = toInterfaceOrientation;
[self resetSearchResourcesFrames]; // Set the origin of UISearchBar
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
[UIView beginAnimations:#"" context:nil]; //Animation for smooth resizing of search tableview only
[UIView setAnimationDuration:0.2];
[self resetSearchResourcesFrames]; // Width rotation only possible after rotation
[UIView commitAnimations];
}
-(void)resetSearchResourcesFrames
{
if(([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeLeft) ||
([buttonGridViewController sharedInstance].currentOrientation == UIInterfaceOrientationLandscapeRight))
{
//Search
btnSearch.frame = CGRectMake(920, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(755, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(755, 100, 250, 400);
}
else
{
//Search
btnSearch.frame = CGRectMake(666, 5, 40, 40);
self.searchDisplayController.searchBar.frame = CGRectMake(500, 50, 250, 44);
self.searchController.searchResultsTableView.frame = CGRectMake(500, 100, 250, 400);
}
}

Resources