Why am I unable to zoom my UIImageView embedded in a UIScrollView? - ios

I literally have the most basic of code and I'm so frazzled as to why it won't zoom:
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
self.scrollView.delegate = self;
self.scrollView.contentSize = self.imageView.bounds.size;
self.scrollView.minimumZoomScale = 0.5;
self.scrollView.maximumZoomScale = 10.0;
[self.view addSubview:self.scrollView];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"community1.jpeg"]];
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:self.imageView];
}
In the simulator I try to zoom and nothing at all happens. It's a rather large image if that's relevant.

Did you implement -viewForZoomingInScrollView: in your scroll view's delegate? If not, you need to do that, returning the view you want to actually zoom when you pinch.

Related

UIScrollView doesn't scroll in the bottom part of screen

I'm using a UIScrollView with 1 image and 2 tableview inside. The scroll works correctly if I tap on the center and on the top of the screen but doesn't works if I try to scroll tapping on the bottom of the screen. Here the code that I used.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
self.scrollView.scrollEnabled = YES;
self.scrollView.delegate = self;
[self addSubview:self.scrollView];
self.theImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, lenghtImage, hightImage)];
self.theImage.image = [UIImage imageNamed:#"theImage.png"];
[self.scrollView addSubview:self.theImage];
self.tableA = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosA, self.bounds.size.width, heightA];
self.tableA.delegate = self;
self.tableA.dataSource = self;
self.tableA.scrollEnabled = NO;
[self.scrollView addSubview:self.tableA];
self.tableB = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosB, self.bounds.size.width, hightB)];
self.tableB.delegate = self;
self.tableB.dataSource = self;
self.tableB.scrollEnabled = NO;
[self.scrollView addSubview:self.tableB];
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width,heightImage+heightTableA+heightTableB);
Your scroll view contentSize should be equal or greater than its total subview size.
You calculate contentSize height by adding all of its subview height and also their y coordinate.
so it should be:
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, hightImage + yPosA + heightA + yPosB + hightB);

UscrollView: bad scroll on iPhone 6 plus

I have a problem. I use a UIScrollView with 1 image and 2 tableview inside.
The scroll works correctly on an iPhone 5 but on an iPhone 6Plus the scroll has a problem not so easy to explain. If I tap in light way the scroll is ok, but if I tap in a little long way the screen the scroll doesn't works. Here the code that I used.
self.scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
self.scrollView.scrollEnabled = YES;
self.scrollView.delegate = self;
[self addSubview:self.scrollView];
self.theImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, lenghtImage, hightImage)];
self.theImage.image = [UIImage imageNamed:#"theImage.png"];
[self.scrollView addSubview:self.theImage];
self.tableA = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosA, self.bounds.size.width, heightA];
self.tableA.delegate = self;
self.tableA.dataSource = self;
self.tableA.scrollEnabled = NO;
[self.scrollView addSubview:self.tableA];
self.tableB = [[UITableView alloc] initWithFrame:CGRectMake(0, yPosB, self.bounds.size.width, hightB)];
self.tableB.delegate = self;
self.tableB.dataSource = self;
self.tableB.scrollEnabled = NO;
[self.scrollView addSubview:self.tableB];
self.scrollView.contentSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, heightA + heightB);
can your tableviews be eating the touches? have you tried with tableView.userInteractionEnabled = false to see if it works ?

How to zoom a UIImageView within a UIScrollView?

I have a UIImageView within a UIScrollView and I can vertically scroll, but not horizontal scroll because of how I set the content size. That's exactly how I want.
However, I can't seem to zoom in and out. I set the minimum and maximum zoom scale, but it's not working.
Can someone tell me why?
Thanks.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.scrollView.scrollEnabled = YES;
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
tempImageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = tempImageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.zoomScale = 1.0;
self.scrollView.delegate = self;
[self.scrollView addSubview:tempImageView];
}
You have to implement the following delegate method for zooming:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
and make sure that you add self.imageView to your self.scrollView instead.
It should look like this:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"] ];
self.imageView.frame = CGRectMake(0, 0, self.scrollView.frame.size.width, self.view.frame.size.height);
self.scrollView.backgroundColor = [UIColor blackColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = self.imageView.image.size.width / self.scrollView.frame.size.width;
self.scrollView.delegate = self;
[self.scrollView addSubview:self.imageView];
}
To get zooming to work, you need to provide a delegate method that returns the view you wish to get zoomed:
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return self.imageView
}
You will also need to hold on assign tempImageView to a property so that you can refer to it here. If you don't want to hang on to it in a property, you will need some other way of identifying the view, e.g. scrollView.subviews[0] if it is the only subview.
Try this Try this
[scroll setUserInteractionEnabled:YES];
And your Maximum zoom scale is strange. Try with 2,0 to test.

