Scrollview content shows but not scrolling - ios

I have a ViewController with a ScrollView containing bunch of content from IB but the scrollview isn't working.
- (void)viewDidLoad
{
[super viewDidLoad];
/*
* configure a bunch of label, text ,images and views from IB
*/
UIScrollView* scrollView = (UIScrollView*)self.view;
scrollView.scrollEnabled = YES;
scrollView.contentSize = CGSizeMake(320, 2000);
}
I can see my content up until the bottom but can't scroll. If it means anything, this ViewController in one of the tabs in a TabBar

For scroll view to scroll its Frame size must be smaller then its Content Size
You have added the scrollView FROM code programatically but looks like you are talking about adding it through Xcode's Storyboard or in nib file
IF you are adding it thought code you need to add following code
[self.view addSubView:scrollView];
Here is one example
#interface ImageScrollView ()
#property (nonatomic, strong) UIScrollView *scrollView;
#property (nonatomic, strong) UIPageControl *pageControl;
#end
-(void) baseInit {
// Scroll View - Full frame width
_scrollView = [[UIScrollView alloc] init];
_scrollView.frame = self.frame;
_scrollView.delegate = self;
// ImageView - Full frame width
CGFloat width = self.frame.size.width;
CGFloat height = self.frame.size.height;
int i = 0;
int count = [imagesArray count];
for (i = 0; i < count; i++) {
UIImage *image = [UIImage imageNamed:imagesArray[i]];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * width, 0, width, height)];
[imageView setImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.clipsToBounds = YES;
imageView.tag = i+1;
[_scrollView addSubview:imageView];
}
[_scrollView setContentSize:CGSizeMake(count * width, height)];
[_scrollView setFrame:CGRectMake(0, 0, width, height)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
pageControl.numberOfPages = count;
pageControl.currentPage = 0;
CGRect pageFrame = CGRectMake(5, 5, 50, 50);
pageControl.frame = pageFrame;
pageControl.backgroundColor = [UIColor redColor];
[self addSubview:_scrollView];
[_scrollView addSubview:pageControl];
}
Dont forget to call baseInit function in your view did load.
This example work without needing to add any objects in Storyboard or nib files

Related

scrollView with paging enabled not working properly ? (Objective-C)

i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];

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.

Adding UIImageView to UIScrollView programmatically

I'm trying to create programmatically a UIScrollView containing a UIImageView. The image of the UIImageView should be scrolled vertically, my problem is that the UIScrollView is never added to my main View.
Here's the code
#property (strong, nonatomic) UIImageView *advertiseView;
#property UIScrollView *sv;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_sv = [[UIScrollView alloc] init];
_advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,568)];
UIImage *image = [UIImage imageNamed:#"abc.png"];
_advertiseView.image = image;
_advertiseView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[_sv addSubview:_advertiseView];
_sv.contentSize = CGSizeMake(320,1136); // size of abc.png
[self.view addSubview:_sv];
}
What am i missing??
You don't set the ScrollView size. Try this:
_sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
int width = 0;
int height = 0;
for (int i=0; i<[menupicarray count]; i++)
{
NSString *menupicname=[NSString stringWithFormat:#"%#",[menupicarray objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] init ];
[imageView setImageWithURL:[NSURL URLWithString:menupicname]
placeholderImage:[UIImage imageNamed:#"NOmage.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake( MenuView.frame.size.width*i,0, MenuView.frame.size.width, MenuView.frame.size.height-5);
[MenuView addSubview:imageView];
width = imageView.frame.size.width;
height = imageView.frame.size.height;
MenuView.contentSize = CGSizeMake(width*i+imageView.bounds.size.width, height);
}

The property frame of UIViewController's view doesn't work while using a single subview under UIScrollView

Before using UIScrollView's zoom feature, everything is fine.
The code as bellow:
UIScrollView* scrollView = [[UIScrollView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[scrollView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[scrollView addSubview:wineView];
After adding zoom feature, code looks like:
UIScrollView* scrollView = [[UIScrollView alloc]init];
UIView* zoomView = [[UIView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[zoomView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
scrollView.maximumZoomScale = 2.0;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[zoomView addSubView:wineView];
[scrollView addSubview:zoomView];
The problem is:
wineView displayed on the first page, not on the foodIntroCount+1 page.
Your code should consider the previous y position and height both if you want to add subview one after another vertically. So your code should look like below
imgView.frame = CGRectMake(0, height*i+y, width, height);
The code isn't tested though, try this..

how can i put the ScrollView on ViewController

I am trying to put the Scrollview on the viewcontroller? is it possible? and i also want to put the UIView on the ScrollView.
UIScrollView *scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(20, 60, 280, 200)];
scrollView.contentSize = CGSizeMake(280, 200);
scrollView.backgroundColor=[UIColor orangeColor];
scrollView.delegate = self;
[self.view addSubview:scrollView];
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(20, 60, 280, 200)];
container.backgroundColor = [UIColor whiteColor];
[scrollView addSubview:container];
In this code i tried to put the scrollview on the VC and also i tried to put View on the ScrollView.
rb1 = [[RadioButton alloc] initWithGroupId:#"first group" index:j];
rb1.frame = CGRectMake(20,dy,22,22);
[container addSubview:rb1];
[rb1 release];
label1 =[[UILabel alloc] initWithFrame:CGRectMake(50, dy, 60, 20)];
label1.backgroundColor = [UIColor clearColor];
label1.text = [answers objectAtIndex:j];
[container addSubview:label1];
and here i tried to put the a radio button on View.
I hope it is clear:) Sorry my bad english.Thanks.
Scroll occurs when the frame of a scroll view is less than your content size.
Your frame is having size (280,200) and you have provided same size to contentSize.
To make the scroll possible replace below line into your code.
scrollView.contentSize = CGSizeMake(300, 260);
Here you put the same content size as scroll size:
scrollView.contentSize = CGSizeMake(280, 200);
In content size you should enter the size of area you want to scroll. In your case scroll does not need to scroll because scrollable area is same as scrollView size.
try this to put the scrollview on the controller
#import <UIKit/UIKit.h>
#interface YourClass : UIViewController {
IBOutlet UIScrollView *scrollView;
}
#property (nonatomic, retain) UIScrollView *scrollView;
#end
to put v/v try this
NSUInteger scrollContentCount = 0;
for (NSUInteger arrayIndex = 0;
arrayIndex < [contents count];
arrayIndex++) {
// set scorllview properties
CGRect frame;
frame.origin.x = self.mScrollView.frame.size.width * arrayIndex;
frame.origin.y = 0;
frame.size = self.mScrollView.frame.size;
myScrollView.autoresizingMask = YES;
// alloc - init PODetailsView Controller
myController = [[MyController alloc] initWithNibName:#"MyController" bundle:[NSBundle mainBundle]];
[myController.view setFrame:frame];
// add view in scroll view
[self.myScrollView addSubview:myController.view];
scrollContentCount = scrollContentCount + 1;
}
// set scroll content size
self.myScrollView.contentSize =
CGSizeMake(self.myScrollView.frame.size.width * scrollContentCount,
self.myScrollView.frame.size.height);
}

Resources