IOS: add some sub-scrollview in a scrollview, image don't appear - ios

I have this code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
[subview setImage:[image objectAtIndex:i]];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
scrollV is the main scrollview and it have "paging enabled"
if I use this code I have my scrollV with paging enabled but I see inside it only "1.png" at first page, I don't see others png, why?

You're settings the frame of subview to the incorrect value. That is making it the same frame as its subScrollView so it's pushing it off to the right. Try this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *image = [NSArray arrayWithObjects:[UIImage imageNamed:#"1.png"], [UIImage imageNamed:#"2.png"], [UIImage imageNamed:#"3.png"], nil];
for (int i = 0; i < image.count; i++) {
CGRect frame;
frame.origin.x = scrollV.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollV.frame.size;
UIScrollView *subScrollView = [[UIScrollView alloc] initWithFrame:frame];
UIImageView *subview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
UIImage *image = [image objectAtIndex:i];
subScrollView.contentSize = image.size;
[subview setImage:image];
[subScrollView addSubview:subview];
[scrollV addSubview:subScrollView];
[subview release];
[subScrollView release];
}
scrollV.contentSize = CGSizeMake(scrollV.frame.size.width * image.count, scrollV.frame.size.height);
}
Edit: Also probably best to set the content size of the inner scrollview. I added code to that effect above.

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

How to start the photo scroller with the selected image from collectionView in ios

I have used the code
imageSlider = [[UIScrollView alloc]initWithFrame:CGRectMake(5, 60, 700, 700)];
imageSlider.pagingEnabled = YES;
imageSlider.showsHorizontalScrollIndicator =NO;
for (int i = 0; i < [myObject count]; i++) {
CGRect frame;
frame.origin.x = imageSlider.frame.size.width * i;
frame.origin.y = 0;
frame.size = imageSlider.frame.size;
NSManagedObject *con = [myObject objectAtIndex:i] ;
UIImage *Image = [UIImage imageWithData:[con valueForKey:#"image"]];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 700, 700)];
imgView.image = Image;
imgView.frame = frame;
[imageSlider addSubview:imgView];
}
imageSlider.contentSize = CGSizeMake(imageSlider.frame.size.width*([myObject count]), 600);
imageSlider.delegate = self;
[self.view addSubview:imageSlider];
It starts from first image I dono how to start image from the selected image in previous viewController. Thanks in advance..

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.

Simple Image Gallery using UIScrollView

I am trying to create a very basic image gallery that will contains three images and user will be able to slide over and view the images.
My solution is working using UIColor
NSArray *colors = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor redColor], [UIColor greenColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [colors count], 208);
But images do not displayed with similar solution
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:#"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3c.jpg"], nil];
for (int i = 0; i < images.count; i++) {
CGRect frame;
frame.origin.x = 320 * i;
frame.origin.y = 0;
frame.size = CGSizeMake(320, 208);
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [images objectAtIndex:i];
[self.slideshow addSubview:subview];
}
self.slideshow.contentSize = CGSizeMake(320 * [images count], 208);
Any idea? The UIScrollView exists inside a UIView.
Try and change
NSArray *images = [NSArray arrayWithObjects:[UIImage imageWithContentsOfFile:#"pic_1_3a.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3b.jpg"], [UIImage imageWithContentsOfFile:#"pic_1_3c.jpg"], nil];
to
NSArray *images = [NSArray arrayWithObjects:[UIImage imageNamed:#"pic_1_3a.jpg"], [UIImage imageNamed:#"pic_1_3b.jpg"], [UIImage imageNamed:#"pic_1_3c.jpg"], nil];
- (void)viewDidLoad
{
arrImages=[[NSArray alloc]initWithObjects: #"bigbazaar_card.jpg", #"CouponImage.png", #"futureBazaar_card.jpg",#"No_deal.png",#"Reliance_card.jpg",#"wallmart_card.jpg",#"westside_card.jpg", nil];
int x=5;
int y=5;
for (int i=0; i<[arrImages count]; i++) {
NSString *str=[arrImages objectAtIndex:i];
UIImageView *currentimage=[[UIImageView alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[currentimage setImage:[UIImage imageNamed:str]];
[scroll addSubview:currentimage];
UIButton *btnTag=[[UIButton alloc]initWithFrame:CGRectMake(x, y, 150, 220)];
[btnTag setTitle:#"" forState:UIControlStateNormal];
selectedImageIndex=i;
btnTag.tag=i;
NSLog(#"btntag-->%i",btnTag.tag);
[btnTag addTarget:self action:#selector(BtnShowImage:) forControlEvents:UIControlEventTouchUpInside];
[scroll addSubview:btnTag];
x= x+170;
}
scroll.contentSize = CGSizeMake(170* arrImages.count,scroll.frame.size.height);
}
and you have to declare delegate of UIScrollView in .h file as shown below:
#interface ViewController : UIViewController<UIScrollViewDelegate>
and you have to implement ScrollView's Delegate method if you want to display image on click as shown below:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGPoint contentOffset = scrollView.contentOffset;
contentOffset.x += scrollView.frame.size.width / 2;
NSInteger index = contentOffset.x / scrollView.frame.size.width;
NSString *strTemp=[arrImages objectAtIndex:index];
[imageData setImage:[UIImage imageNamed:strTemp]];
}

Add multiple UIImageview in UIScrollview programmatically

UIView *view = nil;
NSArray *subviews = [scrollView subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
I had placed one Scrollview, within this scroll view i place 5 UIImageview in XIB file
I have one image and one button, whenever I clicked the button the image should be loaded into all the UIImageviews within the UIScrollview programmatically.
and also I used the following coding
for(UIImageView *addview in Scroll)
{
if([addview isKindOfClass:[UIImageView class]])
{
UIImageView *newImage = (UIImageView *)addview;
newImage.image = [UIImage imageNamed:#"EarP010.jpg"];
}
}
Try this,
NSMutableArray * imagesArray = [[NSMutableArray alloc]init];
for (int imageCount = 0; imageCount < (noOfImages);imageCount++)
{
[imagesArray addObject:[UIImage imageNamed:#"localImagePath%d.png",imageCount]];
}
UIScrollView * scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0.0,0.0,480.0,960.0)];
scrollview.contentSize = CGSizeMake(scrollview.size.width * (noOfImages),scrollview.frame.size.height);
scrollview.pagingEnabled = YES;
CGFloat xPos = 0.0;
for (UIImage * image in imagesArray) {
#autoreleasepool {
UIImageView * imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(xPos, 0.0,scrollview.frame.size.width ,self.scrollView.frame.size.height);
[scrollview addSubview:imageview];
xPos += scrollview.size.width;
}
}
Use this
NSArray* objects = [[NSBundle mainBundle] loadNibNamed:#"your xib name" owner:nil options:nil];
UIView* mainView = [objects objectAtIndex:0];
for (UIView* view in [mainView subviews]) {
if([view isKindOfClass:[UIImageView class]])
{
UIImageView *iview = (UIImageView *)view;
iview.image = [UIImage imageNamed:#"your imagename.png"];
}
}
You could add the image views in a while loop if you know how many image view you need.
UIScrollView *sv = [UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
....
[self.view addSubView:sv];
int i = 0;
while(i < numberOfImages){
i = i + 200;
UIImageView *i = [UIImageView alloc] initWithFrame:CGRectMake(20, i, 200, 100)]
....
[sv addSubView:i];
i++;
}
This is one situation you could use. You will need to know the number of image views you need and what the Y values are on these image views. Much of this code is pseudo code and needs to be filled in.

Resources