Disabling Userinteraction of all views besides one - iphone

I am new to iphone.I am struck in my project at some task that is I have a view controller in that lot of buttons and webview is there when i click on webview it recognize the gesture and set some size to webview but tere is some buttons are visble in the screen at that time i want to disable the interaction of all other things in view controller except the webview.Similarly like as UIAlertView there also userinteraction disabled with all other things except the alertview.If any body know this please help me...

This will disable user interaction on all views except for the uiwebview
for(UIView *currentView in self.view.subviews)
{
if([currentView isKindOfClass: [UIWebView class]])
{
currentView.userInteractionEnabled = NO;
}
}

You can set the property userInteractionEnabled=YES //or No of any UI Object.

You can place your webview on a transparent view ( let's name it "coverView" ) , which frame equals to screen's frame. When you present the coverView (with the webview sticked to it) the user will see everything around the webview (because the coverView is transparent), but any interraction with other objects around the webview will be blocked by the coverView.

Related

Disable touch events on Superview only?

I have a subview on the screen that is loaded from a XIB, I need to disable touch events ONLY on its superview while not touching the subview in the end. How would I do this?
Also, I am using iOS.
Thanks!
This is how you can go about doing this -
Go to interface builder & select your superview (for which you want to disable touches).
Open right navigation bar. At the bottom there is a checkfield called "User Interaction Enabled". You can unmark it. Of course you can go about doing this from code too. But this is more graphic. This screenshot might help. See bottom of the image...
I am also new to iphone development but htis piece of code helped me out when i tried to move only UIImage view.
you can apply cahnges on this code if you are using buttons or labels in your view.
if ([touch.view isKindOfClass:[UIImageView class]])
{
imageDemo.center = touchLocation;
}
In XIB, rather than using all other views as subview, u can just use superview at same level and set its usertouch interface as FALSE.

How to detect tap on uiview with lots of controls?

My problem is about tap detection.
I have a uiviewcontroller and there are some controls on uiview (labels, buttons, tableview, imageview, etc..)
When I tap the uibutton I display a small uiview (200x150), if the user taps the uibuttons in smallview I hide the smallview.
But I can't hide the uiview if the user taps the background.
I tried this code..
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//NSLog(#"Touches began.");
[self hideShareView];
}
It doesn't work if I tap the another button in the uiviewcontrols view.
I just want my uiviewcontrol's uiview to react first.
I think its about firstResponder but I dont know how to set it first.
edit: i want it to work like a uiPopover in ipad.
I believe that the correct approach is to add a new transparent UIView when you display your "smallview". You should add a UITapGestureRecognizer to that UIView, in order to trigger the desired selector when the tap is detected. Also, you must ensure that the views are arranged properly, with your smallview being the one at the top, the transparent UIView being immediately below and the rest of the view hierarchy below the transparent UIView.
Finally, you should ensure to remove the transparent UIView from your view hierarchy at the same time that you remove your smallview.
Does that make sense?
Please have a look at this question (how-to-make-a-superview-intercept-button-touch-events), it does seem highly related.
try your hands with bringing your small view (i.e. shareview) to front or sent your main view behind your small view.
If it still doesn't work & you don't want your main view to perform any action when smallview is opened then try
[<YOUR_MAIN_VIEW> setUserInteractionEnabled:NO];
, but MAKE SURE you can do this only when you don't want your main view to perform any action when smallview is opened

Automatic scroll to top doesn't work in UITableView