Arrange UIscrollView to back

Maybe a huge noob question but i cant seem to figure it out, even after some time googling i cant find out to arrange a scrollview underneeth a navigation bar.
My code:
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;
}
Try inserting the view behind the superview or try sendSubviewToBack.
[[self.view superview] insertSubview:scroll belowSubview:self.view];
or
[self.view sendSubviewToBack:scroll];
It sounds like you are just wanting to add the scroll view to the self.view though. Do as the other answer suggest and create the scrollView from the self.view frame then add it to your self.view.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame]; // Thilina
[self.view addSubview:scroll];
Since you are passing the frame of [UIScreen mainScreen] at the time of allocating the UIScrollView , the size of UIScrollView is overlapping the UINavigation . Replace it with
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
also rather than replacing the self.view with the scrollView , try to add the scrollView as subview on self.view
[self.view addSubview:scrollView];
Do the following task in the viewDidLoad
Hope it will help you.
You can simply use the self.view's frame when creating the ScrollView. This will solve your problem.
- (void)loadView {
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
scroll.backgroundColor = [UIColor blackColor];
scroll.delegate = self;
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Plattegrond DeFabrique schematisch.png"]];
scroll.contentSize = image.frame.size;
[scroll addSubview:image];
scroll.minimumZoomScale = scroll.frame.size.width / image.frame.size.width;
scroll.maximumZoomScale = 2.0;
[scroll setZoomScale:scroll.minimumZoomScale];
[self.view addSubView:scroll]; //MarkM
}

UIScrollView programmatically Scrolling

I have a UITextView inside a UIScrollView that needs to automatically scroll once the user starts editing it. This is because the keyboard will cover the textview.
Here is the code -
In viewDidLoad:
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, segmentedControl.frame.origin.x + self.segmentedControl.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, emailField.frame.origin.y + 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
and here's the delegate method implementation -
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[scrollView setContentOffset:point animated:YES];
}
However, nothing happens.
your are doing fine but use feedBackformView instead of scrollView,while setting content offset in textViewDidBeginEditing:method, just have a look on below code
feedBackformView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 50, 320, 200)];
feedBackformView.backgroundColor = [UIColor whiteColor];
feedBackformView.scrollEnabled = YES;
feedBackformView.delegate = self;
feedBackformView.userInteractionEnabled = YES;
feedBackformView.showsVerticalScrollIndicator = YES;
feedBackformView.contentSize = CGSizeMake(320, 700);
commentsView = [[UITextView alloc] initWithFrame:CGRectMake(5, 40, 250, 150)];
commentsView.delegate = self;
commentsView.layer.borderWidth = 2.0f;
commentsView.layer.cornerRadius = 5;
[feedBackformView addSubview:commentsView];
[self.view addSubview:feedBackformView];
in textview DelegateMethod,
-(void)textViewDidBeginEditing:(UITextView *)textView{
CGPoint point = textView.frame.origin;
[feedBackformView setContentOffset:point animated:YES];
}
hope it will hepls you...
You can use scrollView scrollRectToVisible:animated or the setContentOffset:animated methods described here:
UIScrollView Class Reference
Keep in mind that UITextView comes with its own scroll view. so you may not need to nest it inside another scrollview. But you may need to if your app is that interesting.
Is your point of scrolling already at a visible point?
-(void)textViewDidBeginEditing:(UITextView *)textView{
// try this instead
CGPoint point = CGPointMake(textView.frame.origin.x,
textView.frame.origin.y + textView.frame.size.height);
[scrollView setContentOffset:point animated:YES];
// What does this produce?
NSLog(#"%# %#", NSStringFromCGPoint(scrollView.contentOffset),
NSStringFromCGRect(textView.frame));
}

Resources