iOS: Add UIView to UITableView - iphone

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?

Related

Can a UIView know when it is on screen

I have a UIView with a lot of components enclosed in it and would like to update some of them if the view is removed or if its parent view controller is popped/pushed. Is it possible for a UIView to get this information
Similar to the method in UIViewController
-(void)viewWillAppear;
Would like something like
-(void)UIViewWillAppear;
Edit for some comments I saw:
I'll explain a bit more
But I have a special case where the UIView needed to add a "floating view" on top of itself (Imagine a zooming/panning/scrolling UISCrollView subclass with floater on top of itself) such that when it scrolled the floating view stayed in place relative to the superview. I tried recalculating the new origin of the "floater" inside of the -(void)layoutSubviews method but the re-placement was very choppy. In order to solve this choppyness problem, the custom UIView added the floating view (which in theory is its subview) as a subview for its superview (a bit of a tongue twister :) ).
Now arises a problem when the custom UIView is removed (or its containing view controller is pushed offscreen). How can the special UISCrollView remove the floating view from its superView.
You can override willMoveToSuperview: to find out when a view is inserted into a hierarchy and when it's removed. That's probably not what you want since the view can be part of a hierarchy and not be inserted by itself.
To find out if it's on screen use willMoveToWindow:. If the argument is non-nil the view just became part of a visible view hierarchy.
If you need to do something after the change use didMoveToWindow:.
UIView do not appear/disappear 'randomly' or when they want - your view controllers (or code) control this. So you should find out when you show them, and call code you need.
The UIView class reference has some kvo observing change.
By implementing -(void)willRemoveSubview:(UIView *)subview you could see the other way round.
UPDATE After reading the explanations:
I hope I understood correctly. I did something similiar time ago, but with a UITableView rather than a UIScrollView (but they are quite the same underneath).
It was like a popup detail view. I solved, as you already did, by adding the detail view to the UITableView superview, and then I added a close UIButton in the detail view, with a corresponding IBOutlet:
#interface CustomUIView : UIView
#property(nonatomic,weak) IBOutlet UIButtonView *closingButton;
-(void)closeDetail:(IBAction)action;
#end
and the action was just:
-(void)closeDetail:(IBAction)action {
// do your cleaning/change to the detail subviews
[self removeFromSuperview]; // <-- clsoe the detail view
}
sdsds

View with uitableview, tabbar and more objects

I'm developing an app and I need a view with these elements:
- UITableView
- UITextField and UIButton
- TabBar
As you can guess, I am developing a chat, but when I put the elements in the .xib, I can't show de layer: UITextField and UIButton. If I put them without a view, they dont appear.
If I put a view under the table, and in that view I put the text and the button I get this error: http://pastebin.com/CKfxijz9 (I put the error there because it's to long)
Thanks in advance
There are some ways of dealing with that, depending on how it should behave. Some of them are:
Provide a table footer or header view that holds the button and
the text field.
Nest the table view into another view. The table
view and the view containing the button and text fielt are on the
same level within the view hierarchy. They are rather siblings than
sub- and superview to each other.
Use a UISlider instead of a table. (However, I personally would use the table.)
Here's an approach I've used in the past (not sure if it's best practice, but it works).
Add your button and textfield to a new view (let's call it, bottomView)
Add bottomView to the superview of your tableview
Set the frame of your bottom view so that it fits to the bottom of the screen (this will make it so your tableview will scroll, but keep your bottomView always attached to the bottom of your mainview)

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

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

Add a UIView after a UITableView but before a UITabBar?

I'm trying to add a UIView, in particular a UIImageVIew, after (that is, below, but not in a z-index sense) a UITableView but before (that is, above) a UITabBar.
You know, the typical "banner/adv space" that you can see everywhere.
My main problem btw is that i don't know exactly where to put it, as a subview of wich view specifically; the UITableView resize automatically according to the space left from the UITabBarController's main view height, minus the height of the tabbar.
I would like it to be put inside the UITableView instead of somewhere else beacuse it is more related to the content of the UITableView, but i have all the autoresizing problems of above. I've tried playing with autoresizingmask, and with the autoresizesubviews flag, but without success. I've even tried the footer ot the UITableView, but that is not fixed in position, it scrolls away if the table is long (expected, and normal).
Is there a way to add a subview in that point, stretching the table itself correctly?
Thanks everybody.
Use a normal UIViewController instead of a UITableViewController. Use the view controller's view as a container view in which you place the table view and the image view. Your view controller can still act as your table view's delegate and/or datasource.