Can a UIView know when it is on screen - iphone

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

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?

Using UIViewController with a UIView - (Want to use UIScrollView instead) - How to make the switch

I need to implement a scrolling window in order to accommodate all the items on the form I am creating. My current implementation is a UIViewController (that's vcAddCourse) and it has a UIView in it with my current form.
Here is what I have done to add the UIScrollView into the equation.
1) Using IB, I dragged a uiscrollview object 'into' the existing uiview object.
2) Using IB, Ctrl-dragged from file owner to the IBOutlet I created for the new uiscrollview
Here is how I init this controller.
3) I made sure that all the items on my form were now dragged to be under the UIScrollview object.
Finally, over in my .m file, in the ViewdidLoad() function, I added the following line:
theScroller.contentSize=CGSizeMake(328, 680);
No joy. I see the form but it does not scroll.
Note: this on iphone simulator.
What else must I do to swap out the UIView for the new UIScrollView?
Thanks!
To keep things like these clear, I keep the scrollView and the (larger) subview separate in the xib file (as siblings). Your form should be on a UIView, and would be larger than the scrollview. That way you can use IB to layout the form exactly as you want.
To make things work you just have to have the IBOutlets to the UIScrollView and the UIView, and add the view to the scrollview in the viewDidLoad method of your viewController:
[theScroller addSubView:formView];
I'm not exactly sure, but I don't think you have to set the contentSize manually after this, as it should automatically be made big enough to fit your view. If it's not working you might try this:
theScroller.contentSize = formView.frame.size;
Hope this helps

determine if uiview is displayed

is there a possibility to determine if an uiview obj is going to be displayed. imagine: you have 2 uiviews in an uiscrollview. now you are going to switch per gesture from the first view to the second. the first view now is NOT in the viewport. now you are going to go back to the first view. and now I want to be notified that this view is in viewport, or is redisplayed. the same has to be for the second view. I have not found any callback or something like this.
You make sure your UiViewController overrides viewWillAppear: (before it appears this method is called) or viewDidAppear: (after this method is called).
See: http://developer.apple.com/iphone/library/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/viewWillAppear:
That depends what you mean by "switch". If one view is just scrolled out of the visible area of the scrollview, but still remains attached as a subview to it, then you may want to check if the bounds of your view overlap those of the scrollviews visible area.
You could do this by using UIScrollView Delegate's scrollViewDidScroll: method to implement a check for overlaps while the user is scrolling.
If however your view is actually removed from the viewstack, then you may want to subclass UIView and implement willMoveToSuperview: to check if the view has been added to the scrollview again.

How to prevent a UiView from sliding down underneath the TableView?

