Adding UISearchBar programatically - Bar is Off Screen at Launch - iPad - objective-c

I have added a UISearchBar to a table in my iPad app using the below code. The table is on the left hand side of my split view controller.
The problem is that when the app starts the search bar is strangely off screen to the left - you can just see the last few pixels of it. If you click on it, or scroll down and back up, it reverts itself and looks like it should.
Can anyone suggest how to fix this ?
Thanks
searchBar = [[UISearchBar alloc] init];
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:self];
searchBar.delegate = self;
searchController.delegate = self;
searchController.searchResultsDelegate=self;
searchController.searchResultsDataSource=self;

You can try to create the UISearchBar with a frame like:
CGRect searchViewFrame = CGRectMake(33, 33, 264, 31);
or whatever dimensions or origin.
Like other resource, you can create the UISearchBar within a UIView, and add this UIView to the UITableView:
UIView *containerSearch = [[UIView alloc] initWithFrame: searchViewFrame];
searchBar = [[UISearchBar alloc] init];
[containerSearch addSubview: searchBar];
self.tableView.tableHeaderView = containerSearch;

Related

put kGADAdSizeSmartBannerLandscape at the bottom of the screen

I am trying to put kGADAdSizeSmartBannerLandscape at the bottom of my screen.
Here is my code. Please help.
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerLandscape] ;
bannerView_.adUnitID = #"AD ID";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
Simple.. Create a blank View at the bottom with size of 0, 678, 1024, 90 for ipad and similarly for ipod. And then add bannerView_ as a subview of that newly created View

How to move scrollView down 44x to make room for NavBar

I am using the following code to create my scrollview. I would like to move the scrollView down 44px to make room for my nav bar.
scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor clearColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Menu.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[scroll addGestureRecognizer:doubleTap];
self.view = scroll;
any help is appreciated.
Your code is correct, to manually create a frame do:
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,44,320,480)];
:)
This is for iPod and iphone View but this will let the scroll start at 44px
Cant you just add a scroll view in in interface builder as an outlet, place it underneath your nav bar.
Declare scroll view in .h
in .m under ViewDidLoad
[scroll setScrollEnabled:YES];
[scroll setContentSize: CGSizeMake(320, 1000)]; ////or whatever size you want here
You probably want to init it with a different frame... but it depends on where/how this nav bar is being created. If you have a view controller, and these are all subviews of the view controller's view, then you should be creating the objects in viewDidLoad, then just create the nav bar first, using self.view.bounds to obtain the initialization width. I assume you'll want to put UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin as the autoResizingMask here. Then if the scrollview is the rest of the view below the nav bar, you can create the frame for it using
CGRect scrollFrame = CGRectInset(self.view.bounds, 0, navbar.bounds.size.height)
Put an autoresizingMask of UIViewAutoresizingFlexibleWidth | UIViewAutoResizingFlexibleHeight on the scrollview.
If you are creating the views in a different place/way, then some of that might need modification. I was assuming your view is a nav bar at the top, x pixels tall (44 in this case but it doesn't and shouldn't matter in the context of setting the scrollview frame). and then a scrollview that fills the rest of the view.

UIScrollView, Pull down effect similar to CalcBot

