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

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

Related

iOS: Add UIView to UITableView

I'm trying to add a UIView on top over the UITableView to mimic the iPhone Facebook style menu. I have it working fine by making the controller a UIViewController then adding a tableview however I am unable to make the menu a static menu unless the controller is a UITableView.
Is it possible to add a view ontop of a tableview and only make the tableview in the background scrollable without the view in the foreground scrolling?
Here is what I have with the subclass being UIViewController
But I am unable to make the tableview cells static via IB since it is not a subclass of UITableView Controller.
EDIT per NSJones Code:
It seems to be going somewhat in the right track. However the view still blocks the table. If I remove the view from the storyboard it will only display the table.
You can make a view hover the same way you make any real thing hover; Hold it up with something invisible.
Basically what you want to do is create a clear UIView (with user interaction disabled) that is the size of your view controller's view, and add it as a subview to your view controller's view property. That way it sits invisibly on top. then you can add a subview to that clear view and that subview won't move.
Edit:
It seems this nice clean approach won't work for you since you need your view controller to be a UITableViewController. The answer for this slightly more complex approach is to use a delegate method for UIScrollView which also works for UITableView. Apple has a fantastic demo of this concept in the WWDC2011 - Session 125 - UITableView Changes, Tips, Tricks video. If you can watch it I highly recommend it. The meat of this issue begins at about 36:10.
But to sum it up you implement the scrollViewDidScroll: delegate method. And handle the movement of the tableview by adjusting the position properties of the view. Here I am keeping an UIView property named viewToKeepStill still using this method.
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// CGFloat stillViewDesiredOriginY; declared ivar
CGRect tableBounds = self.tableView.bounds; // gets content offset
CGRect frameForStillView = self.viewToKeepStill.frame;
frameForStillView.origin.y = tableBounds.origin.y + stillViewDesiredOriginY; // offsets the rects y origin by the content offset
self.viewToKeepStill.frame = frameForStillView; // set the frame to the new calculation
}
Instead of adding it as a subview of the table view, add it as a subview of the superview of the table view; that way it won't scroll.
So instead of this:
[tableView addSubview:viewController.view];
Do this:
[tableView.superview addSubview:viewController.view];
Assuming you want something that is visible full-time with the table, start with a view which contains both the menu view and the UITableView. Make the table smaller so it ends where the menu view begins. The table view can work with less vertical space.
If you have your UIViewController's view to be your table view then your table is going to span over the whole screen, so you won't be able to add anything on top of it.
Why not try the following:
1) create a new UIViewController
2) add a view on top where you want your menu
3) in the space left under just drag a table view from the component library
4) don't forget to set the 2 table view delegates to be your view controller class
that's about it?

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...

Integrate an UIScrollView to an existing view

I have a View in a xib file and a corresponding ViewController.
With Interface Builder I put an image, some labels and now a table view in it.
What I want to have, is that the whole view will be scrollable, because the new table view is currently only scrolling inside the tableview boundaries.
How can I put a scroll view behind this existing view with IB?
I already put a UIScrollView to the IB and put all other elements on it. Also I create an UIScrollView IBOutlet and connect it, but the screen is not scrollable...
Do I have to do something like
[myScrollView addSubview: ???];
Thank you a lot in advance & Best Regards.
You need to set the contentSize property of the scrollview before it is usable. Yeah, I know - freaking ridiculous that this isn't automatic.
Just do myScrollView.contentSize = CGSizeMake(int, int).

Adding a subview to UITableCellView

I want to add a subview to the UITableCellView class. However, non of the provided views in the class seem to be able to do exactly what I was looking for.
I basically want to add my own background view, filling the whole cell. However, if I replace the backgroundView, the style from the grouped table view layout isn't displayed anymore. If I add a subview to backgroundView, the subview is not shown at all. If I add a subview to the contentView, I can't draw behind the accessory icon.
What am I missing?
Basically you can't change the backgorund of GroupedTable View.
Try using it with PlainTable.
and add the your backgroung image (of size = cellsize) to cellforRowAtIndex method.
You might want to take a look at this article:
"Easy custom UITableView Drawing"
In particular:
First: the UITableView does not itself
draw anything except the background.
To customize the background of a
UITableView, all you need to do is set
its backgroundColor to [UIColor
clearColor] and you can draw your own
background in a view behind the
UITableView.
Simply add the custom view as part of your contentView. Set a unique reuse identifier for that cell, configure it when you create it and from then on simply reset the data components (this is easiest to do if you create a custom cell controller class so that it can track all the parts and use setters/getters for the data).

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 :)