UIScrollView scroll functionality not working? - ios

I am a newbie to IOS app development. I am trying to implement a scrollview. I am not able to scroll at all.
Below is my view implementation.
I created a scroll view and added small blocks with different colors in it.
I added one block out of current screen bounds so that I can scroll down the UIScrollView and view it.
#implementation CustomView
- (instancetype)init
{
self = [super init];
if (self) {
CGRect screenRect = CGRectMake(70, 100, 250, 800);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
scrollView.scrollEnabled = YES;
[scrollView setBackgroundColor:[UIColor yellowColor]];
[scrollView setAlpha:0.2];
[self addSubview:scrollView];
CGRect temp = CGRectMake(100, 150, 10, 10);
UIView *firstView = [[UIView alloc] initWithFrame:temp];
[firstView setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:firstView];
temp = CGRectMake(150, 200, 10, 10);
UIView *secondView = [[UIView alloc] initWithFrame:temp];
[secondView setBackgroundColor:[UIColor greenColor]];
[scrollView addSubview:secondView];
temp = CGRectMake(200, 750, 10, 10);
UIView *thirdView = [[UIView alloc] initWithFrame:temp];
[thirdView setBackgroundColor:[UIColor orangeColor]];
[scrollView addSubview:thirdView];
scrollView.contentSize = screenRect.size;
}
return self;
}
#end
I am using the view I created before in my view controller. I am adding it to my view controller's view.
#interface ViewController ()
#property (nonatomic, readonly) CustomView *currentView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_currentView = [[CustomView alloc] init];
[self.view addSubview:_currentView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I am not able to scroll at all with this implementation. am i missing something ?
Can someone please explain what is wrong with my code.
Thank you

When you add a scrollview to the screen it should be the size which you want to display on the screen. It will be displayed on the screen simply as an UIView only when you look at it.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame: CGRectMake(20, 20, 250, 250)];
The above initialisation will give you a scroll view of the size 250X250 placed at the position (20,20).
For scrolling you need to define the content size of the scroll view.
This content size should be bigger than your scrollview size otherwise you will not be able to differentiate it. The size of the contentsize should be decided based on the content you are going to put inside it.
For example if the three views you are adding inside the scrollview occupy a total width of 500 and a height of 300 then your content size should be equal to that only.
[scrollView setContentSize:CGSizeMake(500, 300)];
This will allow you to scroll inside the scrollview.
Also the content you are adding to you scrollview should be added considering the top left corner of the scrollView as the (0,0) location.
So if you want to display something on the top left corner of your scrollView then the frame dimensions of that particular view should be like this
CGRectMake(0,0,30,30);
This will add a view of the size 30X30 on the top left corner of the scrollView.
Hope it helps.

Your scroll view doesn't scroll because its frame is as large as its content size. Add this line in the view controller after before adding the scrollview as subview:
_currentView.frame = self.view.bounds ;

The contentSize property should not be the size of the screen, it needs to be larger if you want scrolling to work. You should calculate the size of the content within the scrollView, for example the combined height of all the subviews and the spaces between them or maybe the MaxY of the bottom view.
Also, try using a UITableView (or a UIStackView in iOS 9) to accomplish the same behavior without doing any math

First of all, what you are trying to do should be done using UITableView. I am not sure why are you trying to do it this way.
Anyhow, did you try to set also the contentSize property of UIScrollView?
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/#//apple_ref/occ/instp/UIScrollView/contentSize

Related

UIScrollView scrolling automatically by 64 points

I'm adding a UIScrollView to a UIViewControllers view. For some reason, between adding the scroll view to the view and it getting displayed, the contentOffset is set to {0, -64}, 64 being the status bar's 20 plus the navigation bar's 44 points (I guess). Below is some code that reproduces the issue, and an image.
How do I prevent iOS from setting the contentOffset?
- (void)viewDidLoad
{
[super viewDidLoad];
_scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 100, 100, 100)];
_scroll.backgroundColor = [UIColor greenColor];
_scroll.delegate = self;
UIView *red = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
red.backgroundColor = [UIColor redColor];
[_scroll addSubview:red];
[self.view addSubview:_scroll];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
// outputs {0, -64}
NSLog(#"%#", NSStringFromCGPoint(_scroll.contentOffset));
}
Set automaticallyAdjustsScrollViewInsets on your view controller to NO, otherwise it'll adjust insets on the first subview of it's root view that happens to be of UIScrollView class.
More on this in iOS 7 Transition Guide.
For iOS 11+, set UIScrollView's contentInsetAdjustmentBehavior = .never to prevent the system from adjusting contentOffset automatically when it's being added to another view