I'm trying to make a UI similar to CalcBot where you can pull down the view controller with your finger and it reveals a hidden background view.
I think this is done with the UIScrollView but cannot work out the exact method to have for example 2x Horizontal views with one of them having a vertical.
I do have the horizontal scroll working fine, i'm just stuck with how I can make screen 1 pull down with a vertical scroller.
Any tips or pointers would be appreciated.
Thanks Aaron
You can have nested scrollviews to get scrolling working in both directions. Let's have a vertical scroller inside a horizontal scroller (though you can very well do it the other way round too), so views 3 and 1 get into a vertical scrollview, which in turn gets inside a horizontal scroller along with view 2.
You could create such a view hierarchy in code (probably in the main viewcontroller's viewDidLoad). The following snippet assumes view1, view2 and view3 as webviews.
CGRect bounds = self.view.bounds;
// main guy is a horizontal scroller
UIScrollView *hScrollView = [[UIScrollView alloc] initWithFrame:bounds];
hScrollView.contentSize = CGSizeMake(bounds.size.width * 2, bounds.size.height);
hScrollView.delegate = self;
[self.view addSubview:hScrollView];
// the horizontal scroller contains a vertical scroller
UIScrollView *vScrollView = [[UIScrollView alloc] initWithFrame:bounds];
vScrollView.contentSize = CGSizeMake(bounds.size.width, bounds.size.height * 2);
vScrollView.delegate = self;
[hScrollView addSubview:vScrollView];
// add view3 and view1 to the vertical scroller
UIWebView *view3 = [[UIWebView alloc] initWithFrame:bounds];
[view3 loadHTMLString:#"<h1>3</h1>" baseURL:nil];
[vScrollView addSubview:view3];
UIWebView *view1 = [[UIWebView alloc] initWithFrame:CGRectOffset(bounds, 0, bounds.size.height)];
[view1 loadHTMLString:#"<h1>1</h1>" baseURL:nil];
[vScrollView addSubview:view1];
vScrollView.contentOffset = CGPointMake(0, bounds.size.height);
// add view2 to the horizontal scroller
UIWebView *view2 = [[UIWebView alloc] initWithFrame:CGRectOffset(bounds, bounds.size.width, 0)];
[view2 loadHTMLString:#"<h1>2</h1>" baseURL:nil];
[hScrollView addSubview:view2];
// enable paging in both directions
hScrollView.pagingEnabled = TRUE;
vScrollView.pagingEnabled = TRUE;
That would have you scrolling in both directions but that would also let you scroll-right from view3 to view2. If you want to prevent that, you should disable scrolling on hScrollView whenever the vScollView is showing view3. You can do that in scrollViewDidEndDecelerating: in the delegate of vScrollView. Something like:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView == self.verticalScrollView) {
self.horizontalScrollView.scrollEnabled = (self.verticalScrollView.contentOffset.y > self.view.bounds.origin.y);
}
}

Resizing UITabBarController Child Views

I'm sure this is a matter of me not knowing how to phrase my question, but I'm at a loss.
I'm working on an app with 2 subviews that will be on the top of the screen at all times (taking up a total of 114 pixels including the status bar). I want the rest of the space to be taken up with a UITabBar and it's child views.
The closest I've been able to get is a tab view that is sized correctly but is positioned at 0,0 so it's under my persistent subviews.
// Create the main toolbar
toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0.0, 20, 768, 44);
UILabel *textLabel = [[UILabel alloc] init];
textLabel.frame = CGRectMake(240, 20, 300, 40);
textLabel.textAlignment = UITextAlignmentCenter;
textLabel.backgroundColor = [UIColor clearColor];
textLabel.text = #"Character Name";
[self.window addSubview:textLabel];
[textLabel release];
...
// Create the stats bar
StatsViewController *statsView = [[StatsViewController alloc] init];
statsView.view.frame = CGRectMake(0.0, 64, 768, 50);
[self.window addSubview:statsView.view];
// Create Tab Bar Controller and Tab View Controllers
tabBarController = [[UITabBarController alloc] init];
...
[self.window setRootViewController:tabBarController];
[tabBarController release];
[self.window addSubview:toolbar];
[self.window bringSubviewToFront:statsView.view];
[self.window bringSubviewToFront:textLabel];
[self.window makeKeyAndVisible];
return YES;
A tab bar controller (or any view controller, for that matter) will always try to resize its view so that it takes up the entire bounds of its superview. So if you want the tab bar controller to not do that, I would create another plain view controller that becomes your new root view controller.
In that view controller's view, you create 2 container subviews, one for your statsView and one for the tab bar controller. Now you add the tab bar controller's view to the one container view and the StatsViewController's view to the other.
(Note: I did not test this.)

Position of UIScrollView after change in orientation

I have a UIScrollView with an image inside it. This is the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIScrollView *imScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 96)];
[imScrollView addSubview:imView];
[imScrollView setContentSize:CGSizeMake(530, 96)];
[[self view] addSubview:imScrollView];
[imView release];
[imScrollView release];
}
This is what it looks like in portrait mode:
I want to know
1. How can I position the image exactly in the middle of the screen (horizontally).
2. How can I make sure this is going to be the same if the orientation switches to landscape. This is what currently the landscape looks like :
Thanks.
Try setting the autoresizingMask property of your imView just after you create it:
imView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
The UIView class reference contains more info.