Is it possible to avoid SuperViews when hitTesting? - iphone

Basically, I want to be able to click all the Subviews in the Image below...
I want to be able to tap the Subviews of View B but View A is in the way even though View A Subviews are not blocking the views below. Is this possible? In other words I want to be able to tap through the transparent parts of a view even though its frame/bounds cover that area. View B is under View A in a ScrollView.
thanks,
austin

The solution is to subclass View A (if it's not a custom view already) and override pointInside:withEvent.
Return YES if the specified point is inside one of View A's sub-views, or NO otherwise. When pointInside:withEvent returns NO the system will continue to try other views until it finds one that claims the point is inside it, then it will call hitTest:withEvent to see which inner-view to send the touches to (the default behaviour).

Related

How To Prevent Interface Builder From Adding Items To A Custom View?

UPDATE: Edited for clarity.
This is not a showstopper, but it is annoying, and I'd like to figure out how to address it.
I have a custom UIView. A UIScrollView, to be precise, that is programmatically populated with a bunch of UIViews at runtime. It is not meant to have anything embedded in Interface Builder (IB).
What happens with any UIView, is that when you drag another element over it, the UIView becomes "droppable," and allows the other element to be dropped into it.
There are a few native Apple elements that won't let you drop stuff into them, like UIPickerView.
Is there a flag or something I can set, so that it won't allow IB to add anything to it?
Like I said, not a showstopper. If stuff gets added, it is destroyed before the programmatic population happens, but it just seems "neater" to make it clear that it's not "droppable."
UPDATE: In the screengrabs below, the Login Picker View is a standard UIPickerView, and the Display Results Scroller View is my custom scroller. If I drag something out of the Library, and try to drop it on the Picker View, nothing happens. However, I am able to drop it into my Display Results View.
What I want, is to be able to declare my Display Results View as a "no fly zone," so it is no longer droppable.
I hope that makes it clear.
Cannot Drop onto A PickerView
Can Drop On My Custom View
I'm not saying this is a great solution, but if you're using a storyboard (not a xib) then it seems to work…
In your storyboard, instead of dragging a scroll view out of the library, drag out a container view.
IB automatically creates a new storyboard scene connected to the new container view by an embed segue. Delete the newly-created scene.
Set the container view's custom class to UIScrollView.
IB does not allow you to add any subviews to a container view.
Note that IB also doesn't show you any UIScrollView-specific properties in the Attributes inspector, so if you need to customize this funky scroll view, you have to do it programmatically.
The library doesn't offer the container view in a xib, hence the storyboard requirement.

Moving and existing UI on a UIScrollView

I have some xibs with all sorts of text controls (UITextFields, UITextViews). Since the keyboard obscures some of these text controls when text input starts, I followed Apple's guideline for managing content located under the keyboard.
In short, the solution involves moving all interface elements on a UIScrollView. Doing it from Interface Builder I simply add a UIScrollView on the view, make it the size of the view, send it to back and link it's referencing outlet to the file's owner view property.
In IB this all looks fine, and the UI elements appear above the UIScrollView. However when I run the program, the UI elements are nowhere to be found. Their IBOutlets however seem to get initialized so it looks like they are actually constructed. I've tried to set the UIScrollView alpha to 0 to see if they are placed behind it but I still can't find them. New items that are added to the UIScrollView however, seem to work fine.
This leaves me with the not so great option of rebuilding all my xibs where I need to do this change. It kind of looks like an Interface Builder bug to me. What do you guys think?
Well I've found a solution. It goes like this:
Drag a UISCrollView in IB's main window (where we have the File's Owner and First Responder objects).
Rescale it to the size of the initial view
Drag and drop everything from the original view to the scroll view.
Link the scroll view's referencing outlet to the file's owner view property.
Delete the old view from IB's main window.
#MihaiD
use tableview.contentOffset=CGPointMake(x,y);
I don't know if you really need a UIScrollView. See this question on SO it slides the parent view up.

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 resize other subviews and parent view when one subview size changes

I have a view which is similar to mail app. I have a scroll view and in that many other subviews. When the user clicks on any textview, it expands to show the text in it. e.g. "to" field in mail. I would want to resize the other subviews such that it starts below the expanded region. How can I do that.
I created the view through IB and I marked it for flexible top margin to facilitate this. But nothing happens automatically and hence I was wondering if I need to call sizethatFits/setneedsLayout.
When the subview resizes, call [[self superview] setNeedsLayout]. In your superview, implement -layoutSubviews to do the actual layout. You'll have to calculate everything yourself. Fixed/flexible margins are relevant to resizing the superview, not on peer views. The default -layoutSubviews does nothing at all; it just gets called at appropriate times.
If you need to force layout to happen at a particular point, then you can call -layoutIfNeeded on yourself or your superview. Read the docs on how this method works. Generally speaking you don't need to call this, though. It will usually get called at the appropriate time if you just use -setNeedsLayout.

Nested UIScrollView does not paint

I am building an application which allows the user to view and edit data on a daily basis. To accomplish this i have created a view in Interface builder which contains all of the daily data, and i "tile" this view within an appropriately sized UIScrollView to allow the user to swipe between the days. The view contains two UITextFields, a couple UILabels, and a UITextView. At any given time i have five instances of this view which i re-position around and update the data for the appropriate position.
Everything works great with the exception of the UITextView. The UITextView does not display its Text property until the user has interacted with it (I'm guessing this triggers a redraw).
Is there an easy way to get the UITextView to "paint" its content when its not on screen (but still added as a subview to another view)?
One thing you can try, which is what I just did to fix the same problem, is hook into the parent scrollview's scrollViewDidScroll delegate function. Then in that function, send a message to the UITextView that would force a redraw, my method was setting its frame to the same frame. It's pretty brute force but it's better than nothing.