ios ScrollView empty blank area does not scroll

I have created a scrollview programmatically in ios.
I am displaying 2-3 images on that scrollview and adding images vertically one below other.
When I touch on the image and scroll vertically it works. But if I touch the empty space between the images and try to scroll it does not work. Scrollview does not respond to that.
Here is the relevant code :
UIImageView* imageView = [[UIImageView alloc] initWithFrame:imageRect];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self didAddSubview:imageView];
.....
......
[scrollView setScrollEnabled:YES];
[[self view] addSubview:scrollView];
Any inputs will be helpful.
Thanks.
You can set the content size of scrollView use this code hope it will help you :-
- (void) viewDidLoad
{
[super viewDidLoad];
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
[scrollView setScrollEnabled:YES];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 250, 200)];
[imageView setImage:[UIImage imageNamed:#"background.png"]];
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
scrollView.contentSize = CGSizeMake(320, 600);
}
Set,
[scrollView setFrame:size];
[scrollView setContentSize:contentsize];
If it will not scroll vertically or horizontally then you have to change content size it should be greater then current size.
This question is old but I was having the exact same issue with my scrollview, and this was the only page I found about this all over the internet.
I was using a content view inside scroll view, and I just set a background color on my contentView and it worked.
Content size was being set automatically throw auto layout, so no need to set it manually.

Adding UIScrollView to a UIViewController

I have a UIViewController, and I want to add a UIScrollView to it (enable scroll support), is it possible?
I know it is possible, if you have a UIScrollView to add a UIViewController to it, but I'm interested also if reverse was true, if I cann add a UIScrollView to an existing UIViewController, such that I get scrolling feature.
Edit
I think I have found an answer: Adding a UIViewController to UIScrollView
An UIViewController has a view property. So, you can add a UIScrollView to its view. In other words, you can add the scroll view to the view hierarchy.
This is can achieved by code or through XIB. In addition, you can register the view controller as the delegate for your scroll view. In this way, you can implement methods for performing different functionalities. See UIScrollViewDelegate protocol.
// create the scroll view, for example in viewDidLoad method
// and add it as a subview for the controller view
[self.view addSubview:yourScrollView];
You could also override loadView method for UIViewController class and set the scroll view as the main view for the controller you are considering.
Edit
I created a little sample for you. Here, you have a scroll view as a child of the view of a UIViewController. The scroll view has two views as children: view1 (blue color) and view2 (green color).
Here, I suppose you can scroll in only one direction: horizontally or vertically. In the following, if you scroll horizontally, you can see that the scroll view works as expected.
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
scrollView.backgroundColor = [UIColor redColor];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width * 2, self.view.bounds.size.height);
[self.view addSubview:scrollView];
float width = 50;
float height = 50;
float xPos = 10;
float yPos = 10;
UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(xPos, yPos, width, height)];
view1.backgroundColor = [UIColor blueColor];
[scrollView addSubview:view1];
UIView* view2 = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width + xPos, yPos, width, height)];
view2.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view2];
}
If you need to scroll only vertically you can change as follows:
scrollView.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height * 2);
Obviously, you need to rearrange the position of view1 and view2.
P.S. Here I'm using ARC. If you don't use ARC, you need to explicitly release alloc-init objects.

UIScrollView won't stay in place after user finishes scrolling

