UiSearchBar OffScreen when Initialised - iPad - objective-c

I have the following code which works under iOS5 for putting a UISearchBar at the top of a UITableView on the iPad :
- (void)viewDidLoad
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGRect searchViewFrame = CGRectMake(33, 33, 264, 45);
UIView *containerSearch = [[UIView alloc] initWithFrame: searchViewFrame];
searchBar = [[UISearchBar alloc] init];
searchBar.barStyle = UIBarStyleDefault;
[containerSearch addSubview: searchBar];
self.tableView.tableHeaderView = containerSearch;
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:self];
searchBar.delegate = self;
searchController.delegate = self;
searchController.searchResultsDelegate=self;
searchController.searchResultsDataSource=self;
[searchBar release];
}
Under iOS6 however this code behaves strangely. When the iPad is started in landscape mode the UISearchBar is offscreen. It does not appear in its correct position until the ipad is rotated to portrait and back to landscape.
Is this just an iOS6 bug or any suggestions as to how it can be fixed ?
Thanks !

Have you tried to init the searchbar with a frame? searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0,0,265,45)];

Related

Unable to set rightview or leftview in textfield of searchbar in ios 7

Purpose:
I want to set right view as label in searchbar's textfield
Following Code working fine in ios 6 for doing the same thing:
UISearchBar *search = [[UISearchBar alloc] init];
[search setTintColor:[UIColor grayColor]];
search.frame = CGRectMake(0, 150, 320,50);
search.delegate = self;
UITextField *searchField=[search.subviews objectAtIndex:1];//Changed this line in ios 7
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
[self addSubView:search];
When i have Run the same code in ios 7(with xcode 7.0.2 fully updated), It was giving me error, after googling i got that hiearchy of searchbar to textfield has been changed, so i have modified my code as:
replaced commented line in above code with:
UITextField *searchField=[((UIView *)[search.subviews objectAtIndex:0]).subviews lastObject];
after doing this, it is not giving me any error(also checked the logs by printing the description of textfield and everything is working good), when i run it, it gives textfield with red background but the rightView's label is not shown to me. Now, can anyone help me how to show this rightview.
If you want to do this programatically then you have to declare UISearchBar object in your .h file.
UISearchBar *searchBar;
And in your .m file you can code like:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
searchBar = [[UISearchBar alloc] init];
[searchBar setTintColor:[UIColor grayColor]];
searchBar.frame = CGRectMake(0, 150, 320,50);
searchBar.delegate = self;
[self.view addSubview:searchBar];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField;
if ([[[UIDevice currentDevice] systemVersion] floatValue]<7.0)
searchField=[searchBar.subviews objectAtIndex:1];
else
searchField=[((UIView *)[searchBar.subviews objectAtIndex:0]).subviews lastObject];
searchField.backgroundColor=[UIColor redColor];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I think this will work for both iOS7 and prior versions of iOS7.
First of all, your approach is not good and not maintainable across iOS versions.
I think that initialization of UISearchBarTextField's right view is taking place after you set your custom view to it's rightView property. You will see correct result if you set searchField.rightView in viewDidAppear method:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UITextField *searchField=[((UIView *)[_searchBar.subviews objectAtIndex:0]).subviews lastObject];
UILabel *label=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text=#"Hello";
label.textColor=[UIColor greenColor];
searchField.rightView = label;
searchField.rightViewMode = UITextFieldViewModeAlways;
}
I've tested it on iOS 7 simulator and it works.
You have to get the text field first.
No need to index subviews to get text field or bar button.
You can directly get the text field and bar button
In swift
let textField = searchBar.valueForKey("searchField") as? UITextField
let cancelBarButton = searchBar.valueForKey("cancelBarButtonItem") as? UIBarButtonItem
In Objective-C
UITextField *textField = [searchBar valueForKey:#"searchField"]:
UIBarButtonItem *cancelBarButton = [searchBar valueForKey:#"cancelBarButtonItem"]:
Then you can set the left view and right view with
textField.leftView = yourView
textField.rightView = yourView

UISearchDisplayController inside UIPopOverController - searchResultsTableView not displaying properly

So I have a tableview inside a UIPopOverController and I attached a UISearchBar to the header of the tableview and I have the UISearchDisplayController displaying the correct results. However, I can't get the results to display within the UIPopoverController window. It's displaying behind the popover view into the DetailView covering half the screen (SplitViewController for iPad). How do I bring the results/tableview into the popover view? I tried adding the searchTableView to the popover view, but it just covers up the original tableview.
Here is the code of setting this up:
//Initialize Popover Controller
UIView *ccPopoverView = [[UIView alloc] init];
UIViewController* popoverTableContent = [[UIViewController alloc] init];
self.ccTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, 320, 400) style:UITableViewStylePlain];
self.ccTableView.dataSource = self;
self.ccTableView.delegate = self;
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320.0, 44.0)];
self.searchDisplayController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
searchBar.frame = CGRectMake(0, 0, 0, 38);
self.ccTableView.tableHeaderView = searchBar;
ccPopoverView.backgroundColor = [UIColor blackColor];
//Here is where I tried adding the searchResultsTableView as a subview.
//[ccPopoverView addSubview:self.searchDisplayController.searchResultsTableView];
[ccPopoverView addSubview:self.ccTableView];
popoverTableContent.view = ccPopoverView;
self.popoverTableController.delegate = self;
self.popoverTableController = [[UIPopoverController alloc] initWithContentViewController:popoverTableContent];
[self.popoverTableController setPopoverContentSize:CGSizeMake(320, 440) animated:NO];
Thanks!
I fixed the issue instead of adding self when I initialized the search bar, I needed to put in the popovercontroller as the initialization.

