display selected thumbnail for initial subview in a scrollview - ios

Trying to get the selected image from an array of images in a collection view to be the initial subview in a scrollview. Right now the scrollView loads the first image from the array, no matter which image is tapped on in the collection view. Please give some advice or websites with information on how to achieve this. Thanks!
for (int i = 0; i < self.album.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [self.album objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.album.count, self.scrollView.frame.size.height);
}

At the time of creating scrollview add below line
self.scrollView.contentOffset = CGPointMake(self.scrollView.frame.size.width * self.selectedImage, self.scrollView.frame.size.height);
where self.selectedImage is the index of the image in the array
PD: Sorry for my english.

Related

UIScrollView + PageControl

I have a view controller with outlets of scrollview (initial size: 390, 170) and page control. I need it to display an image per page from my array. But i have unexpected result (see image below).
Images are not full-width and after 2nd page scrollView ends.
Here is code
- (void)viewDidLoad {
[super viewDidLoad];
//Put the names of our image files in our array.
imageArray = [[NSArray alloc] initWithObjects:#"header1.jpg", #"header2.jpg", #"header3.jpg", nil];
[self.articlesPageControll setNumberOfPages:imageArray.count];
for (int i = 0; i < [imageArray count]; i++) {
//We'll create an imageView object in every 'page' of our scrollView.
CGRect frame;
frame.origin.x = 390 * i;
frame.origin.y = 0;
frame.size = self.articlesScrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[self.articlesScrollView addSubview:imageView];
}
//Set the content size of our scrollview according to the total width of our imageView objects.
self.articlesScrollView.contentSize = CGSizeMake(self.articlesScrollView.frame.size.width * [imageArray count], 170);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.articlesScrollView.frame.size.width;
int page = floor((self.articlesScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.articlesPageControll.currentPage = page;
}
UPD: 3rd page with image actually exists, but it's impossible to scroll to this page
Thanks in advance!
Solution self.articlesScrollView.contentSize = CGSizeMake(390 * [imageArray count], 170);

Trying to create multiple views from one XIB file programmatically

I have a page enabled scroll view which I would like to fill with UIViews. I have tried creating a "XIB" file and loading from it in a for loop, but this only gives me one UIView in my scroll view.
int amount = garments.count;
[self.pageControl setNumberOfPages:amount];
for (int i = 0; i < amount; i++) {
DOVGarment *garment = garments[i];
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.size = self.scrollView.frame.size;
DOVGarmentDetailView *view = [DOVGarmentDetailView new];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"detailView" owner:view options:nil];
view = [topLevelObjects objectAtIndex:0];
view.image.image = [UIImage imageNamed:garment.fileName];
view.labelID.text = garment.ID;
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * amount, self.scrollView.frame.size.height);
Anyone have an idea why this doesn't work? Or if this is even possible?
You are creating multiple views but you are not changing the frame so they will all be placed directly on top of each other.
Update your code to offset each view relative to the one created before
Every view object you place in your xib file has only one instance of it. So every run cycle of your for loop you are accessing the same UIView instance.
Incase you need to fill your scrollView with view's you need to alloc and initialise again in every run cycle of the for loop thereby creating many instances of UIView.
This is how I did it which places UIViews horizontally in my scrollView -
for (int i = 0; i<numberOfViews; i++)
{
UIView*smallView = [[UIView alloc]initWithFrame:CGRectMake((i*scrollview.frame.size.width+heightOfYourView), 0, scrollview.frame.size.width, scrollview.bounds.size.height)];
[scrollview addSubview: smallView];
}
scrollview.contentSize = CGSizeMake(numberOfViews*scrollview.frame.size.width, scrollview.frame.size.height);

xcode 5 horizontal scrollview doesn't work (iOS7)

Im trying to create an filter bar with a horizontal scroll view.
How can I create the horizontal scrollview in xcode 5 for iOS7?
This is my code:
[self.scroll setScrollEnabled:YES];
[self.scroll setDelegate:self];
self.scroll.backgroundColor = [UIColor clearColor];
scroll.contentMode = UIViewContentModeScaleToFill;
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
[self.view addSubview:scroll];
[self.scroll setContentSize:CGSizeMake(320.0,143.0)];
For scroll view to scroll horizontally the width of the content size should be larger than the actual frame of scroll view.
If your scroll view frame size is (320, 143), the width of content size of scroll view should be larger than 320 so that the scroll view scroll.
For horizontal scroll view you can use, EasyTableView. Its simple to use.
You will need to get use to the facilities - the horizontal scrollview works and it works in different ways including simulating pages that you can flick right or left.
scrollview has a physical size (frame) and a content size, the thing with scrollviews is the content size will be larger than the physical dimensions so I can for example have a UIImageView of 930x 250 viewable in an iPhone with 320 x 480
(I'll skip the rest of the setup - just what's essential - for all the other tricks look into the apple sample projects - there is a lot - Note also that a lot of apps just use the Interface Builder to paint the view with the scrollview )
UIScrollview *scroll = [[UIScrollview alloc] init]
scroll.frame = CGSizeMake (0,100,320,250);
scroll.contentsize = CGSizeMake(930,250)
UIImageView *imageview = [[UIImageView alloc]init];
imageview.image = myimage; //(UIImage)
[self.view addSubview:scroll]; // add subview as the bottom layer to the main view
[scroll addSubview imageview];
Below is something more complex with pages:
pagectrl.numberOfPages = 7;
pagectrl.currentPage = 0;
scrollView.pagingEnabled = YES;
scrollView.contentSize=CGSizeMake(320* pagectrl.numberOfPages, 500);
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.bouncesZoom = NO;
scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
search_url.delegate = self;
user.delegate = self;
password.delegate = self;
rpc_code.delegate = self;
// add pages
int page = 0;
CGRect frame = scrollView.frame;
pagesize = frame.size.width;
frame.origin.y = 0;
frame.origin.x = frame.size.width * page;
firstView.frame = frame;
[scrollView addSubview:firstView];
page ++;
frame.origin.x = frame.size.width * page;
locsubView.frame = frame;
[scrollView addSubview:locsubView];
page ++;
frame.origin.x = frame.size.width * page;
QRgensubView.frame = frame;
[scrollView addSubview:QRgensubView];
page ++;
frame.origin.x = frame.size.width * page;
scansubView.frame = frame;
[scrollView addSubview:scansubView];
page ++;
frame.origin.x = frame.size.width * page;
symbologysubView.frame = frame;
[scrollView addSubview:symbologysubView];
page ++;
frame.origin.x = frame.size.width * page;
gaView.frame = frame;
[scrollView addSubview:gaView];
page ++;
frame.origin.x = frame.size.width * page;
upcsubView.frame = frame;
[scrollView addSubview:upcsubView];
[self registerForKeyboardNotifications];
if (gotopage == 7) {
int xloc = ((gotopage - 1) * pagesize);
CGRect fieldrect = CGRectMake(xloc,0,320, pagesize);
[scrollView scrollRectToVisible:fieldrect animated:YES];
}

The buttons / labels added to the UIScrollView are appeared only on the first page

I have a UIScrollView paging option enabled with the next code:
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i = 0; i < 6; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[scrollView scrollRectToVisible:frame animated:YES];
UIView *subview = [[UIView alloc] initWithFrame:frame];
itemName.text = #"Samara";
[self.scrollView addSubview:subview];
[subview release];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 6, 1.0);
}
I have a number of elements (buttons, labels), added in storyboard to this ScrollView element, but in UI they're appeared only on the first page - the next pages are blank. How can I solve this?
Just reading through your code example - here is how to make your scrollview page, what you add inside those scrollview is up to you:
//Set your scrollView frame
[self.scrollView setFrame:CGRectMake(0,0,320,460];
//Will use this value to set the content width after the loop
CGFloat scrollWidth = 0;
CGFloat pageWidth = self.scrollView.frame.size.width;
CGFloat pageHeight = self.scrollView.frame.size.height;
for (int i = 0; i < 6; ++i)
{
//Create the page and make the origin.x i * pageWidth
UIView *page = [[UIView alloc] initWithFrame:CGRectMake((i * pageWidth),0,pageWidth,pageHeight)];
scrollWidth += page.width;
//Add the page
[self.scrollView addSubview:page];
}
//set the content size to the right edge of the last page (scrollWidth)
[self.scrollView setContentSize:CGSizeMake(scrollWidth,pageHeight)];
//Scroll to the last page - assuming you wanted this by looking at the code in your loop
[self.scrollView scrollRectToVisible:CGRectMake((scrollWidth - pageWidth),0,pageWidth,pageHeight)];
//Enable paging and scrolling (scrollEnabled should be YES by default)
self.scrollView.pagingEnabled = YES;
self.scrollView.scrollEnabled = YES;
Best I can do based on your code - hope it helps

IOS Add subview on viewDidLoad

i have a problem when i'm trying to add subviews to a UIScrollView on viewDidLoad.
I'm using this code to programmatically add the UIImageViews to the scrollView:
- (void)viewDidLoad {
[super viewDidLoad];
NSInteger const_width = 100;
NSInteger numberOfViews = 4;
CGRect theFrame = [self.scrollView frame];
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * const_width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y,const_width,110)];
UIImage *image = [UIImage imageNamed:#"cocoloco.jpg"];
[imageView setImage:image];
//[self.scrollView addSubview:imageView];
imageView.tag = i;
CGRect rect = imageView.frame;
rect.size.height = 110;
rect.size.width = 110;
imageView.frame = rect;
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(const_width * numberOfViews, 110);}
But i get the current view:
It seems that the scroll view frame takes its position regardless the 3 yellow tabs (that are a special TabBarController) so i get a wrong frame origin from the UIScrollView and therefore the UIImageViews are wrong positioned.
Any idea?
I don't know exactly how to do it, but add the height of the frame to the "Y" position of your rectangle. Something like this:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin,theFrame.origin.y + (theFrame.height),const_width,110)];
To get the scrollview below the navigation bar at the top, try
self.scrollview.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
Then position the table below the scrollview by setting it's origin base on the scrollview origin and height:
CGRect frame = tableView.frame;
frame.origin.y = self.scrollview.frame.origin.y + self.scrollview.frame.size.height;
tableView.frame = frame;
You should move any resizing or layout of your UI to the -(void)layoutSubviews method and this should sort your problem correctly.

Resources