Adding UIImageView to UIScrollView programmatically - ios

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);
}

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)];

Adding label in scrollView

My problem : I have a white window and I don't understand why.
I tried some solutions but nothing works.
Thx for your help.
CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
[self.view addSubview: monScrollView];
for (int i = 0; i < 10; i++)
{
CGRect rectImage = CGRectMake(0,longueurDevice/2+i*30,320,30); // Définition d'un rectangle
CGRect rectLabel = CGRectMake(10,longueurDevice/2+i*30,320,30);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
imageView.image = [UIImage imageNamed:#"barreTest.png"];
[monScrollView addSubview: imageView];
UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
monLabel.text = #"Ceci est un label";
[monScrollView addSubview: monLabel];
}
Your UIlabel and UIImageView frame are seated out of visible bounds.
as example if you set
CGRect rectImage = CGRectMake(0,i*30,320,30);
CGRect rectLabel = CGRectMake(10,(i*30)+40,320,30);
you will be able to see them on the scrollview. Please set frame properly to see your UI components and also set content size of scroll view to make it scrolling. If your content size is bigger the scrollview frame, scroll view will be able to scroll,
Thanks for your answers.
Now it works. If you have any advice for me, it will be a pleasure to me.
int max = 10;
CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
monScrollView.contentSize=CGSizeMake(largeurDevice,30*max);
[self.view addSubview: monScrollView];
for (int i = 0; i < max; i++)
{
CGRect rectImage = CGRectMake(0,i*30,largeurDevice,30); // Définition d'un rectangle
CGRect rectLabel = CGRectMake(10,i*30,largeurDevice,30);
CGRect rectBututton = CGRectMake(largeurDevice-20,longueurDevice/2+i*30,10,10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
imageView.image = [UIImage imageNamed:#"barreTest.png"];
[monScrollView addSubview: imageView];
UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
monLabel.text = #"Ceci est un label";
[monScrollView addSubview: monLabel];
}

Z position of UIImageView to that of UIScrollView

I have both UIScrollView and another UIImageView. I want the UIImageView to come over the UIScrollView. I've tried all the bringSubviewToFront and the insertAtIndex stuff but its not working. Please help me out!
Here is the code -
For UIScrollView:
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 300,
SCREEN_WIDTH, SCREEN_HEIGHT)];
scrollView.showsHorizontalScrollIndicator = FALSE;
[self.view addSubview:scrollView];
__block int tagValue = 1;
__block NSInteger tag = 1;
for (int i=0; i<[listOfImages count]; i++) {
NSDictionary *myDic = [listOfImages objectAtIndex:i];
NSString *urlImage = [myDic objectForKey:#"product_image"];
//NSLog(#"%lu",(unsigned long)[listOfImages count]);
image = [[UIImageView alloc] initWithFrame:CGRectMake(leftMargin, 0, 200, 140)];
// [image setImage:[UIImage imageNamed:#"img_def.png"]];
NSURL *imageURL = [NSURL URLWithString:urlImage];
[image setImageWithURL:imageURL
placeholderImage:[UIImage imageNamed:#"img_def.png"]];
image.tag = tag;
image.contentMode = UIViewContentModeScaleAspectFit;
[scrollView insertSubview:image atIndex:1];
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:#selector(handleTap:)];
recognizer.numberOfTapsRequired = 1;
recognizer.numberOfTouchesRequired = 1;
recognizer.delegate = self;
[image addGestureRecognizer:recognizer];
[image setUserInteractionEnabled:YES];
leftMargin += SCREEN_WIDTH;
tagValue += 1;
tag += 1;
}
[scrollView setContentSize:CGSizeMake(leftMargin, 0)];
[scrollView setPagingEnabled:YES];
And the image code that I want to come on top of the scroll view -
UIImage *originalPromo = [UIImage imageNamed:#"promo"];
UIImage *scaledPromo = [UIImage imageWithCGImage:[originalPromo CGImage]
scale:(originalPromo.scale *2.0) orientation:(originalPromo.imageOrientation)];
UIImageView *promo = [[UIImageView alloc] init];
[promo setFrame:CGRectMake(45.5, 300.0,
scaledPromo.size.width, scaledPromo.size.height)];
[promo setImage:scaledPromo];
[self.view insertSubview:promo atIndex:100];
Instead of using insertSubview:atIndex use addSubview:. When a subview is added, it's always added on top of all other subviews already in the parent view.

Scrollview content shows but not scrolling

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

UIScrollView With Pictures Generated

I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}

Resources