How to stop search bar from scroling with tableheader for iPhone - iphone

I want to make my search bar static but not scrolling with tableview. I did this:
self.tableView.tableHeaderView = searchBar;
but I want to make my search bar just static. I have a UItableViewController. How to make it static?

You should add the UISearchBar to the main view. Use IB to do this, or use
[self.view addSubview:searchBar];
if you are creating them programmatically. Either way, also make sure the UITableView is positioned some 40px lower.

Related

UISearchBar linked with a UISearchDisplayController in the header of a UITableView in iOS7

I have a UISearchBar linked with a UISearchDisplayController in the header of a UITableView in iOS7.
I set the UISearchBar with the style minimal. When the searchdisplaycontroller shows his table it has the behavior of the picture below. The table is scrolling above the searchbar. When I switch the style of the tableview to Prominent the table scrolls under the searchbar as expected.
Is this a bug or this behavior is expected?
The text isn't floating above the search bar, it's going below. Minimal style doesn't provide a background so this is expected. It will, however, provide a background during activation if there is a navigation bar present (Minimal style really wants to be over either no content or blurred content).
You are free to add your own custom background if you'd like.
Try with
self.edgesForExtendedLayout = UIRectEdgeNone;
in viewDidLoad method. It works for me.
I think this issue may for the Auto-Layout or Auto-Resizing. Just check in the nib file in insppector view.
Make the x=0 and y=0 for search bar and for tableview x=0 and y=44 , now you will get the tableview below the searchbar.

iPhone: How to add a UILabel on top of a UITableView?

In my Universal IOS4 app, I have a UITableview in my xib and I control it with my UITableViewController. As you know tableview by default covers all the area in window left from the navigation bar on top and the toolbar at the bottom.
What I want is to add another UIComponent(probably a big UILabel) just under the navigation bar and place the scrollable tableview just under that UILabel, so UIlabel is not scrollable but only the table is
How can I do and control that?
Thanks
Never mind: I think this will scroll with the table.
You can set the section header to the view below. This may work if you only have one section. Best solution would be to change to a UIViewController.
Programmatically add a header to the table
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0,0,320,30)] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0,0,50,30)] autorelease];
label.text = "Hello";
[view addSubview:label];
self.tableView.tableHeaderView = view;
I would suggest not to go with using UITableViewCOntroller at all. If you use UITableVIewCOntroller to ease the way you use Delegates and Data Sources, then you will have to face this kind of Customization problems.
You will have below problems if you UITableViewController:
can not set the background to a custom UIImage. You can only set the UITableView’s BG property. If you use a Custom VC and add a UITableView to it. you can entirely customize what to keep on top of the View, widtt and height of your table view.
You can not have a static Header view in your View at all. Because if you use UITableVC, you can only have the default header that you can create using Table View's data source methods. But if you use a customized VC and add a TableView to it, you can add your own customized header or controls as a header.
only advantage you will have if you use a UITableViewController is that if you have UITextFields in your Table View, UITableViewCOntroller will automatically scroll the hidden text field to above the Keypad if you start editing one.
I would not suggest you to add anything to UIWIndow, as only the first added view to window will get the rotation events
http://developer.apple.com/library/ios/#qa/qa1688/_index.html
One simple idea is to have another UIViewController (let's call this a_viewController) and set the tableview (let's call it a_tableView) and the label (samewise, a_label) as its subviews.
[a_viewController.view addSubview:a_tableView]
[a_viewController.view addSubview:a_label];
TableView header may not work in case you want the label to stay as you scroll.
But if you have only one section in your tableview, section header may become a handy option.
Can i make a table's tableHeaderView position fixed as I scroll?
One of the way is you can add on UIWindow. But make sure you handle it properly while navigating views... Following is the way...
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];
Create your UILabel instance (myView) and add it as subview in UIWindow...

UISearchBar on the top of UITableView which can hide but stay close to UINavigationBar

I would like to have a UISearchBar on the top of my UITableView which hides when you scroll down: The answer is easy, I just need to add it on my table view header like this:
UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
self.tableView.tableHeaderView = search;
[search release];
But the problem is that when you scroll up the UISearchBar fit to the top of the UITableView, and not the top of the UINavigationBar. To be more clear I made a screen in Mail (not good) and Game Center (good).
I want the same as in Game Center. Do you have any idea how they are doing this ?
You will need to hide the search bar on your own when you scroll the tableview. So, don't put it as a UITableView header. You could hide it by setting its height to zero. That way if your tableview is set to autoresize it will expand.
I would experiment with having the UITableView and the UISearchBar as peers within another view. The GameCenter image does not have the search bar as the table view header, rather it has them as separate subviews.
You could also look at UISearchDisplayController but I think it doesn't quite have the behaviour that you want.
Edit: This question is basically your question and has some code in the answers.
A tableview header will move while scrolling downwards. It is tableview's property.
If you want to place a searchBar with navigation bar. Why dont you place the search bar just below the navigation bar. Use search bar and separate tableview.
The right side 's UISearchBar is on the another UIView, it is not a part of UITableView. So, you can add a child view to put the UISearchBar, and another child view is for the UITableView.
This will answer your question :
iPhone: Hide UITableView search bar by default
same concept, different controller.
Good luck.

How can I add a static background behind a UITableView programmatically?

I have a TableViewController and would like to have a static picture as a background, which doesn't scroll along.
The way that everyone recommends using
[UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundPattern.png"]]
doesn't work as it will
1.) move along and
2.) put the background pattern in every cell
I know how to do it in a XIB file (namely adding another layer underneath the TableView) but how do I do it programmatically from the TableViewController?
[myTblViewController.view insertSubview:myImageView belowSubview:myTblViewController.tableView];
That should work.
If it turns out that tableView is not a direct subview of the table view controller's main view, you can try:
[[myTblViewController.tableView superview] insertSubview:myImageView belowSubview:myTblViewController.tableView]; //Edited superview should be all lower case

UITableView problem with "Inset" property

My navigation-based-app's background looks like a paper on a cork bulletin board. My RootViewController is, of course, an UITableView. I want to have the TableView end on the paper and not on the cork. Some pictures here to explain it better:
alt text http://img528.imageshack.us/img528/3469/bildschirmfoto20091028u.png
This is how it looks when I scroll to the last row of the TableView. For this, I'm using the "Inset" property for "Content-Bottom" and "Scrollers-Bottom" in Interface Builder.
alt text http://img24.imageshack.us/img24/3469/bildschirmfoto20091028u.png
This is how it looks when I scroll to the top of the TableView.
alt text http://img28.imageshack.us/img28/64/35859544.png
And this is how I want the TableView to look when I scroll to the top.
How can I do this?
Thanks a lot ;-)
Yassin
You can make frame of your tableView with the smaller size.height parameter. It will do the trick.
Oh... Ok. I have no mac nearby right now so I don't want to post much code. I will try to explain. First of all, you should inherit your rootViewController from UIViewController, not UITableViewController. If you are using IB, you have to refer your main view to your RootViewController's view property. Then you can set background of you main view
[self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"myImage.png"]]];
or add a UIImageView to your view and set your background image there. Then you have to add a UITableView to your view and set it's frame at any size you want. Next step - to refer your new tableView to the outlet inside your class. Smth like that:
IBOutlet UITableView* myTableView;
in your viewDidLoad: method set your new tableView's background to clearColor
[myTableView setBackgroundColor:[UIColor clearColor]];
and the last one. all your self.tableView you have to replace with myTableView.
P.S. Oh, don't forget that your new tableView should has frame with x,y,width properties, identical to your self.view's ones and tableView's height property should be a bit smaller.
Or just use insertSubview:corkBoard atIndex:1 during viewDidLoad or whatever. The corkBoard name should be whichever view the cork board is on, and index counts how many views are below it. So 1 would be only one view underneath, the UITableView. Make sense? Of course if you have more views you want underneath it, just change the index count.
In my opinion, this works better than setting a frame. The frame would have an awkward whitespace below it, while this just puts the table behind the cork. Quite ingenious :)