UIScrollView pages in the negative direction - ios

So I have a UIScrollView set up the following way and for some reason, the scroll view is displayed like this:
Phone Screen
---------------
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|----Content----|
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------|---------------|
---------------
My goal is to have the user scroll to the right, like below:
Phone Screen
---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|----Content----|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
|---------------|---------------
---------------
Here's how the scroll view is set up (this is a bit simplified, but accurate):
CGFrame frame = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height);
CGSize contentSize = CGSizeMake(self.bounds.size.width*2, self.bounds.size.height);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.contentSize = contentSize;
scrollView.propertyN = bool; //E.g. pagingEnabled, bounces, clipsToBounds, etc.
UIImageView *imageView = [[UIImageView alloc] initWithImage:someImage];
imageView.frame = frame;
[scrollView addSubview:imageView];
[self.view addSubview:scrollView];
Has anyone else had this problem? I'm sure I'm making some extremely basic mistake in my setup of the scroll view but can't seem to spot it.

Could the the origin for self.view.frame be offscreen (e.g. -self.bounds.width)?
I'd try starting with simple code and building back up to what you want. Here's some simple code that's similar to the code above that you can throw in a blank project to convince yourself that the code you posted isn't the likely culprit:
CGFloat width = self.view.bounds.size.width;
CGFloat height = self.view.bounds.size.height;
CGRect frame = CGRectMake(0.0f,0.0f,width,height);
CGSize contentSize = CGSizeMake(width*2, height);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:frame];
scrollView.backgroundColor = [UIColor yellowColor];
scrollView.contentSize = contentSize;
scrollView.pagingEnabled = YES;
UIView *view1 = [[UIView alloc] initWithFrame:frame];
view1.backgroundColor = [UIColor blueColor];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(width,0.0f,width,height)];
view2.backgroundColor = [UIColor greenColor];
[scrollView addSubview:view1];
[scrollView addSubview:view2];
[self.view addSubview:scrollView];

Related

Why the origin of contentview doesn't coincide the origin of scrollview when application setup?

The problem shows in the gif picture. (The view in yellow is the contentview. The view in black is the scrollview.)
The whole project code on Github.
Here is the code :
CGRect frame = CGRectMake( 50, 100, 200, 200);
UIScrollView *scrollView= [[UIScrollView alloc] initWithFrame:frame];
[self.view addSubview:scrollView];
frame= CGRectMake( 0, 0, 500, 500);
UIImageView *myImageView= [[UIImageView alloc] initWithFrame:frame];
[scrollView addSubview:myImageView];
scrollView.contentSize = CGSizeMake(500,500);
scrollView.backgroundColor = [UIColor blackColor];
myImageView.backgroundColor = [UIColor yellowColor];
scrollView.contentOffset = CGPointMake(0, 0)
One more problem shows in the picture:
Why the horizontal scrollbar is not at bottom of scrollview?
When I added a view between the scrollview and self.view the problem disappeared.
The code that I added:
UIView *view = [UIView new];
[self.view addSubview:view];
I found that the problem happened when I used UITabBarController.
Make the UIScrollView a global variable. Make the contentInset of the UIScrollView and UIScrollView's indicators UIEdgeInsetsZero when the views are loaded like this:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_scrollView.contentInset = UIEdgeInsetsZero;
_scrollView.scrollIndicatorInsets = UIEdgeInsetsZero;
}

Image not shown on vertical-scrollView in a background horizontal-scrollView

I would like to show let's say 3 long pictures (longer than iPhone's screen height) in a big background UIScrollView horizontally, and since each picture is longer than the screen height, so I put another 3 Sub-UIScrollView that are for vertical swiping in the background one.
I setup the background one like this:
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight)];
_scrollView.delegate = self;
_scrollView.contentSize = CGSizeMake(screenWidth*_count, screenHeight);
_scrollView.pagingEnabled = YES;
_scrollView.alwaysBounceVertical = NO;
_scrollView.alwaysBounceHorizontal = YES;
And I setup each Sub-UIScrollView like this:
if (imageHeight > screenHeight) { //requiring extra scrollView to support
UIImageView *longImgView = [[UIImageView alloc] init];
[longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
longImgView.image = _image;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(originX, 0, screenWidth, screenHeight)];
[scrollView setContentSize:CGSizeMake(screenWidth, imageHeight)];
[scrollView setContentOffset:CGPointMake(originX, 0)];
scrollView.backgroundColor = [UIColor blueColor];
scrollView.alwaysBounceVertical = YES;
scrollView.alwaysBounceHorizontal = NO;
[scrollView addSubview:longImgView];
[_scrollView addSubview:scrollView];
}
Please notice that I set scrollView.backgroundColor = [UIColor blueColor]; to highlight this sub-scrollView, but when I run it, I can only see the blue-colored sub-scrollView, I cannot see the picture showing (longImgView.image = _image;).
UPDATE: I can see the memory address allocated to the _image with break point and console. Also I can see the sub-scrollView has set the same contentSize as the image size but only cannot see the image itself.
Wait: [longImgView setFrame:CGRectMake(originX, 0, screenWidth, imageHeight)];
make that:
[longImgView setFrame:CGRectMake(0, 0, screenWidth, imageHeight)];

iOS 8 Custom keyboard UIScrollview isn't working

I have tried to add UISchollView in custom keyboard view but it shows view but not scrolling.
here is my code. i have also tried self.view but its not working either.
UIScrollView *scrollview1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
NSInteger viewcount= 8;
for (int i = 0; i <viewcount; i++)
{
CGFloat y = i * 50;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, y,50, 50)];
view.backgroundColor = (i%2==0)?[UIColor greenColor]:[UIColor blueColor];
[scrollview1 addSubview:view];
}
scrollview1.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height *viewcount);
scrollview1.backgroundColor = [UIColor redColor];
scrollview1.pagingEnabled=YES;
[self.inputView addSubview:scrollview1];
UIScrollView will scroll if its contentSize is greater then its frame size. So try setting its contentSize property and also check if userInteraction is enabled or not.
Hope it will work.

How to create UIScrollview inside UIScrollview programmatically

Please tell me how to set scrollview inside a scrollview like nested process.
The following code works partially.
int x=10;
int y=10;
for(int i=0; i<5; i++)
{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(x, y, 50, 50)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
scrollview.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(50,50);
y=y+95;
}
Now all I can see are 3 scrollviews and the others are hidden. How can I create main scroll to view so that the child scrollViews are not hidden?
You need to have an initial scrollView that you then put these scrollViews in.
UIScrollView * mainScrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
mainScrollView.contentSize = CGSizeMake(50, (y + 95) * 5);
// further configure
[self.view addSubview: mainScrollView];
Then change
[self.view addSubview:scrollview];
To
[mainScrollView addSubview: scrollView];
//I have created Two Scroll view programmatically this way
UIScrollView *scrollViewOuter = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
scrollViewOuter.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
scrollViewOuter.contentSize = CGSizeMake(2000.0f, 2000.0f);
UIScrollView *scrollViewInner = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
scrollViewInner.backgroundColor = [UIColor whiteColor];
scrollViewInner.contentSize = CGSizeMake(2000.0f, 2000.0f);
[scrollViewOuter addSubview:scrollViewInner];
[self.window addSubview:scrollViewOuter];
//You can change frame and use in your own way
Just create a parent scrollview that is big enough to hold the 5 smaller ones, then change this line:
[self.view addSubview:scrollview];
to
[parentScrollView addSubview:scrollview];

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
}

Resources