UIScrollview not scrolling what am i doing wrong - uiscrollview

I have an issue where i am adding a UIScrolview to my UIViewController, but my UIScrollview doesnt want to scroll. I want it to work the same way that it works on a UITableViewController.
Here i initialize my UIScrollview
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
scroll.contentSize = CGSizeMake(320, 800);
scroll.backgroundColor = [UIColor redColor];
scroll.showsHorizontalScrollIndicator = YES;
scroll.scrollEnabled = YES;
scroll.userInteractionEnabled=YES;
[self.view addSubview:scroll];
Here i add my content to the scrollview:
UILabel *memberNo = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 100, 20)];
memberNo.text = #"Member No";
memberNo.font = [UIFont boldSystemFontOfSize:16];
memberNo.textColor = [UIColor limeColor];
[scroll addSubview:memberNo];
memberText = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, 300, 30)];
memberText.placeholder = #"Member No";
memberText.borderStyle = UITextBorderStyleRoundedRect;
memberText.text = #"1111111";
[scroll addSubview:memberText];

Try this
set content size more than the screen size and it will scroll whether there is more content or not
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 800)];
HorizontalScroll
scroll.contentSize = CGSizeMake(320, 900);
VerticalScroll
scroll.contentSize = CGSizeMake(340, 800);

As others have said, the scroll view's size (specifically, its bounds.size) must be smaller than its contentSize for it to actually scroll.
You probably want your scroll view to fit inside its superview, so try setting its frame to the superview's bounds:
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:self.view.bounds];

The content is the same size as the scrollview: there is nothing to scroll.
The scrollview needs to contain a view that is bigger than it's own size.

Try setting the contentSize to be larger than the frame. For example:
scroll.contentSize = CGSizeMake(1000, 800);
If that doesn't work, then check where you are setting the content size. Where are you initializing the UIScrollView? Try moving the above line to viewWillAppear or viewDidLayoutSubviews.

frame of the scroll view refers to the size of the scroll view itself whereas content size is the size for the view which is to be shown in the scroll view. If the size of view to be shown in scroll view is having smaller size than the scroll views frame size, then it won't at all scroll, but if you set content size more then scroll view has to scroll to show the 'extra' size more than its own frame size.
e.g. If height of scroll view is 100 and you have set the height factor in content size equal to 150, then these extra 50 pixels will be scrollable.
Good Luck!

Related

Xcode UIImageView in UIScrollView IOS

can you help me? I would like add background image with scrolling, because my background image is too high. How can I do this? I add UIScrollView and into I add UIImageView but it doesnt scroll.
Thank you for replies.
Here is working code:
- (void)viewDidLoad
{
[super viewDidLoad];
//create scrollView and set the frame to the size you want.
//In this example the scrollView frame is the whole ViewController size
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[scrollView setBackgroundColor:[UIColor clearColor]];
//create a UIImage,set the imageName to your image name
UIImage *image = [UIImage imageNamed:#"YourImageName.png"];
//create UIImageView and set imageView size to you image height
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, image.size.height)];
[imageView setImage:image];
//add ImageView to your scrollView
[scrollView addSubview:imageView];
//set content size of you scrollView to the imageView height
[scrollView setContentSize:CGSizeMake(self.view.frame.size.width, imageView.frame.size.height)];
[self.view addSubview:scrollView];
}
If you want the scroll view to scroll, set the content size to be bigger than the scroll view frame.

Resize UICollectionView Height

I'm trying to resize a UICollectionView height by setting it to 0 when the view controller is loaded, then increasing its size with an animation when a button is pressed. I've tried a few different things but it doesn't change in size at all. Here's all the different things I've tried to change its height to 0:
CGRect bounds = [self.collectionView bounds];
[self.collectionView setBounds:CGRectMake(bounds.origin.x,
bounds.origin.y,
bounds.size.width,
bounds.size.height - 100)];
....
CGRect frame = [self.collectionView frame];
[self.collectionView setFrame:CGRectMake(frame.origin.x,
frame.origin.y,
frame.size.width,
frame.size.height - 100)];
....
CGRect frame = self.collectionView.frame;
frame.size.height -= 100;
self.collectionView.frame = frame;
....
self.collectionView.clipsToBounds = YES;
self.collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
If you are using Interface Builder for UICollectionView initialisation, switch off "Use Autolayout" from the File Inspector in the xib file where the your CollectionView is created. Then you can change the height with setFrame or setBounds methods.
You can avoid disabling Autolayout by creating an outlet for the height constraint and then adjusting the constraint's constant in code.
Outlet
#IBOutlet weak var collectionViewVerticalConstraint: NSLayoutConstraint!
Adjustment
collectionViewVerticalConstraint.constant = 0

How to mask a UIScrollView?

I currently have an image mask I want to use to mask a UIScrollView.
The scrollview holds 1 UIImageView.
Here is what I do at the moment in viewdidload:
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);
[Scroll1.layer setMask:mask];
This works to some degree.
It masks the ImageView inside the scrollview but not the scrollview itself.
Is there a way to mask the scrollview's CALayer and not imageview's layer?
You should put the UIScrollView into a UIView, and then apply the mask to the UIView instead of the UIScrollView.
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#"ScrollMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 512, 384);
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 512, 384)];
[containerView.layer setMask:mask];
[containerView addSubview:Scroll1];
[self.view addSubview:containerView];

Disable Subview of UITableview from Scrolling

(xCode 4.3.2, ARC, Storyboard) I have a Master/Detail project and the MasterViewController is a UITableViewController, as usual. It has a navigation bar at the top and a toolbar at the bottom. I've created a custom subview that I load programmatically in viewDidLoad.
It loads fine, and is on top just like I want, but when the user scrolls the tableview, the subview scrolls too. I need the subview to "stick" to the bottom.
Here is the code I used to add the subview:
CGRect totalFrame = CGRectMake(0, 314, 320, 58);
UIView *totalBG = [[UIView alloc] initWithFrame:totalFrame];
totalBG.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"totalBG.png"]];
[self.view addSubview:totalBG];
CGRect totalLabelFrame = CGRectMake(12, 12, 80, 40);
totalLabelBG = [[UILabel alloc] initWithFrame:totalLabelFrame];
totalLabelBG.backgroundColor = [UIColor clearColor];
totalLabelBG.text = #"Total:";
[totalLabelBG setFont:[UIFont boldSystemFontOfSize:22]];
totalLabelBG.userInteractionEnabled = NO;
totalLabelBG.textColor = [UIColor whiteColor];
totalLabelBG.shadowColor = [UIColor blackColor];
totalLabelBG.shadowOffset = CGSizeMake(0, 1);
[totalBG addSubview:totalLabelBG];
I tried setting userInteractionEnabled to NO in viewDidLoad and detecting a
touch, and setting the totalLabelBG.center property, and neither worked.
I've also read through a lot of threads on disabling scrolling for webviews, but found nothing relevant.
I found another SOF question with a response that may or may not work, but the user did not explain the answer. "The trick is to adjust the frame of the "non-scrollable" subview inside -layoutSubviews."
I achieved the desired result by adding the subview to the UINavigationController container like this:
[self.navigationController.view addSubview:totalBG];
Like in the answer above. Just add that particular subview to any subview outside on the UIScrollView instance. If its not suppose to scroll then it doesn't make sense to put it inside the scroll view to begin with. Instead make the scroll view only take up the height up to the top of the totals subview

UIScrollView viewForZoomingInScrollView when pagingEnabled with multiple UIImageViews in it

I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.

Resources