Touch on ImageView to another ViewController

I have 2 ViewControllers and I create one UIImageView to show like Splash Screen on IPhone. I write it in TestAppDelegate.m :
====================
splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashScreen.image = [UIImage imageNamed:#"Default.png"];
[self.window addSubview:splashScreen];
sleep(6);
[splashScreen removeFromSuperview];
====================
My question is,
if I touch on this imageview I will go to 2nd ViewController.
else after time sleep, automatically to 1st ViewController.
So, it's possible to do that ?
Do this:
Add UIGestureRecognizerDelegate in appDelegate.h file.
#interface AppDelegate : UIResponder <UIApplicationDelegate,UIGestureRecognizerDelegate>
Now
splashScreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
splashScreen.userInteractionEnabled = YES;
splashScreen.image = [UIImage imageNamed:#"Default.png"];
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tapRecognizer.numberOfTapsRequired = 1;
tapRecognizer.numberOfTouchesRequired = 1;
tapRecognizer.delegate = self;
[splashScreen addGestureRecognizer:tapRecognizer];
[tapRecognizer release];
[self.window addSubview:splashScreen];
sleep(6);
[splashScreen removeFromSuperview];
//add ViewController1 here
ViewController1 *objViewController1 = [[ViewController1 alloc]initWithNibName:#"ViewController1" bundle:nil];
[self.window addSubview:objViewController1.view];
Now handler will be called when tapped on splash screen
- (void)handleTap:(UITapGestureRecognizer*)recognizer
{
// Do Your thing.
if (recognizer.state == UIGestureRecognizerStateEnded)
{
[splashScreen removeFromSuperview]; //edited here
ViewController2 *objViewController2 = [[ViewController2 alloc]initWithNibName:#"ViewController2" bundle:nil];
[self.window addSubview:objViewController2.view];
}
}
Yes. It's possible.In AppDelegate keep NSTimer.
In the selector of timer write code to push to the 1st view controller.
And put a Touch Recognizer on the imageview and on the touch event write code to push to the 2nd View Controller.

Black Bar in UIImagePicker on iPad - Only in Landscape Right Orientation

I am showing a UIImagePicker (itself in a UIView) via a UIPopOver. This works fine, however when the iPad is rotated onto its right side I get a strange black bar along the left hand side of the popover as per the image below. The cancel button is also partially off the right hand of the screen. This doesn't happen in any other orientation which is odd.
The code is also listed below. Can anyone suggest why I am getting this black bar ?
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = CGSizeMake(width, height);
[imagePickerController.view setFrame:containerController.view.frame];
[containerController.view addSubview:imagePickerController.view];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Class cls = NSClassFromString(#"UIPopoverController");
if (cls != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:containerController];
[popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];
[containerController release];
}
For some strange reason, the view's frame does change in landscape right.
To come over this, set the frame after you present the popover (view code below).
That should do the trick.
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
UIViewController *containerController = [[UIViewController alloc] init];
containerController.contentSizeForViewInPopover = CGSizeMake(width, height);
[containerController.view addSubview:imagePickerController.view];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
Class cls = NSClassFromString(#"UIPopoverController");
if (cls != nil) {
popoverController = [[UIPopoverController alloc] initWithContentViewController:containerController];
[popoverController presentPopoverFromRect:selectedRect inView:self.view permittedArrowDirections:4 animated:YES];
[imagePickerController.view setFrame:containerController.view.frame];
[containerController release];
}
Also, in your controller, add this to reset the frames when the rotation occurs:
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[imagePickerController.view setFrame:imagePickerController.view.superview.frame];
}
The above approach works with iOS 5 and above but on iOS 4 and prior it's not possible to add the UIImagePickerController as a subview and then show it. It always has to be presented as a ModalViewController.
Try this. Reposting one solution I found using [currentDevice endGeneratingDeviceOrientationNotifications]
UIImagePickerController *picker = [[[UIImagePickerController alloc] init] autorelease];
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.delegate = self;
// Display the camera.
[self presentModalViewController:picker animated:YES];
// Given by default your orientation is in landscape right already
while ([currentDevice isGeneratingDeviceOrientationNotifications])
[currentDevice endGeneratingDeviceOrientationNotifications];
Source: Disable rotation in UIImagePicker

How do I make UISearchBar in a UINavigationBar resize only the left margin using a block animation?

I am trying to duplicate the UISearchBar animation seen in mobile Safari. Instead of only moving the left margin, the UISearchBar Expands off the screen and then "jumps" into the proper location. The downsizing is similarly uneven. How can I make this animation even like the UISearchBar in mobile safari?
searchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0,0,175,44)] autorelease];
searchBar.delegate = self;
searchBar.showsCancelButton = YES;
searchBar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth;
UIBarButtonItem *customBarItemRight = [[[UIBarButtonItem alloc] initWithCustomView:searchBar] autorelease];
tabBarController.navigationItem.rightBarButtonItem = customBarItemRight;
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchDisplayController.searchResultsTableView.hidden = NO;
[UIView animateWithDuration:.3
animations:^ {
CGRect newBounds = searchBar.bounds;
newBounds.size.width = 350;
searchBar.bounds = newBounds;
}];
[searchDisplayController setActive:YES animated:YES];
}
You should not use UISearchDisplayController for this, you need to implement animation for your custom view controller.