usually when tapping the top of the screen the tableview scrolls all the way to the top. For some reason this doesn't work in one of my view controllers.
The hirarchy is as follows:
UIView
-> WebView
-> TableView
----->SearchBar
SearchDisplayController.
I think I have everything hooked up correctly (datasource, delegate, ...). I have a similar view controller where everything works. The only difference seems to be WebView, which is missing in the view controller where the tap-and-scroll-to-top works...
Any ideas?
Best regards,
Sascha
You have probably added some view to your view hierarchy that is a UIScrollView or contains scroll views (e.g. UIWebView or UITextView). When you tap the status bar, iOS searches for the topmost scrollview with scrollsToTop set to YES and scrolls to its top.
Add this to your class:
- (void) disableScrollsToTopPropertyOnAllSubviewsOf:(UIView *)view {
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
((UIScrollView *)subview).scrollsToTop = NO;
}
[self disableScrollsToTopPropertyOnAllSubviewsOf:subview];
}
}
…and call
[self disableScrollsToTopPropertyOnAllSubviewsOf:myAddedSubview];
to solve the problem and let your table view scroll to top again.
You need to disable scrollsToTop on all but one of the scrollable views. Since UITableView is a descendant of UIScrollView, it's easy to do. However, if you want to disable it on the webview, it's a little trickier. This seems to work:
[webView.subviews objectAtIndex:0].scrollsToTop = NO;
But that is a little iffy since it's poking around in undocumented parts of the webview. It may stop working if Apple decides to rearrange the subviews.
Don't know what Apple were smoking but by design, the behaviour on iPhone is distinctly different from iPad. Apple's documentation does include a "Special Condsideration" note as follows...
On iPhone, the scroll-to-top gesture has no effect if there is more
than one scroll view on-screen that has scrollsToTop set to YES.
So on iPhone you have to ensure that there is only ever one UIScrollView (or UITableView/UICollectionView/UIWebView etc) that has its scrollsToTop = YES.
Since scrollsToTop defaults to YES you have to explicitly set scrollsToTop = NO for the UIScrollView's you do not want to scroll when the user taps the status bar.
Apparently if you have more than one scrolling view (in your case the WebView and TableView) in a view controller, the "tap status bar to scroll to top" is disabled.
http://twitter.com/drance/status/2448035250438144
(Matt Drance is a former iPhoneOS developer evangelist and current iOS development rockstar)
Swift variant of opyh's answer. Upvote him!
func disableScrollsToTopPropertyOnAllSubviewsOf(view: UIView) {
for subview in view.subviews {
if let scrollView = subview as? UIScrollView {
(scrollView as UIScrollView).scrollsToTop = false
}
self.disableScrollsToTopPropertyOnAllSubviewsOf(subview as UIView)
}
}
I just wanted to add to this, if you are using a UITextView, it isn't the subviews that need the scrollsToTop adjusting, it's the actual UITextView, in short you need this code:
[myTextView setScrollsToTop:FALSE];
This should fix the problem (additionally, the 'disableScrollsToTopPropertyOnAllSubviewsOf' function doesn't work on the UITextViews).
Hope this helps someone!
I had this problem with just a tableview in the NIB. Very frustrating until I remembered I was programmatically adding a UITextView as the table view header. scrollsToTop=NO on the UITextView did the trick.
Set scrollsToTop = NO on all scroll views in the view, except for the one you want to scroll to top. That helps the system find the correct scroll view.

MKMapView inside a UIScrollView doesn't move with swipes

I've got a detail view with various labels and such providing information about a place (address, phone, etc.). The information provided is taller than an iPhone screen so they're all in a UIScrollView (itself inside a UIView) that allows you to swipe up and down to see everything.
I also have an MKMapView inside the scrollview. When it's not attached to anything in Interface Builder it moves up and down with the scrollview, as it should, staying in it's correct relative position to the other controls in the scrollview. You can play with the map, zooming and panning it, etc. and it shows your current location by default.
However, as soon as I hook it to an MKMapView variable in IB, the mapview no longer scrolls with the scrollview. Instead it literally just sits in the position it's originally displayed in (bottom of the view, with a little of the map hidden below the bottom of the view) and the scrollview scrolls up and down behind it.
What's happening here? I've tried changing a bunch of the mapview's and scrollview's properties in IB, but it has no effect. The only thing I haven't tried is just creating the mapview entirely in code, but that doesn't seem like an obvious solution.
EDIT: Sorry to everyone about the expired bounty. I got hung up in other areas of the project and couldn't get back here until now. I didn't know it would expire.
FURTHER EDIT: Well, I figured out my problem. For reasons completely unknown to me I had
[self.view addSubview:mapView];
in the viewcontoller's ViewDidLoad. Once it was hooked up then that line of code would (obviously) make the map a subview of my of view, effectively yanking it out of the scrollview.
Stupid mistake, sorry to have wasted your time (and the bounty). I'll delete this question after I think the answerers have had a chance to see the result.
Looking like as you are using the ScrollView,you need to scrolling facility in your DetailView.
Instead of using the ScrollView ,I had an alternative of this ....
You can try your hard luck by using the TableView instead of ScrollView.
Just take all the labels and mapView in a single View and then put that view in the header of the TableView.
like this :
UITableView
--> View
------>All Labels // Inside the singleView
------>MKMApView // At bottom of the View
Still You can play with the map, zooming and panning it, etc. and it will show your current location by default.
Hope this alternative can solve your problem.......
All the Best
If hooking up an outlet in IB is breaking an otherwise working view, you might be able to try this to locate the view at runtime:
- (UIView *) findClass:(Class) aClass inView:(UIView *) aSuperview {
for ( UIView *view in aSuperview.subviews ) {
if ( [view isKindOfClass: aClass] ) break;
if ( ( view = [self findClass: aClass inView: aSuperview] ) ) break;
}
return view;
}
- (void) viewDidLoad {
MkMapView *map = [self findClass: [MkMapView class] inView: self.view];
}
I figured out my problem. For reasons completely unknown to me I had
[self.view addSubview:mapView];
in the viewcontoller's ViewDidLoad. Once it was hooked up then that line of code would (obviously) make the map a subview of my of view, effectively yanking it out of the scrollview.
Do you have setContentSize property set to the content's size in the viewDidLoad method of the UIViewController?

iPhone: calling UIImagePickerController 'moves' the view that calls it

I recently encountered very strange behaviour with the UIImagePickerController. On our main view we have a button that calls the image picker controller. If you hit cancel on the image picker, I simply dismiss the controller, and for some reason or another my main view moves down roughly about the height of the status bar (which is not turned off on the main view, left at default gray). Anyone else encounter this?
Yeah I saw that post, and it didn't fix my issue. What I ended up doing was adding this in my app delegate before adding my first view to the window:
CGRect theRect = mainMenuController.view.frame;
float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
theRect = CGRectOffset(theRect, 0.0,statusBarHeight);
mainMenuController.view.frame = theRect;
This fixed it completely.
This problem is annoying, its been discussed before, check it out Contents of UIScrollView shifted after dismissing ModalViewController