Within my UIView, I have a UIScrollView which fills the first view, so than when the content is bigger than the iPhone screen size, the user can scroll the page down. It works well, but when the user finishes the scroll movement - i.e. removes his fingers, the page snaps back into it's original position. Obviously that is not what I want, how can it be avoided?
Here is the relevant code in the UIView class which declares and uses the UIScrollView class.
#implementation TestView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
}
CGRect scrollViewFrame = CGRectMake(0, 0, 320, 460);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame];
scrollView.canCancelContentTouches=NO;
[self addSubview:scrollView];
CGSize scrollViewContentSize = CGSizeMake(320, 500);
[scrollView setContentSize:scrollViewContentSize];
CGRect rectForBigRedSquare = CGRectMake(50, 50, 200, 200);
UILabel *redSquare = [[UILabel alloc] initWithFrame:rectForBigRedSquare];
[redSquare setBackgroundColor:[UIColor redColor]];
[scrollView addSubview:redSquare];
return self;
}
An additional question is this: how is it possible to make it such that the user can only scroll down, that is to see content at the bottom which was out of view, but not to scroll up so that there is space before the start of the content. In
Basically you just have to set contentSize of your scrollview according to the contents.
CGSize scrollViewSize = CGSizeMake(newWidth, newHeight);
[self.myScrollView setContentSize:scrollViewSize];
Okay, the easiest way to get this scrollview working as you desire is to ensure that content size of the scrollview is identical to the frame size of the content you wish to scroll.
You can achieve this by having a content view into which you add all the views you wish to be visible and then add that content view to the scrollview while ensuring that the content size of the scrollview is set to the content view's frame size.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
UIView* contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1280, 460)];
UIView* redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[redView setBackgroundColor:[UIColor redColor]];
[contentView addSubview:redView];
[redView release];
UIView* blueView = [[UIView alloc] initWithFrame:CGRectMake(960, 0, 320, 460)];
[redView setBackgroundColor:[UIColor blueColor]];
[contentView addSubview:blueView];
[blueView release];
CGSize contentViewSize = contentView.frame.size;
[scrollView addSubview:contentView];
[scrollView setContentSize:contentViewSize];
[contentView release];
[self addSubview:scrollView];
The app I was working on had similar symptoms. The user could scroll down but on release the view would snap back to the initial position. The page was set up as follow:
[VIEW]
[SAFE AREA]
[SCROLL VIEW]
[CONTENT VIEW]
I strongly suspect that a combination of Auto-Layout and manual constraints caused by several adjustment iterations was causing the issue. To resolve this all constraints where removed from the View.
The Scroll View was assigned the following constraints:
Scroll View.leading = Safe Area.leading
Scroll View.top = Safe Area.top
Scroll View.trailing= Safe Area.trailing
Scroll View.bottom = Safe Area.bottom
The Content View was then assign the following constraints
ContentView.Leading = Scroll View.Leading
ContentView.top = Scroll View.top
ContentView.centerX = ScrollView.centerX
The Content View was also given the following self constraint
height = 1000

UIView in UIScrollView does not appear

I'm trying to create a horizontally scrollable menu similar to that in this video.
For some reason the UIView doesn't appear after adding a bunch of UIButtons to it and adding it to the UIScrollView. Here's my code (it's called in -viewDidLoad of a subclass of UIViewController):
//set up scrollview
UIScrollView *designPicker = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 431, 320, 49)];
//set up a view to drop into the scroll view
UIView * buttonsView = [[UIView alloc] initWithFrame:CGRectMake(0, 431, 640, 49)];
//add buttons to scrollview
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
float runningX = designPicker.frame.origin.x;
for (i = 1; i <= 10; i++)
{
UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[tempBtn setBackgroundImage:[UIImage imageNamed:#"button.png"] forState:UIControlStateNormal];
CGRect rect = CGRectMake(runningX, designPicker.frame.origin.y, 30.0, 30.0);
tempBtn.frame = rect;
[buttonsView addSubview:tempBtn];
runningX = runningX + 35;
[tempBtn release];
}
[designPicker setContentSize:buttonsView.frame.size];
[designPicker addSubview:buttonsView];
[self.view addSubview:designPicker];
You should not add the buttons using the frame of the UIScrollView. The origin of the frame is in the superview's (superview of the UIScrollView) coordinates. You should make the buttons' frame relative to the UIView. So if you want the buttons to appear at the top of the view that you should start at (0,0).
Instead of adding scrollview as subview to your view, add view as subview of scrollview. As-
[self.scrollView addSubview:self.view];
// release scrollView as self.view retains it
self.view=self.scrollView;
[self.scrollView release];
And make sure your view should have large content size than content size of your scrollview.It worked for me.

Resources