Gap between subviews of UIScrollView - iphone

I have two subviews of UIScrollView. One is textView and other is tableView. I made textview's height flexible based on its content, but I have one problem. If the text in the textview (which is parsed data) is small, there is a big gap between textview and tableview. Or if the text is too large then it covers the tableview. How can I keep the same gap between textview and tableview irrespective of the amount of content of text view? This is all done in IB.
Thanks.

you must handle both textview and tableview position dynamically like ur text view height and y-axis is
y-axis 10; height 40;
now in tableview setframe:cgrectmake(your X coordinate, textview.frame.size.height+20)
so its always show same difference between textview and tableview if your textview height is 60 than tableview y point is 60+20

- (void)loadView
{
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.showsVerticalScrollIndicator = NO;
pagingScrollView.showsHorizontalScrollIndicator = NO;
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width * [self imageCount],
pagingScrollViewFrame.size.height);
pagingScrollView.delegate = self;
self.view = pagingScrollView;
}
- (CGRect)frameForPagingScrollView {
CGRect frame = [[UIScreen mainScreen] bounds];
frame.origin.x -= PADDING;
frame.size.width += (2 * PADDING);
return frame;
}
- (CGRect)frameForPageAtIndex:(NSUInteger)index {
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
CGRect pageFrame = pagingScrollViewFrame;
pageFrame.size.width -= (2 * PADDING);
pageFrame.origin.x = (pagingScrollViewFrame.size.width * index) + PADDING;
return pageFrame;
}

Related

jump to pages in a scrollview that is using UIPagecontrol

I have UIScrollview that contains different UITableviews. You can scroll through these UITableviews using a UIPagecontrol.
This is how I add my UITableview to the UIScrollview
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageStages.count) {
// If it's outside the range of what we have to display, then do nothing
return;
}
// Load an individual page, first seeing if we've already loaded it
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
CGRect frame = self.scrollView.bounds;
//frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
float offset=20;
frame.size.width = frame.size.width-(2*offset);
frame.origin.x = (326 * page)+offset;
frame.origin.y = 0;
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame];
tableStage.backgroundColor = [UIColor colorWithRed:(250/255.0) green:(248/255.0) blue:(247/255.0) alpha:100];
tableStage.contentMode = UIViewContentModeScaleAspectFit;
tableStage.delegate = self;
tableStage.dataSource = self;
tableStage.tag = page;
tableStage.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:tableStage];
[self.pageViews replaceObjectAtIndex:page withObject:tableStage];
}
}
Now I want that user can jump to a certain UITableview from another UIViewController. Therefore I have a variable pageNumber. When I set the pageNumber I call a method jumpToPage3 and I give the pageNumber with it. This is how the function looks like.
-(void)jumpToPage3:(NSNumber *)page{
NSLog(#"jump to page 3");
float offset=30;
float height = self.scrollView.bounds.size.height;
float width = 320 - (2*offset);
int pagee = [page intValue] - 1;
float x = (320 * pagee) + offset;
[self.scrollView scrollRectToVisible:CGRectMake(x, 0, width , height) animated:NO];
}
The problem is that it is not jumping to the correct page. It stays on the first page.
Can someone help me with this?
When you add content to the scroll view you also have to tell increase the contentSize

Dynamic UITextView in a dynamic UIScrollView - iOS

I'm facing a problem which takes me too long to solve.
I built a view, which contains a ScrollView. in the ScrollView, there's a view, which contains an image, and a UITextView. the textview should be in a dynamic height, with scrolling disabled. the textview gets all the text, but cut it off, and shows only the text that fits the height. in addition, the ScrollView doesn't changes.
- (void)viewDidLoad
....
//sets the text to the textview
[self.contentArticle setText:[NSString stringWithString:xmlParser.articleContent]];
//configures the scrollView
[self configureScrollView];
....
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect frame = self.contentView.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.scrollView.frame = frame;
[self.contentView sizeToFit];
[self.scrollView sizeToFit];
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
I did so many changes, and im not sure what is going on in there anymore!...
help would be sooo nice :)
UPDATE-
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
[self.scrollView addSubview:self.contentView];
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
[self.scrollView setContentSize:textViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
}
Try
- (void)configureScrollView{
self.contentView.autoresizingMask = UIViewAutoresizingNone;
self.contentArticle.autoresizingMask = UIViewAutoresizingNone;
CGRect textViewFrame = self.contentArticle.frame;
textViewFrame.size = [self.contentArticle contentSize];
self.contentArticle.frame = textViewFrame;
CGRect contentViewFrame = self.contentView.frame;
contentViewFrame.size.height = textViewFrame.origin.y+textViewFrame.size.height;
self.contentView.frame = contentViewFrame;
[self.scrollView setContentSize:contentViewFrame.size];
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}
Source code
had same issue , tried to find solution for many days and finally i got it working.
I have a textview inside a scrollview.
disabled autolayout from storyboards
ive added the textview to scrollview with addsubview
and finally set the content of the scrollview, to the textview frame height.
Below my code:
_textView.text = stripped; //(some string ,yours: contentArticle)
[_textView sizeToFit];
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.scrollEnabled = NO;
_textView.frame = frame;
[self.scrollView addSubview:_textView];
[self.scrollView setContentSize:CGSizeMake(320,frame.size.height)];
Hope it helps!
You need to set self.contentView height first Use this code:
- (void)configureScrollView {
[self.contentView addSubview:self.contentArticle];
CGRect frame = self.contentArticle.frame;
frame.size.height = self.contentArticle.contentSize.height;
self.contentArticle.frame = frame;
[self.scrollView addSubview:self.contentView];
frame = self.contentView.frame;
frame.size.height += self.contentArticle.frame.height;
self.contentView.frame = frame;
self.scrollView.contentSize = self.contentView.frame.size;
self.contentArticle.editable=NO;
self.contentArticle.scrollEnabled=NO;
//enable zoomIn
self.scrollView.delegate=self;
self.scrollView.minimumZoomScale=1;
self.scrollView.maximumZoomScale=7;
}

