UISearchBar inside a NavigationBar Get Deferenced When Keyboard Shows Up - objective-c

I'm putting a search bar inside a navigation bar. here is the code
self.navigationBar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
[self.view addSubview:self.navigationBar];
self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneButtonPressed)];
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = #"Name of City";
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTitle = #"Add Location";
self.searchController.displaysSearchBarInNavigationBar = YES;
self.searchController.navigationItem.rightBarButtonItems = #[self.doneButton];
self.navigationBar.items = #[self.searchController.navigationItem];
but the search bar behaves incorrectly.
I'm expecting it would look like this

I've fixed this by bring the navigationBar to front after setting searchBar as the first responder
[self.searchController setActive:YES animated:NO];
[self.searchController.searchBar becomeFirstResponder];
[self.view bringSubviewToFront:self.navigationBar]; //this is the line I add

Related

Can't Set Title of NavigationBar

self.title = #"title";
self.navigationController.title = #"title";
self.navigationItem.title = #"title";
self.navigationController.NavigationItem.title = #"title";
Nothing changes the Title. What can I do?
this is how I made it in the MainTabBarController, but I can't change it for example in the SettingsViewController.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationBar *navbar = [[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 60)];
UINavigationItem *navItem = [UINavigationItem alloc];
navItem.title = #"Test";
navbar.translucent = NO;
[navbar pushNavigationItem:navItem animated:false];
[self.view addSubview:navbar];
}
My Solution was to make in every ViewController it's own NavigationBar and hide the NavitagionBart from the MainTabController.

ScrollsToTop is not working when UIVeiwController is presented from ios8 extension

ScrollsToTop is not working when UIVeiwController is presented from ios8 extension.
But it is working if we presented it from inside the app. Can anyone help? sample code is shared here.
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 300) ];
[scrollview setContentSize:CGSizeMake(320, 800)];
scrollview.layer.borderWidth = 1.0;
scrollview.backgroundColor =[UIColor redColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"slide1"]];
[scrollview addSubview:imageView];
scrollview.showsVerticalScrollIndicator =YES;
scrollview.scrollsToTop = YES;
scrollview.delegate = self;
UIViewController *a = [[UIViewController alloc] init];
a.view.backgroundColor = [UIColor greenColor];
[a.view addSubview:scrollview];
[self presentViewController:a animated:YES completion:^{
}];
I had same issues & resolved using code bellow.
Some change has done in xcode6. Try this code to make scrollview to top.
[self.ScrollView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];

IOS 8 Objective c UIBarButtonItem action doesnt work

Im using UIBarButtonItem action and it works on ios 7 and not on ios 8
In ios 7 after clicking on the button the method cancelButtonPressed (which defined in action) is running but in ios8 nothing happend the cancelButtonPressed method doesnt called.
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
Im using it in barcode scanner plugin, it is open the camera and in that screen i have the cancel button:
- (UIView*)buildOverlayView {
if ( nil != self.alternateXib )
{
return [self buildOverlayViewFromXib];
}
CGRect bounds = self.view.bounds;
bounds = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
UIView* overlayView = [[[UIView alloc] initWithFrame:bounds] autorelease];
overlayView.autoresizesSubviews = YES;
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.opaque = NO;
UIToolbar* toolbar = [[[UIToolbar alloc] init] autorelease];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
//self.navigationItem.rightBarButtonItem=cancelButton;
id flexSpace = [[[UIBarButtonItem alloc] autorelease]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil
];
#if USE_SHUTTER
id shutterButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:(id)self
action:#selector(shutterButtonPressed)
];
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,shutterButton,nil];
#else
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,nil];
#endif
bounds = overlayView.bounds;
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGFloat rootViewHeight = CGRectGetHeight(bounds);
CGFloat rootViewWidth = CGRectGetWidth(bounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[overlayView addSubview: toolbar];
UIImage* reticleImage = [self buildReticleImage];
UIView* reticleView = [[[UIImageView alloc] initWithImage: reticleImage] autorelease];
CGFloat minAxis = MIN(rootViewHeight, rootViewWidth);
rectArea = CGRectMake(
0.5 * (rootViewWidth - minAxis),
0.5 * (rootViewHeight - minAxis),
minAxis,
minAxis
);
[reticleView setFrame:rectArea];
reticleView.opaque = NO;
reticleView.contentMode = UIViewContentModeScaleAspectFit;
reticleView.autoresizingMask = 0
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin
;
[overlayView addSubview: reticleView];
return overlayView;
}
what is the alternative way for using it on ios8?
remove autorelease and initialize bar button like that
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
self.navigationItem.rightBarButtonItem=cancelButton;
You might want to make a global variable in class defination or a property for cancelButton instead of using local variable. I guess arc is removing cancelButton from memory.

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.

Bottom half of UITabbar is not responding to clicks

I have created a UITabbar with 3 items and placed it on a UIScrollView
When I click the buttons of the tabbar they do not respond in the bottom half.
The upper area is working fine.
When clicking in the area just above the tabbar the tabs are also switched.
What can be wrong?
How can i correct this misalignment of the clickable button area?
In viewDidLoad:
[super viewDidLoad];
scroll.frame = CGRectMake(0, 20, 320, 460);
scroll.pagingEnabled = YES;
scroll.contentSize = CGSizeMake(320 * 2, 460);
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.scrollsToTop = NO;
scroll.delegate = self;
scroll.pagingEnabled = YES;
viewNavController1 = [[viewNavController1 alloc] init];
ctrl = [[UITabBarController alloc] init];
ViewController1 *viewC1= [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *control = [[UINavigationController alloc] initWithRootViewController:viewC1];
viewC1.title = #"Title1";
[viewC1 release];
ViewController2 *viewC2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
UINavigationController *control2 = [[UINavigationController alloc] initWithRootViewController:viewC2];
viewC2.title = #"Title2";
[viewC2 release];
UINavigationController *control3 = [[UINavigationController alloc] init];
ViewController3 *viewC3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
[control3 pushViewController:viewC3 animated:NO];
viewC3.title = #"Title3";
[viewC3 release];
[ctrl setViewControllers:[NSArray arrayWithObjects:control,control2,control3,nil]];
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * 0;
frame.origin.y = 0;
viewNavController1.view.frame = frame;
viewC4 = [[ViewController4 alloc] initWithNibName:#"ViewController4" bundle:nil];
[viewNavController1 pushViewController:viewC4 animated:NO];
[scroll addSubview:viewNavController1.view];
frame = scroll.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
ctrl.view.frame = frame;
[scroll addSubview:ctrl.view];
[scroll scrollRectToVisible:CGRectMake(320, 0, 320, 460) animated:NO];
UITabBarItem *itm = [ctrl.tabBar.items objectAtIndex:0];
itm.image = [UIImage imageNamed:#"img1.png"];
itm = [ctrl.tabBar.items objectAtIndex:1];
itm.image = [UIImage imageNamed:#"img2.png"];
itm = [ctrl.tabBar.items objectAtIndex:2];
itm.image = [UIImage imageNamed:#"img3.png"];
[control release];
[control2 release];
[control3 release];
The problem was that the UITabbarController did not have its view added as the root view of the windows. Which it apparently assumes.
so I had to trick it using:
[vc setWantsFullScreenLayout:YES];
vc is the main ViewController holding the scrollview containing the UITabbarController.
See Offset on UIWindow addSubview for more explanations.