Can't override "hitTest" method for UIScrollView - iphone

I've created a scroll view, width less than the screen width and set clipsToBounds=YES. As the UIScrollView doesn't scroll from anywhere outside the range of its frame, I put the UIScrollview inside a UIView and tried to override the "(UIView *)hitTest:WithEvent:" method. But it always shows warning pointInside method not found (or something like that) in the if ([self pointInside:point withEvent:event]) line and it doesn't work. What did I do wrong? Thanks in advance...

You have to change the ScrollView's contentSize in order to allow the ScrollView to scroll. It will only scroll it the contentSize.x is bigger than scrollview.size.x and/or contentSize.y bigger than scrollview.size.y.

Answering my own question, the problem is I didn't create the UIVIew subclass to override the (UIView *)hitTest:WithEvent: while the UIScrollView was in a subview of self.view. So, it is important to check which view inherits the scroll view and write overridden method in the parent view class.

Related

Storyboard UIScrollView contentSize?

I feel like I have touched on every single possible cause for stopping this, but I have a UIScrollView in my Storyboard hooked up with an outlet and in the viewDidLoad I set the contentSize so that I can scroll (yes bigger than my frame size)!
However, whatever I change, I just can't scroll! I have a couple of textfields in my scrollview and bouncing enabled so I can see that when testing its moves up and down with my subviews in it but whatever I set the contentSize to I just can't scroll.
Anything I might be missing/should check? Is this a known issue with UIScrollView being used in a storyboard?
Whats even stranger is, I can do something like this:
[scrollView setBackgroundColor:[UIColor blueColor]]; and I have a blue scroll view! But setting content size fails.
Edit
My only code (otherwise scrollview is just dropped into storyboard view controller):
-(void)viewDidAppear:(BOOL)animated
{
[scrollView setContentSize:CGSizeMake(320, 640)];
}
Logged frame, comes out as expected:
width: 320.00
height: 504.00
Edit 2
Turns out that removing any subviews of the scroll view in my storyboard lets it scroll just fine. If I add any subview to it at all via the storyboard, even a blank brand new UIButton it just won't apply the contentSize/allow scrolling.
use ViewDidLayoutSubview
- (void)viewDidLayoutSubviews
{
[_scrollView setContentSize:CGSizeMake(320, 500)];
}
UIViewController's method invoke sequence is as below
awakeFromNib
viewDidLoad
viewWillAppear
viewWillLayoutSubviews
viewDidLayoutSubviews
viewDidAppear
viewDidLoad is not a good place to put code that relies on frame sizes of IB objects. If you log the contentSize of your scroll view in viewDidLoad, you will see that it's (0,0). Move the code (where you set the content size) to viewDidAppear, and it will work properly.
Check these
User Interaction enabled
Outlet connected
Included contentsize greater than bounds
scrolling Enabled
eg
scrollView.contentSize = CGSizeMake(320, 640);
My storyboard looks like this for scrollview [working]
I had exactly the same line of code in viewDidAppear and it did not work
Moved it to viewDidLayoutSubviews and it worked correctly.
[scrollView setContentSize:CGSizeMake(320, 500)];
Thanks trick14 for the answer.
The issue is most probably with Auto Layout. UIScrollView needs special attention when using AutoLayout.
Quick-fix - bind one of the scroll's subviews to the top AND bottom space of it's superview (the scroll view).
Long story:
Questions on SO:
UIScrollView not scrolling regardless of large contentSize,
UIScrollView will not scroll, even after content size set,
UIScrollView doesn't use autolayout constraints
Apple's Documentation:
https://developer.apple.com/library/ios/technotes/tn2154/_index.html
Trip14's answer worked for me. In swift I coded it as:
override func viewDidLayoutSubviews() {
(self.view as! UIScrollView).contentSize = CGSizeMake(600, 600)
}
This seems to be a similar issue. Other Story
It might be an issue with auto layout or constraints in the storyboard.
the best way with the storyboard.:

UIScrollView as a subview does not scroll

When I set my UIScrollView to be the main view within Interface Builder, the scrolling works as expected. However, when I add the UIScrollView as a subview of UIView the scroll does not work.
So it seems my trouble begins when the UIScrollView is added as a subview to UIView.
The "User Interaction Enabled" it true for both view.
I though perhaps I might have to do something with in touchesBegan something lake passing the touches to the UIScrollView, but have not had much luck with that.
Has anyone seen this before?
scrollView.delegate = scrollView; //or nil may work
It's always good to show a complete working code snippet:
// in viewDidLoad:
//UIScrollview *myScrollView;
UIView_descendent *contentView;
// scrollview won't scroll unless content size explicitly set
[myScrollView setContentSize:contentView.frame.size];
I have not found a way to set contentSize in IB.

