Prevent scrolling uiscrollview, then a specific point

I have a UIScrollView that has subviews that create content about 7000 wide. Now I want the UIScrollView to not scroll further than 2000.

I tried:

[scrollView setContentSize:CGSizeMake(768.0, 2000.0)]; [scrollView setClipsToBounds:YES]; 

But I can still scroll to the end, how can I prevent this?

+4
source share
7 answers

The webView change its scrollView contentSize after the page loads.

In your init, set self as a webView delegate:

 [webView setDelegate: self]; 

And add this method:

 - (void)webViewDidFinishLoad: (UIWebView *)webView { [webView.scrollView setContentSize: CGSizeMake(768.0, 2000.0)]; } 

In my test case, it works fine.


UPDATED:

Calling javascript methods sometimes does not start webViewDidFinishLoad . Thus, monitoring the key values ​​will be the most universal method.

In init:

 [webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil]; 

And somewhere this method:

 - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { UIScrollView * scrlView = (UIScrollView *) object; if ([keyPath isEqual:@"contentSize"]) { NSLog(@"New contentSize: %fx %f",scrlView.contentSize.width,scrlView.contentSize.height); if(scrlView.contentSize.width!=768.0||scrlView.contentSize.height!=2000.0) [scrlView setContentSize:CGSizeMake(768.0, 2000.0)]; } } 

Remember to remove the observer in the case of dealloc:

 [webView.scrollView removeObserver:self forKeyPath:@"contentSize"]; 
+7
source

You can set the content offset to 2000 if it is greater than 2000, so it does not go beyond.

Deploy this method and set it to the delegate of this scrollview

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { if (scrollView.contentOffset.x > 2000) { CGPoint offset = scrollView.contentOffset; offset.x = 2000; [scrollView setContentOffset:offset animated:YES]; } } 

Edit: I just tried this and it works great. Maybe check why setContentSize: doesn't work? Another approach, make a view with a height of 2000, place its contents in it and add this view as a scroll pod.

 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 600, 7000)]; label.numberOfLines = 60; label.font = [UIFont systemFontOfSize:100]; UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame]; NSMutableString *str = [[NSMutableString alloc] initWithCapacity:300]; for (int i = 0; i < 60; i++) [str appendFormat:@"%d\n", i]; label.text = str; [scrollView addSubview:label]; [scrollView setContentSize:CGSizeMake(600, 2000)]; [self.view addSubview:scrollView]; 
+1
source

I believe your code above should work fine. setContentSize will prevent contentOffset from increasing in content. You may need to start by setting the contentOffset content itself (once) if the user has already scrolled outside this area. (For example, try resetting it (0, 0).)

Also, do you enable scaling? If you have zoom! = 1, the size of the content may not be what you think. Before resizing content, try setting the scale to 1.

0
source

Ok So, the first step, your view controller must implement UISCrollViewDelegate, and you must set your controller as a delegate for your UIScrollView.

Then we implement this delegate method in your controller:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { static float stopx = 2000.0f; if (scrollView.contentOffset.x >= stopx) { CGPoint stop = scrollView.contentOffset; stop.x = stopx; [scrollView setContentOffset:stop animated:NO]; } } 

At any time when you get a pass 2000 / stop value, scrollview should be returned

0
source

As others said, do uiscrollviewdelegate

implement the following methods:

 - (void)scrollViewDidScroll:(UIScrollView *)scrollView - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView 

and hopefully this fixes the jump problem you encountered

Note that add similar code to other messages in these methods, for example

 static float stopx = 2000.0f; if (scrollView.contentOffset.x >= stopx) { CGPoint stop = scrollView.contentOffset; stop.x = stopx; [scrollView setContentOffset:stop animated:NO]; } 

Performance may be compromised at first, if so, start extracting specific delegate methods until you get the desired behavior.

0
source

If I do not understand something in your question, it is straightforward. Add this to your view controller containing the web view.

 - (void)viewDidLoad { // other stuff self.webView.delegate = self; self.webView.scrollView.bounces = YES; // load your webView here } - (void)webViewDidFinishLoad:(UIWebView *)webView { CGSize contentSize = self.webView.scrollView.contentSize; CGFloat maxContentWidth = contentSize.width; if (maxContentWidth > 2000) { maxContentWidth = 2000; } self.webView.scrollView.contentSize = CGSizeMake(maxContentWidth, contentSize.height); } 
0
source

It is hard to give a specific answer without seeing your html, but I had a similar problem before, and I think that this is actually your UIWebview, which actually scrolls, not scroll. somewhere on the page in the UIWebview html you will most likely have oversized content. Web browsing naturally wants to scroll. In my case, it was a long URL in a paragraph of text. I also saw how this happens vertically.

You need to find the container element for your oversized html and set the overflow to hide in css. Maybe it's a body tag or an html tag in html or you need to wrap the content.

t

 <div style="overflow:hidden"> Overflowing text </div> 

If you don’t have to play with the following tag

Or set the width of the container div: 100% and height: 100%;

0
source

Source: https://habr.com/ru/post/1381663/


All Articles