Picker in a ScrollView doesn't scroll - iphone

I have a UIScrollView, inside it, a small UIView containing a UIPickerView.
I'm able to scroll the view up and down but whenever I try to scroll the picker, it scrolls the view instead. I can change the picker's value by clicking it but not scrolling.
Any ideas?
EDIT: I just tried scrollView.canCancelContentTouches = NO; but it only works when I first Touch and then Drag, as in the TouchesBegan event. The scrollView still scrolls if I touch-move the Picker as in the TouchesMoved event.
What can I do to give my Picker its natural behavior in a ScrollView?

You've already got canCancelContentTouches set to NO, but try setting delaysContentTouches to NO as well.

Related

iOS UIScrollView in UIWindow

I currently am making an iOS project in which I have a UIScrollView as a direct subview of a UIWindow (using [window addSubview:scrollView];). The window's frame and its content are being set properly, and the contentSize is set to be bigger than the window's frame. When I try to scroll the UIScrollView, it doesn't scroll at all. Both scrollEnabled and pagingEnabled are set to YES, but the scrollview doesn't scroll, which leads me to believe that the touch/scroll events are not even being received by the scroll view. The window has a UITapGestureRecognizer added to it if it makes any difference. Do I need to somehow forward the swipe events to the UIScrollView, or is there a different reason that it's not scrolling?
EDIT: Here's some code.
float count=ceil([self.msgArray count]/2); //msgArray has length of 3+, NSLog()'d and confirmed.
float contentHeight=97.5 * count;
[dataScrollView setContentSize:CGSizeMake(320,contentHeight)];
dataScrollView.userInteractionEnabled=YES;
dataScrollView.pagingEnabled=YES;
dataScrollView.scrollEnabled=YES;
dataScrollView.clipsToBounds=YES; //Have also tried with this set to NO, or not set at all.
//Add subviews to dataScrollView.
EDIT: Here's some more info.
contentHeight is 195.00 when logged. I've removed the delegate method and I am back to using direct subviews of the scroll view. The window's height is 97.50.
EDIT: I've also removed the UITapGestureRecognizer from the UIWindow, but the scrollview still doesn't scroll.
Ah, the UITapGestureRecognizer! This might be a bug that took me hours to figure out in my own project. Is its cancelsTouchesInView property set to NO like it should be? (YES is the default... It can really throw you off if you're not expecting it.)
Just log the scrollview bounds width/height. The content height you are setting should be greater than the scrollview height. If its more then the scroll view automatically enables its scrolling.

How do I stop a UIScrollView from automatically scrolling when a UITextField is touched out of the scrollview's frame?

By default, if a textfield is a child of a scrollview and is touched when it is partially outside of the frame of the parent scrollview, the scrollview scrolls up a little. For example, let's say we have a scroll view with a frame height of 200, but a contentSize height of 260 and we put a text field at position 220. Now I scroll up and position the textfield half inside of the scrollview's frame and let the other half get cut off. If I touch this textfield, I notice that the scrollview automatically scrolls up by a couple pixels before the keyboard comes up. This is problematic because I already have my own scrolling code, so when this happens, it ends up scrolling twice as far as I want it to. Is there any way to remove this default behavior?
It sounds like your scrollview is getting moved in response to the keyboard showing instead of the text field being tapped. Take a look at Apple's Managing the Keyboard documentation; it has a helpful section on scrollviews.
A similar question already has been answered here:
https://stackoverflow.com/a/5673026/550177
You can also try to reset the scrollview's contentOffset when the textfield becomes first responder. Implemement the text field delegate like this:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.scrollView setContentOffset:_scrollView.contentOffset animated:NO];
}
This code removes that automatic animation when the textfield is selected. I've tested with iOS 6.1 in the simulator.

UITableView Scroll w/ keyboardWillShow

I have uitableview scrolling upwards when keyboardWillShow event occurs. tableview is 320x148 and placed at the bottom of the uiview. When a user selects the first uitextfield, nsnotifcation is fired and moves the textfield accordingly.
My problem is, I have a label and logo in the background (uiview) that i want to see move upwards with the "event". I can do this with an animation, but it's not the desired effect.
My guess is to somehow make uiview move with uitableview somehow. Hope that makes sense.
Humm, i think you could put your UITableView inside a UIScrollView. When the UITableView scrolls, you could do the same to the UIScrollView behind..

UIScrollView freezes after using SetContentOffset or scrollRectToVisible

I have a UIScrollView with paging enabled and I want to be able to jump to pages further down the line rather then swiping through each one. I attempted to use both setContentOffset and scrollRectToVisible. They both scroll the view to the correct point but after scrolling animated or not the scrollview becomes frozen and unresponsive to any touches. I tried setting it to the first responder but it changed nothing. I have a button outside of the scroll view and it still functions fine after the setContentOffset. There are also buttons inside the scrollview and not only will the scrollview not respond to touch for dragging but the buttons will not recognize the touches either.
[mainScroll setContentOffset:CGPointMake(mainScroll.frame.size.width*4, 0.0) animated:YES];
tldr; I can scroll through the view fine, through all the pages but when I try and call a setContentOffset or a scrollRectToVisible I get 'frozen' after the move.
I figured it out, stupid mistake.
In my scrollViewDidScroll I had:
scrollView.userInteractionEnabled=NO;
and in my scrollViewDidEndDecelerating
scrollView.userInteractionEnabled=YES;
and in scrollViewDidEndDragging:willDecelerate
if(!decelerate){scrollView.userInteractionEnabled=YES;}
I had this to prevent button presses and any random things during a page transition. But when setContentOffset is called it only causes scrollViewDidScroll to be called and neither of the other two, so the UserInteraction was never set back to Enabled, but only when using setContentOffset.
Simple fix.

Prevent subview from scrolling in a UIScrollView

I have a UIScrollView subclass with a certain subview I'd like to prevent from scrolling (while all the other subviews scroll as normal).
The closest example to this I can think of is UITableView's "index strip" on the right side (look in the Contacts app to see an example). I am guessing this is a subview of the table (scrollview) but it does not move as the user scrolls.
I can't seem to make my subview stay put! How can I accomplish this?
The trick is to adjust the frame of the "non-scrollable" subview inside -layoutSubviews.
Add the view that you want not to move as a sibling view of the scroll view on top of the scroll view instead of as a subview.
You can set it's property called userInteractionEnabled to NO