Resize UIScrollView - iphone

I am creating an app like the "PageControl" from apple.
An scrollview with another scrollview inside for each page.
UIScrollView * scrollForPage = [[UIScrollView alloc]init];
scrollForPage.pagingEnabled = NO;
scrollForPage.contentSize = CGSizeMake(mainScroll.frame.size.width, 200 + mainScroll.frame.size.height+150);
scrollForPage.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollForPage.clipsToBounds = YES;
CGRect frame = mainScroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
CGRect frame2 = frame;
frame2.origin.x = 0;
frame2.origin.y = 0;
controller.view.frame = frame2;
scrollForPage.frame = frame;
[scrollForPage addSubview:controller.view];
[mainScroll addSubview:scrollForPage];
[scrollForPage release];
however my controllerĀ“s view have an dynamic size, how can I resize the "scrollForPage" to have the same size??
Thanks

The code to detect the size of a view is this:
CGRect screenSize = [[UIScreen mainScreen] bounds];
Just make the frame of the scroll view equal to screenSize, and call the above line often.

Related

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

UITableview not placed correctly in uiscrollview

I have a UIscrollView that uses a pagecontrol. I'm adding tables to the scrollview in code. The tableviews should have a padding of 20px on each side. So I do the following.
CGRect frame = self.scrollView.bounds;
frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
frame.origin.x = (320 * page)+20;
NSLog(#"orgin x is %d",(320 * page)+20);
NSLog(#"orgin x is %f",frame.origin.x);
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.delegate = self;
tableStage.dataSource = self;
tableStage.tag = page;
tableStage.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:tableStage];
[self.pageViews replaceObjectAtIndex:page withObject:tableStage];
But I'm getting the following result.
Like you can see the next tableview is always moving something more to the left. Any idea how I can fix this?
Try this
CGRect frame = self.scrollView.bounds;
float offset=20;
frame.size.width = frame.size.width-(2*offset);
frame.origin.x = (320 * page)+offset;
frame.origin.y = 0;
UITableView *tableStage = [[UITableView alloc]initWithFrame:frame];
and no need for this line
tableStage.contentMode = UIViewContentModeScaleAspectFit;
CGRect frame = self.scrollView.bounds;
frame.size.width = 280.0f;
frame.size.height = self.scrollView.bounds.size.height;
frame.origin.x = (self.scrollView.bounds.size.width * page)+(self.scrollView.bounds.size.width - (frame.size.width/2));
frame.origin.y = 0;
If you set the content size properly and the location of each new table view it will work perfectly.Also enable the paging.

Gap between subviews of UIScrollView

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

How to slide a UIView right above a UITabBar

I have a UIView that I want to slide from behind a UITabBar to be position right on top of it.
This doesn't work. My view does not appear.
- (void)showNotificationBar
{
CGRect frame = CGRectMake(0, 500, 320, 32);
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame) - frame.size.height;
notificationBar.frame = frame;
[self.parentViewController.tabBarController.tabBar.superview insertSubview:notificationBar
belowSubview:self.parentViewController.tabBarController.tabBar];
[UIView animateWithDuration:0.5 animations:^{
CGRect frame = notificationBar.frame;
frame.origin.y = CGRectGetMaxY(self.parentViewController.tabBarController.tabBar.frame);
notificationBar.frame = frame;
}];
}
Initialize frame.origin.y like this:
frame.origin.y = self.tabBarController.tabBar.frame.origin.y;
In the animations block, set it like this:
frame.origin.y -= frame.size.height;
If you want it to show in every view, you could do to things: either show it at the bottom of every view or show it in the application's window. I personally like this second approach better because it helps avoiding duplicate code:
CGFloat notificationBarHeight = 40.0f;
UIView *notificationBar = [[UILabel alloc]initWithFrame:CGRectMake(0, self.tabBarController.tabBar.frame.origin.y - notificationBarHeight, 320, notificationBarHeight)];
[self.window insertSubview:notificationBar atIndex:[[self.window subviews]count]];
[self.view insertSubview:notificationView atIndex:1]; worked for me...

UIScrollView paging load from nib just 1 NIB show

i'm new on object-C, i want show some NIB file to UIScrollView with paging, i'm do it but just 1 NIB showing, other nib not show, the sample i make 2 page on each page have NIB, this is code :
bbottompageused = NO;
CGRect frame;
int tview=2;
mycontact = [[MyContact alloc] initWithNibName:#"MyContact" bundle:nil];
myphoto = [[MyPhoto alloc] initWithNibName:#"MyPhoto" bundle:nil];
frame.origin.x = self.midleScroll.frame.size.width * 1;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
[self.midleScroll addSubview:mycontact.view];
frame.origin.x = self.midleScroll.frame.size.width * 2;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
[self.midleScroll addSubview:myphoto.view];
self.midleScroll.contentSize = CGSizeMake(self.midleScroll.frame.size.width * tview, self.midleScroll.frame.size.height);
self.midlePage.currentPage = 0;
self.midlePage.numberOfPages = tview;
Have anyone suggestion for solve this ?
Thanks all,
assign the frame that you have to your views (myContact, myPhoto) and then add them to the scroll view.
You configure the frame but you don't assign this frame to myContact.view and myPhoto.view.
Hence, both of the myContact.view.frame and myPhoto.view.frame remain unchanged.
Do the following:
bbottompageused = NO;
CGRect frame;
int tview=2;
mycontact = [[MyContact alloc] initWithNibName:#"MyContact" bundle:nil];
myphoto = [[MyPhoto alloc] initWithNibName:#"MyPhoto" bundle:nil];
frame.origin.x = self.midleScroll.frame.size.width * 1;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
// Assign the frame
myContact.view.frame = frame;
[self.midleScroll addSubview:mycontact.view];
frame.origin.x = self.midleScroll.frame.size.width * 2;
frame.origin.y = 0;
frame.size = self.midleScroll.frame.size;
// Assign the frame
myPhoto.view.frame = frame;
[self.midleScroll addSubview:myphoto.view];
self.midleScroll.contentSize = CGSizeMake(self.midleScroll.frame.size.width * tview, self.midleScroll.frame.size.height);
self.midlePage.currentPage = 0;
self.midlePage.numberOfPages = tview;