I have a beginner iPhone project going. I have a TableView, which is just a listbox with a bunch of entries and a small UiView on the bottom. See the pic.
The problem is that the UiView essentially becomes part of the listbox and will only appear when I scroll down to the last item in the TableView.
I'd like the UiView to be anchored on the bottom and never move. I've looked all around the Interface Builder and can't find anything (though in its defence I am a total noob to iPhone dev). How do I accomplish a trick like this?
alt text http://www.sqleffects.com/mystuff/ibissue.jpg
Ok, what you need is a UIView, that contains
an UIView, which acts as the TableViewController and thus has to implement the UITableViewDelegate and UITableViewDataSource protocols (but it MUST NOT be derived from UITableViewController directly, since by doing so the UITableView will automatically take up the whole screen size (except toolbars, navbars and/or tabbars))
another UIView, the one you would like to place at the bottom
by doing so, you can create an UITableView in IB (not an UITableViewController!) and connect it with the UITableView property in your UIView (the one mentioned in 1.)
using this method it is possible to give the UITableView a fixed size (which you'll need to, to have room at the bottom for your second UIView)
To build off gabtub's answer, the UIView containing your table view and your bottom view doesn't need to implement the UITableViewDelegate and UITableViewDataSource methods.
Since it sounds like you're building with a view controller, I'd make your main view controller a subclass of UIViewController (instead of using UITableViewController). You could then add your UITableView and your UIView the the UIViewController's view instance.
I'd then make your UIViewController subclass implement the UITableViewDelegate and UITableViewDataSource protocols - you'll end up with something that looks similar (code-wise) to your old UITableViewController subclass, but it's view property will be the underlying UIView instead of UITableView instance (if you poke around in the debugger, you can see the [UITableViewController view] and [UITableViewController tableView] return the same object)
One of the advantages over gabtub's suggestion is it saves you from creating a one-off UIView subclass, since you've probably already got a one-off UIViewController subclass (or, previously had a one-off UITableViewController subclass).
The problem is probably, that right now, the UIView is a section footer view, and is therefore contained within the table view.
If you delete the UIView from the TableView, scale the TableView to make space for the UIView and a bit more and put your UIView where you want it, it probably will be added to the TableView's superview. You can then resize the TableView to the correct size.
The bottom line is this: if you create your project based on the Navigation template, you can't resize the UiTableView, period.
So to do what I want, I basically have to start from scratch and pick a View or Window based template and drop the UiTableView on it manually.

How to add UIScrollView to Interface builder?

I have all my controls laid out in interface builder (many labels, buttons etc). How do I put them all in a scroll view in interface builder so that I can have more space and be able to scroll up and down to reveal more controls? Do I have to do this programatically?
Open the view that has all the controls and labels, etc. (in Interface Builder). Select All. Then under the Editor menu, select Embed In, then Scroll View.
Note: in older Xcode versions, this is under the Layout menu, then Embed Objects In... (scroll view).
My preferred solution, where you don't need to hard-code the size of the contentSize:
NB: you might be able to avoid the source-code parts of this using the trick here: https://stackoverflow.com/a/11239123/153422 - although I haven't tried it yet.
The rest of this trick ... you still need to use anyway
Move all controls into a single UIView (in IB: select all, then go Layout > Embed Objects In ... > View)
Hookup that single UIView to your source code using an IBOutlet property (see below)
IN SOURCE CODE, NOT INTERFACE BUILDER (IB is broken here, it has bugs where it sets the origin of the UIScrollView incorrectly - it tries to center the view. Apple never bothered to check it for basic bugs, sigh): Move the single UIView into a UIScrollView (see code below).
Use sizeThatFits to "automatically" set the correct size.
Code (StackOverflow won't let me put code inside a numbered list. Sigh)
Header file:
/** outlet that you hook up to the view created in step 1 */
#property(nonatomic, retain) IBOutlet UIView *masterView;
Class file:
/** inside your viewDidLoad method */
[scrollview addSubview: masterView]; // step 3
scrollView.contentSize = [masterView sizeThatFits:CGSizeZero]; // step 4
...although I haven't checked this recently, IIRC it works on both 2.x and 3.x
Select all the objects you want to put into a scroll view and go to the Layout menu, choose "Embed Objects In" and choose "Scroll View".
Its easy:
First add a scrollview to your view.
Change the size of the scrollview (e.g. make it 700 pixels long).
Start putting your controls
When you want to put/edit controls in the lower (invisble) part, select the scrollview and change the Y-start position to -300.
Voila.
After editing set the Y-start position back to 0 or whatever it was.
I don't know if it's just me, but I tried to follow the instructions in each of the other answers here and none of them worked. None of the answers included everything needed, each one I guess assuming we know to do something so leaving that part out. I finally figured it out with the help of red artisan. So... I am listing here ALL the necessary steps to get this to work:
In InterfaceBuilder, add a View and then add your controls to it (or if your controls already exist in the main view, you can select all your controls and then go to Editor | Embed In | View, then drag that new View so it is all by itself outside the main view). This View can be any size you like.
In InterfaceBuilder, add a Scroll View to your main view, and size it to take up the whole main view.
Add the code listed below to your UIViewController Header and Class files.
In InterfaceBuilder, hook up the View containing your controls to 'contentView' in the File's Owner. Hook up the Scroll View to 'scrollView' in the File's Owner.
Header File:
#interface MyViewController : UIViewController
#property(nonatomic, retain) IBOutlet UIScrollView *scrollView;
#property(nonatomic, retain) IBOutlet UIView *contentView;
Class File:
#synthesize scrollView, contentView;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.scrollView addSubview:self.contentView];
self.scrollView.contentSize = self.contentView.bounds.size;
}
- (void)viewDidUnload
{
self.scrollView = nil;
self.contentView = nil;
[super viewDidUnload];
}
Although this question is very old, I will suggest a workaround I found as I had the same issue and wasn't able to find much help out there:
When in IB, if you want to place objects outside the 420 pixel, just make sure yourself of having selected Unspecified for all of Status Bar, Top Bar, and Bottom Bar for the View that contains the Scroll View with all the objects. This way, you'll be able to manually resize the screen (for the View). Then, you can follow Ximonn's advice on resizing the H value for the Scroll View, having access to all the other objects, working with them and then, undoing changes for H value and then setting the needed Bars.
Important little thing.
To scroll big subview (UIImageView for example) in UIScrollView remember, for this subview, uncheck "User Interaction Enabled" checkbox in InterfaceBuilder -> View window. Or do it programatically.
subview.userInteractionEnabled = NO;
Otherwise this subview will stack on screen without any effect.
I've been looking for this for a few days, and I finally came across this site with a solution that worked for me.
Scrolling with UIScrollView
Basically you have your main view with a UIScrollView object in it. Then another content view with all your content in it. Then you add the content view to the scroll view. And then finally set the size of the scrollview's content size to the size of the content view.
I know, this thread is a bit older... But somebody could find it on google, it's hight ranked.
I wrote this little helper Method to get the job done:
- (void)addSubview:(UIView *)theSubView toScrollView:(UIScrollView *)theScrollView
{
[theScrollView addSubview:theSubView];
theScrollView.contentSize = theSubView.bounds.size;
}
You just have to declare two IBOutlet's in the header (e.g. contentView and scrollView) and call the method like this, whereever you want to load a UIView into a UIScrollView with your sourcecode:
[self addSubview:contentView toScrollView:scrollView];
I called it in viewDidLoad
This method features iOS
The selected answer works well for Xcode 3.
However, for Xcode 4, menus have been re-arranged slightly.
To do the same in Xcode 4 select your views then use:
Editor > Embed In > Scroll View