nested scrollview in iphone

I am new to iphone development . Can anyone please tell me how to add a vertical scrollview inside a horizontal scroll view. I went through many samples but couldn't get a clear picture about it . I wan my view to be scrolled in both vertical and horizontal directions. Any help is appreciated.
Here is my code for scrolling:
EDIT: Here is my code for scrolling
self.firstScroll.pagingEnabled=YES;
self.firstScroll.clipsToBounds=YES;
int numberOfViews=3;
for(int i=0;i<numberOfViews;i++){
CGFloat xOrigin=self.view.frame.size.width*i;
UIView *awesomeView=[[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[self.firstScroll addSubview:awesomeView];
}
self.firstScroll.contentSize=CGSizeMake(self.view.frame.size.width*numberOfViews, self.view.frame.size.height);
[self.view addSubview:firstScroll];
self.nextVerticalScroll.pagingEnabled=YES;
for(int i=0;i<numberOfViews ;i++){
CGFloat yOrigin=self.view.frame.size.height * i;
UIView *verticalView=[[UIView alloc]initWithFrame:CGRectMake(0, yOrigin, self.view.frame.size.width, self.view.frame.size.height)];
verticalView.backgroundColor=[UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[self.nextVerticalScroll addSubview:verticalView];
}
self.nextVerticalScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*numberOfViews);
[self.view addSubview:nextVerticalScroll];
Thanks,
Raj
You probably do not want to have scrollviews within scrollviews because you can't control which gets the horizontal gestures and which gets the vertical gestures (without a lot of hassle, at least). It is much easier to have a single scrollview with a contentSize that is bigger horizontally and vertically than the bounds of the scrollview itself, e.g.:
- (void)configureScrollView
{
NSInteger rows = 4;
NSInteger cols = 5;
CGFloat width = self.scrollView.bounds.size.width;
CGFloat height = self.scrollView.bounds.size.height;
// configure my scroll view itself
self.scrollView.contentSize = CGSizeMake(width * cols, height * rows);
self.scrollView.pagingEnabled = YES;
self.scrollView.backgroundColor = [UIColor darkGrayColor];
// now let's add the labels to the scrollview
for (NSInteger row = 0; row < rows; row++)
{
for (NSInteger col = 0; col < cols; col++)
{
// making my label just a little smaller than the scrollview's bounds so I can easily see the scrolling/paging
CGRect frame = CGRectMake((width * col) + 20.0, (height * row) + 20.0, width - 40.0, height - 40.0);
// create and configure the label
UILabel *label = [[UILabel alloc] initWithFrame:frame];
label.text = [NSString stringWithFormat:#"%1.0f", row * cols + col + 1.0];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor lightGrayColor];
// add it to my scrollview
[self.scrollView addSubview:label];
}
}
}
I'm just filling my scroll view with 20 different text labels (and colored the background of the scrollview differently from the labels so I could see them), but it demonstrates the basic idea.
Please if you have added Paging in XIB then remove it...I think it will help you.

Horizontal UIScrollView and hundreds of thumbnail images in iOS?

I need to create a horizontal UIScrollView which to hold hundreds of thumbnail images, just like a slide of thumbnails.
For example, there will be 10 thumbnails showing in a single screen, each of them are horizontally adjacent to each other.
My problem is that I don't know how to make a horizontal UIScrollView to hold the multiple thumbnails which showing at the same time ?
A sample photo is as below. See the bottom part of the screen.
Thanks.
You can add all the thumbnails programatically to your scrollview and use the setContentSize method of UIScrollView. you have to pass 2 values in contentOffset. 1 for width and 1 for height. Please follow link to explore more on this. If you need further help please leave a comment.
Hope it helps.
Please consider Following example.
- (void)setupHorizontalScrollView
{
scrollView.delegate = self;
[self.scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setCanCancelContentTouches:NO];
scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView.clipsToBounds = NO;
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
NSUInteger nimages = 0;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; nimages++) {
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", (nimages + 1)];
UIImage *image = [UIImage imageNamed:imageName];
if (tot==15) {
break;
}
if (4==nimages) {
nimages=0;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
[scrollView addSubview:imageView];
[imageView release];
cx += imageView.frame.size.width+5;
tot++;
}
self.pageControl.numberOfPages = nimages;
[scrollView setContentSize:CGSizeMake(cx, [scrollView bounds].size.height)];
}
I suggest you to look at nimbus
Check out bjhomer's HSImageSidebarView project. It lets you load a scrollview horizontally or vertically and load in the images. Super easy to implement.
First of all, at storyboard drag and drop the scroll view and make the outlet of scrollview named scrollView. Two array one is mutable and one is immutable.
#property(nonatomic,strong)IBOutlet UIScrollView *scrollView;
#property(nonatomic,strong)NSMutableArray *images;
#property(nonatomic,strong)NSArray *imagesName;
The immutable array only store the images which we want to show on the scroll view.Make sure UIscrollview delegate is defined.
In viewcontoller.m file in didload function do following code:
imagesName = [[NSArray alloc]initWithObjects:#"centipede.jpg",#"ladybug.jpg",#"potatoBug.jpg",#"wolfSpider.jpg", #"ladybug.jpg",#"potatoBug.jpg",#"centipede.jpg",#"wolfSpider.jpg",nil];
// mutable array used to show the images on scrollview dynamic becaus after one
// image when scroll other will come
images = [[NSMutableArray alloc]init];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
//the loop go till all images will load
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
// make the imageview object because in scrollview we need image
img.frame = CGRectMake(5+xOffset, 0, 160, 110);
// the offset represent the values, used so that topleft for each image will
// change with(5+xOffset, 0)and the bottomright(160, 110)
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
// The image will put on the img object
[images insertObject:img atIndex:index];
// Put the img object at the images array which is mutable array
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
//scroll view size show after 125 width the scroll view enabled
[scrollView addSubview:[images objectAtIndex:index]];
// set images on scroll view
xOffset += 170;
}
You can calculate content size width of the scrollview as width = number of images * size of each image. Then set contentSize of the scrollview to this width and the height that you want (scrollView.contentSize = CGSizeMake(width, height))

UIScrollView Rotation Issues

I have a scrollview that automatically generates a series of subviews containing imageviews. The concept is that it's pretty much a custom PDF style reader, where each page is loaded in to an imageview as an image. There are three "layers" - the outer UIScrollView, the automatically generated subViews and the imageview inside the subview.
I've been having some trouble with this, I've asked the question previously, but unfortunately, the core of my question was in the wrong place. Here is my second attempt:
On rotate, everything is rotated as needed. Unfortunately, this is what I'm getting:
Obviously, I would like Image 1 to be centred and for there to be no hint of image 2 until you flick the view across.
Here is my code for setting up the view:
- (void)loadView {
[self setUpView];
}
- (void)setUpView {
//INITIALISE PAGING SCROLL VIEW
CGRect pagingScrollViewFrame = [[UIScreen mainScreen] bounds];
pagingScrollViewFrame.origin.x -= 10;
pagingScrollViewFrame.size.width += 20;
pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
pagingScrollView.contentMode = UIViewContentModeScaleAspectFit;
//CONFIGURE PAGING SCROLL VIEW
pagingScrollView.pagingEnabled = YES;
pagingScrollView.backgroundColor = [UIColor blackColor];
pagingScrollView.contentSize = CGSizeMake(pagingScrollViewFrame.size.width*7, pagingScrollViewFrame.size.height);
//ACTIVATE PAGING SCROLL VIEW
self.view = pagingScrollView;
//ADD PAGES TO SCROLL VIEW
for (int i = 0; i < 7; i++){
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[self configurePage:page forIndex:i];
[pagingScrollView addSubview:page];
}
}
How do I re-define the size of the frame? What function should I call etc. How do I centre the image?
I'm new to this so I apologise for the ambiguity of my question.
Cheers
I've figured it out. It was the Frame of the individual pages that I needed to change, so (with the help gained from another question) I wrote a re-orient method.
The first step to this came because I figured out that I needed to iterate through and re-size each frame for each of my pages and then re-apply the frames to the view. For this to happen I needed to create an array which I would fill in the For Loop above. I have 7 total pages.
Then I could (on rotate) call the below re-orient method to re-size each view:
- (void) reOrient{
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation)){
CGRect f;
int i = 0;
for (ImageScrollView *page in pageArray) {
f = page.frame;
f.size.width = 320;
f.origin.x = (f.size.width * i);
page.frame = f;
pagingScrollView.contentSize = CGSizeMake(f.size.width * 7, 480);
i++;
}
}else{
CGRect f;
int i = 0;
for (ImageScrollView *page in pageArray) {
f = page.frame;
f.size.width = 480;
f.origin.x = (f.size.width * i);
page.frame = f;
pagingScrollView.contentSize = CGSizeMake(f.size.width * 7, 480);
i++;
}
}
}
This worked for me:
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
//get current page based on offset before rotation
NSInteger currentPage = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
//set anticipated scrollview content size by switching width and height (they will be switched after rotation)
[self.scrollView setContentSize:CGSizeMake(totalPages * self.view.bounds.size.height, self.view.bounds.size.width)];
//set the anticipated content offset based on height before rotation
[self.scrollView setContentOffset:CGPointMake(currentPage * self.scrollView.bounds.size.height, 0.0f)];
}
Basically I am setting the content offset for the new scrollview content size before rotating (I'm switching height and width for the anticipated height and width).
This worked for me when rotating a full-screen paged UIScrollView.
All of the paged subviews have the following autoresizing mask so that they neatly fall into place after rotation:
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin