Trying to create multiple views from one XIB file programmatically - ios

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

Related

How do I create a home Scroll view like Instagram?

I'm trying to create a home scroll, inside it, is a UIView that contains 2 UIImage, 2 UILabel and 2 UIButton. I was trying to copy the entire view and paste it above the copied UIView and so on.
I was trying to do it with this:
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: self.contentView];
UIView *newView = [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
[self.homeScroll addSubview:newView];
It apparently copy paste it correctly but my problem is that I want the newView duplicate stay one above each other. I was trying to position them like:
newView.center = CGPointMake(0.0, screenRect.size.height * i);
and
frame.origin.x = 0; // new x coordinate
frame.origin.y = screenRect.size.height * i; // new y coordinate
newView.frame = frame;
and
newView.layer.frame = CGRectMake(0, screenRect.size.height * i, self.homeScroll.frame.size.width, self.homeScroll.frame.size.height);
But without success.In all cases i is the typical int i variable of a for loop that run a requested object from a web service. And screenRect.size.height is the actual device screen height obtained from here CGRect screenRect = [[UIScreen mainScreen] bounds];. I increased the height of the UIScroll view like this:
CGSize newFrame = (CGSize){0 , screenRect.size.height * numberOfItems};
[self.homeScroll setContentSize:newFrame];
And works as expected.
I 've read About Collection View but it does not seems to be the solution.
Here is a Screen Shot.

View loaded from xib file and added to scrollView draws over it

I have scrollView set for horizontal scrolling (paging enabled, ...).
If i create UiViews in code and add them to scrollView everything works ok.
Example:
//second page
CGRect frame2;
frame2.origin.x = myScrollView.frame.size.width;
frame2.origin.y = 0;
frame2.size = myScrollView.frame.size;
UIImage *tutImage2=[UIImage imageNamed:#"tutorialScreen2.png"];
UIImageView *tutorialImage2=[[UIImageView alloc] initWithImage:tutImage2];
tutorialImage2.frame=tutImageFrame;
UIView *subview2 = [[UIView alloc] initWithFrame:frame2];
subview2.backgroundColor = [UIColor clearColor];
[subview2 addSubview:tutorialImage2];
[myScrollView addSubview:subview2];
The problem is with views loaded from xib, because they draw over scrollView (eventhough their frame is set to height smaller than full screen).
Example:
//third page
CGRect frame3;
frame3.origin.x = myScrollView.frame.size.width*2;
frame3.origin.y = 0;
frame3.size = myScrollView.frame.size;
dashView = [[[NSBundle mainBundle]loadNibNamed:#"DashboardView"owner:self options:nil] lastObject];
dashView.frame=frame3;
[myScrollView addSubview:dashView];
I tried setting my view's simulated metrics size to freeform and retina 4inch and it didnt help. I also turned off autolayout (i dont use it in my xib) but it also didnt help.
Any idea?

How Can I Scroll two UILabels and a UIWebView with the same scroll IOS?

I would like to group two UILabels and one UIWebView with just one scroll for this three components.
I don't know how I can do this ?
I tried with a UIScrollView in .XIB who include labels and UIWebView but it doesn't work.
If it possible I would like a graphic solution.
The soultion is more complex then you actually think
1st add a scrollView
#property (nonatomic, strong) UIScrollView * scrollView;
Then add it as subvew from View
self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.scrollView];
But the most complex part is webview coz it has its own scroll part to fix that -
self.webViewNewsBody.scrollView.scrollEnabled = NO;
self.webViewNewsBody.scrollView.bounces = NO;
[self.scrollView addSubview:self.webViewNewsBody];
Yet you cant find the final ht of the web view hence wait till it loads its content
- (void)webViewDidFinishLoad:(UIWebView *)webView {
CGRect frame;
float webHt = [[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollHeight;"] floatValue];
// Finally set the total ht for the scroll view
self.scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, 300 + webHt);
}
At first make Outlets for you labels and webView
[UIView animateWIthDuration:0.2 animations:(^{
CGFrame fr = self.labelFirst.frame;
frame.origin.y += 50;
self.labelFirst.frame = fr;
fr = self.labelSecond.frame;
fr.origin.y += 50;
self.labelSecond.frame = fr;
fr = self.webView.frame;
fr.origin.y += 50;
self.webView.frame = fr;
}];
make webView Scrollable no
webView.scrollView.scrollEnabled = NO;

display selected thumbnail for initial subview in a scrollview

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.

IOS: UIScrollView with an infinite paging view

I have this code for a scrollview to showe 3 images:
const CGFloat kScrollObjHeight = 150.0;
const CGFloat kScrollObjWidth = 320.0;
const NSUInteger kNumImages = 3;
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
scrollView2.pagingEnabled = YES;
scrollView3.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
//[scrollView2 addSubview:imageView];
//[scrollView3 addSubview:imageView];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
But now I want to do a scrollview that when is in last image show me after the first image of scroll view and the same thing when I have the first image if I go back, it must show the last image; so I want create a paging loop.
The basic idea is to set yourself as a UIScrollViewDelegate and apply some modulo arithmetic to the scroll position in order to wrap it around.
There are two basic variations on the idea. Suppose your images are A, B, C, so you currently have them within the scrollview ordered as ABC.
In the more logically pleasing solution — especially if you had lots and lots of images — you watch the scroll position and as soon as it gets to a position where the view is being pushed rightward and C has left the screen, you reorder the images as CAB and shift the current scroll position one spot to the right so that the move is invisible to the user. To put that another way, the scroll position is restrained to an area of two screens, centred on the middle of B (so, you get all of B and half a screen either side). Whenever you wrap it from somewhere on the left to somewhere on the right you shift all your image views one place to the right. And vice versa.
In the slightly easier to implement variation, instead of creating a scroll view with images arranged ABC, arrange then as CABCA. Then apply the same wrap around logic but to a central area of four screens and don't do any view reshuffling.
Make sure you use just setContentOffset: (or the dot notation, as in scrollView.contentOffset =) as a setter. setContentOffset:animated: will negate velocity.

Resources