Reposition UIButtons after zoom

I have a scrollview with an imageview on it. I am able to zoom the image view placed inside the scrollview. On top of it I have some uibuttons, which do not reposition after zoom.
How do I reposition UIButtons after zoom?
Please help
Thanks
One way would be to implement the scrollViewDidZoom: delegate method of your scroll view delegate, determine the currently visible rectangle using contentOffset and contentSize properties of your UIScrollView, compute the new location of your buttons relative to the visible rectangle of the scroll view, and reposition your buttons into that rectangle by setting their frame property as desired.
As Fisk mentioned, the UIScrollViewDelegate should help. One of the delegate methods is:
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
If you implement the UIScrollview delegate this method should get called when your scrollview is done zooming. So in this method you can reposition your button after the zoom:
[yourButton1 setCenter:CGPointMake(newX,newY)];
Good luck.
Your question is a bit vague. However, whatever it is that you are doing, I believe UIScrollViewDelegate can help you.
This is a delegate for the scroll view that lets you know when it is scrolling. I never got where the buttons are supposed to be, but with this delegate I guess you can simply place them wherever they need to be.

Problem with UIScrollView inside another UIView

I have this UIScrollView inside another UIView, not occuping the entire area (all this is initialized via nib file). I added some content to the scroll view, but whenever I scroll it, the UIScrollView content area moves outside the ScrollView frame, over the UIView area not designated for displaying it.
Shouldn't it remain inside its frame even when I scroll it?
It may be that the clipsToBounds property of your UIScrollView is set to NO. Check the setting in IB, or you can set it in code like so:
scrollView.clipsToBounds = YES;
Failing that, double check that your UIScrollView has exactly the bounds you think it does. Is there any autoresizing flag stuff going on that might be changing the scroll view's bounds?
try to set the clipsToBounds to YES, it may be that:
yourViewController.clipsToBounds = YES;
if not... fbreto is right, post your code...
Make sure you have setContentSize. And also set the frame size of the uiviews inside the uiscrollview correctly when you add them.
Here is a sample code of it http://developer.apple.com/library/ios/#samplecode/Scrolling/Introduction/Intro.html

Anchor a UIView

I have a UITableViewController inside of a UINavigationController.
I want to have a UIView appear over the top of the table view but not susceptible to the scrolling of the table view.
I.e. if the table view is scrolled, the UIView should remain in the same position relative to the screen, rather than relative to the table view. It should appear anchored in a certain position.
What is the best way to achieve this?
EDIT: To clarify, the view should float transparently over the top of the table view.
Many thanks!
I also wanted to have a floating UIView over my tableView.
So, within my RootViewController (which is a UITableViewController), this worked for me
- (void)viewDidLoad {
/* mylabel is a UILabel set in this class */
[self.mylabel setUserInteractionEnabled:NO];
/* navigationController comes from higher up in the navigation chain */
[self.navigationController.view addSubview:self.mylabel];
}
Similar to what Peter said, create a UIView that will contain both the TableView and the subclassed UIView. Such as:
UIView *view = [[UIView alloc] initWithFrame:frame]; // Define frame as you like
[view addSubview:myTableView]; // This is the reference to your tableView
[view addSubview:myAnchoredView]; // This is the reference to your UIView "floating" subclass
You will also need to turn off user interaction for your floating view. I don't know if this will specifically pass the touches to the underlying UIView's or not though:
[myAnchoredView setUserInteractionEnabled:NO];
If this is blocking touches to your tableView, you may need to pass the reference to your tableView to the anchored view at initialization, then pass the touch events along. You can do this by overriding the touch response methods in UIResponder. (If there is a better way, someone please speak up.)
Do you mean the anchored view should appear transparent over the UITableView, or just above, i.e. anchored view uses top 20% of the available space, table view uses the rest?
In any case, create a UIView containing the anchored view and the table view. If you want the anchored view transparent over the table view, it's a bit tricky, because to scroll the table view, touches have to pass through the anchored view.
Add the surrounding view's view controller to the navigation controller instead of just the tableview.
I investigated how UIScrollView keeps its scrollIndicator above the UIScrollView's content and yet unmoving by examining a UIScrollView in the debugger.
The scrollIndicators are UIImageViews. And you can see they are direct descendants of the UIScrollView itself. You can also see that any scrolled content is also a direct descendent. So how is it that the scroll indicators don't move?
I tried updating the position of my static content constantly in - (void)scrollViewDidScroll:(UIScrollView *)scrollView this, surprisingly, works. I'm not sure if it is how UIScrollView itself does it, but without some private magic, it must be something like this.