This question already has answers here:
UIScrollView not scrolling
(27 answers)
Closed 7 years ago.
I'm adding subViews to a programmatically added UIScrollView, yet when the subViews go off the screen the scrollview isn't scrolling. Any Ideas?
I need to update the content size of the scrollview more than once.
First I add the scrollView itself as subView of the view, like so:
[super viewDidLoad];
[self addScrollView];
[self APISimpleDemo];
}
#pragma mark Add Scroll View
-(void)addScrollView {
//Add Scroll View
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
CGRect scrollFrame;
scrollFrame.size.height = self.view.frame.size.height - (navHeight - 150); //set height minus nav bar and top section
scrollFrame.size.width = self.view.frame.size.width;
scrollFrame.origin.y = 180;
scrollFrame.origin.x = 0;
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
NSLog(#"TableView Frame %#", NSStringFromCGRect(scrollFrame));
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
}
The NSLog output is: {{0, 180}, {375, 773}}
Next the code runs through and the end of each test (in this example 2) a tableView is added to the newly added scrollView. Like so...
-(void)addSpeedTable {
CGRect frame = self.tableView.frame;
frame.size.height = (40 * resultHeadings.count)+35;
frame.size.width = self.view.frame.size.width;
//If more than one table has been shown increase Y axis height for view
if(tablesDisplayed > 0) {
NSLog(#"Tables displayed greater than zero");
viewHeight = viewHeight + frame.size.height;
}
frame.origin.y = viewHeight;
frame.origin.x = 0;
self.tableView = [[UITableView alloc] initWithFrame:frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 40;
self.tableView.sectionFooterHeight = 0;
self.tableView.sectionHeaderHeight = 22;
self.tableView.scrollEnabled = NO; // Disable ScrollView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(#"TableView Frame %#", NSStringFromCGRect(frame));
[self.scrollView addSubview:self.tableView];
tablesDisplayed++;
}
As I needed the content size of the scrollView to increase as TableViews were added to the ScrollView I created a method that updates the scrollView content size every time a table is added:
The code now looks like:
-(void)addScrollView {
//Add Scroll View
CGFloat navHeight = self.navigationController.navigationBar.frame.size.height; //get height of nav bar
CGRect scrollFrame;
scrollFrame.size.height = self.view.frame.size.height - navHeight - 150; //set height minus nav bar and top section
scrollFrame.size.width = self.view.frame.size.width;
scrollFrame.origin.y = 180;
scrollFrame.origin.x = 0;
self.scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
}
#pragma mark Update Scroll View Content Size
-(void)updateScrollSize {
//View height is set in the add table method depending on the size of the table
CGFloat scrollHeight = 400 + viewHeight;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, scrollHeight);
}
-(void)addSpeedTable {
CGRect frame = self.tableView.frame;
frame.size.height = (40 * resultHeadings.count)+50;
frame.size.width = self.view.frame.size.width;
//If more than one table has been shown increase Y axis height for view
if(tablesDisplayed > 0) {
NSLog(#"Tables displayed greater than zero");
viewHeight = viewHeight + frame.size.height;
}
frame.origin.y = viewHeight;
frame.origin.x = 0;
[self updateScrollSize]; //Update scrollView contentSize when adding new table to view
self.tableView = [[UITableView alloc] initWithFrame:frame];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"newCell"];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
self.tableView.rowHeight = 40;
self.tableView.sectionFooterHeight = 0;
self.tableView.sectionHeaderHeight = 22;
self.tableView.scrollEnabled = NO; // Disable ScrollView
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView = _tableView;
NSLog(#"TableView Frame %#", NSStringFromCGRect(frame));
[self.scrollView addSubview:self.tableView];
tablesDisplayed++;
}
You are not setting scrollView.contentSize.
Related
I have a UIScrollView to pan some photos. After loading the view, when I touch the page (anywhere within) it makes my insideview (UIImageView) to goes up.
Here is the photo when it loads (the green is the inside UIView, the black the UIScrollView).
Fist load view:
After the touch:
Here is the code that generates it:
Adding the view to the scrollview:
- (void)loadPage:(NSInteger)page {
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
// 3
NSURL *eventImageURL = [NSURL URLWithString:[[self.pageImages objectAtIndex:page] imageURL]];
UIImageView *newPageView = [[UIImageView alloc]init];
[newPageView setImageWithURL:eventImageURL placeholderImage:nil usingActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame=frame;
newPageView.tag=page;
newPageView.userInteractionEnabled=NO;
newPageView.backgroundColor=[UIColor greenColor];
newPageView.clipsToBounds=YES;
[self.scrollView addSubview:newPageView];
}
The scroll view setup:
- (void)viewDidLoad {
[super viewDidLoad];
CGSize scrollableSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
// Do any additional setup after loading the view from its nib.
// 1
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
self.scrollView.minimumZoomScale=1.0;
self.scrollView.maximumZoomScale=2.0;
self.scrollView.clipsToBounds=YES;
self.scrollView.contentSize=CGSizeMake(1280, 960);
self.scrollView.tag=10;
[self.scrollView setUserInteractionEnabled:YES];
[self.scrollView setMultipleTouchEnabled:YES];
}
Found it.
On viewdidload I set:
self.automaticallyAdjustsScrollViewInsets = NO;
And everything works fine.
I have a UIScrollView in my storyboard that scrolls vertically. Inside of this scrollView, I have programmatically created a horizontal UIScrollView.
The vertical scrollView is called scroller. The horizontal UIScrollView is called scrollInfo. For some reason i am unable to register when the horizontal scrollInfo is being used. Any ideas on why this isn't working?
This is in my viewDidLoad:
scrollInfo = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
155,self.view.frame.size.width, self.view.frame.size.height/4)];
//This will create a scrollview of device screen frame
scrollInfo.scrollEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0,
self.view.frame.size.width, self.view.frame.size.height/4)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5
alpha:1];
[scrollInfo addSubview:awesomeView];
}
// 3 views added horizontally to the UIScrollView by using xOrigin.
scrollInfo.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews,
self.view.frame.size.height/4);
[scroller addSubview:scrollInfo];
//Add scrollview to viewcontroller
scrollInfo.pagingEnabled = YES;
pageControl = [[UIPageControl alloc] init]; //SET a property of UIPageControl
pageControl.frame = CGRectMake(0,200,self.view.frame.size.width,
self.view.frame.size.height/8);
pageControl.numberOfPages = 3; //as we added 3 diff views
pageControl.currentPage = 0;
[scroller addSubview:pageControl];
Here is my scrollViewDidScroll method:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([mainTableView isEqual: scrollView]) {
if (mainTableView.contentOffset.y > 0) {
// yourTableView is not on top.
NSLog(#"not top");
}
else {
// yourTableView is already on top.
NSLog(#"is top");
[UIView animateWithDuration:0.5f delay:0
options:UIViewAnimationOptionCurveLinear animations:^{
scroller.contentOffset = CGPointMake(0, 0);
} completion:NULL];
}
}
else {
CGFloat pageWidth = self.view.frame.size.width;
int page = floor((scrollInfo.contentOffset.x - pageWidth / 2 ) / pageWidth) +
1; //this provide you the page number
pageControl.currentPage = page;// this displays the white dot as current page
NSLog(#"page");
}
}
Looks like you forget to connect UIScrollViewDelegate to your scrollviews. After doing this you better operate with scrollview.tag to recognize which scrollview calling delegate
I don't know why I always get stuck with scrollview's position issue... I just want to display scrollview after a label at 5 px distance, but its taking extra margin on top. I am using storyboard - xcode 5. Scrollview positions correctly when I test with iOS 7, but creates problem in iOS 6 simulator.
Note: I am not using any constraints.
In storyboard,
iOS 6 snapshot
iOS 7 snapshot
Simple piece of code:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
if([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
if([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)])
self.automaticallyAdjustsScrollViewInsets = NO;
//add subviews to scrollview
_latestNewsLabel.layer.borderColor = [UIColor greenColor].CGColor;
_latestNewsLabel.layer.borderWidth = 1.0;
_latestNewsScroll.layer.borderColor = [UIColor blueColor].CGColor;
_latestNewsScroll.layer.borderWidth = 1.0;
self.view.backgroundColor = [UIColor whiteColor];
_categoriesList = [NSArray arrayWithObjects:#"US World", #"World", #"Business", #"Political", #"Entertainment", #"Sports", #"IT", nil];
selectedIndex = -1;
[_collectionView registerClass:[CustomCollectionCell class] forCellWithReuseIdentifier:#"Category"];
CollectionViewLayout *collLayout = [[CollectionViewLayout alloc] init];
[self.collectionView setCollectionViewLayout:collLayout];
}
-(void) viewWillAppear:(BOOL)animated{
CGRect newFrame = _latestNewsScroll.frame;
newFrame.origin.y = _latestNewsLabel.frame.origin.y + _latestNewsLabel.frame.size.height + 5;
_latestNewsScroll.frame = newFrame;
newFrame = _collectionView.frame;
newFrame.origin.y = _latestNewsScroll.frame.origin.y + _latestNewsScroll.frame.size.height + 10;
_collectionView.frame = newFrame;
}
I'm creating a "how-to"view where I show the user 5-6 pictures on how to use the app. I want it to be like a container inside the real view. Also I want it to have a transition with swipe and a page control. Something like the AppStore has on the pictures with screenshots of an app if you know what I mean?
Is there an easy way to do this? All help highly appreciated!
here a simple code, but you can customize it with loop, animation or what you want to do ;) ...
- (void)viewDidLoad
{
[super viewDidLoad];
//init scollview
scrollView = [[UIScrollView alloc] initWithFrame:myBounds];
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
//Ajout des covers classiques
for (int i = 0; i < [myCovers count]; i++) {
CGRect frame;
frame.origin.x = scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = scrollView.frame.size;
//Vue 1
UIView *subview1 = [[UIView alloc] initWithFrame:frame];
[subview1 addSubview:[myCovers objectAtIndex:i]];
[scrollView addSubview:subview1];
}
//Content Size Scrollview
scrollViewBack.contentSize = CGSizeMake(scrollViewBack.frame.size.width * ([myCovers count]), scrollViewBack.frame.size.height);
[self.view addSubview:scrollViewBack];
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width*([myCovers count]), scrollView.frame.size.height);
[self.view addSubview:scrollView];
//Page Control
pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, scrollView.frame.size.height - PAGECONTROL_HEIGTH - myBaseline, scrollView.frame.size.width, PAGECONTROL_HEIGTH)];
pageControl.numberOfPages = [myCovers count];
[self.view addSubview:pageControl];
}
#pragma mark -
#pragma mark Params setting
- (void) setObjects:(NSArray *)covers {
myCovers = [[NSArray alloc] initWithArray:covers];
}
#pragma mark -
#pragma mark Scrollview delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {
CGFloat pageWidth = scrollView.frame.size.width;
NSInteger offsetLooping = 1;
int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + offsetLooping;
pageControl.currentPage = page % [myCovers count];
}
You can create UIImageView for the 'n' number of images you have.
Add this images on UIScrollView. Keep the size of your scrollview same as your UIImageView size.
Most important thing, enable the Paging property of UIScrollView
It should give you the look same as AppStore screen
shot screen.
I am creating an iPAD app. I have got a MAIN VIEW controller which covers the whole screen. And I have another class DROP-DOWN Class which i will be using to mimic the functionality of a drop-down as in Windows OS components. For that I am using a simple button, a scroll view and a few labels. I instantiate an object of that class and use it in my MAIN VIEW controller. To create the effect of a drop down, I alter the instance's views frame on the touch up of the button in toggle mode.(Click once, frame increased, click again frame decreased).
The creation part was fine, and the function to toggle to worked fine when implemented on a single instance of the DROP DOWN object.
But the problem comes when I instantiate these objects thru a for loop. The views seem to just vanish on button click.
Here is the code within the dropdowns.
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(10,54,100,50)];
scrollView.contentSize = CGSizeMake(50, 100);
for (int i = 0; i< 10; i++)
{
UILabel *newLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 10*i, 100, 10)];
[newLable setFont:[UIFont boldSystemFontOfSize:10]];
newLable.text = [NSString stringWithFormat:#"Testing %i",i];
[scrollView addSubview:newLable];
}
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
}
-(IBAction)btnClicked:(id)sender
{
if (!isOpen) {
oldRect = self.view.frame;
CGRect newFrame = self.view.frame;
newFrame.size = CGSizeMake(190, 200);
NSLog(#"New Frame - %f,%f,%f,%f",self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.height,self.view.frame.size.width);
self.view.frame = newFrame;
}
else {
self.view.frame = oldRect;
NSLog(#"Old Frame - %f,%f,%f,%f",self.view.frame.origin.x,self.view.frame.origin.y,self.view.frame.size.height,self.view.frame.size.width);
}
isOpen = !isOpen;
}
And here is how this is called from the main view.
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(drawTheFilters)];
}
-(void)drawTheFilters
{
for (int i = 0; i<5 ; i++) {
DropDownView *dropView = [[DropDownView alloc]initWithNibName:#"DropDownView" bundle:[NSBundle mainBundle]];
CGRect newFrame = dropView.view.frame;
newFrame.origin = CGPointMake(200*i, 200);
dropView.view.frame = newFrame;
[self.view addSubview:dropView.view];
NSLog(#"Frame %i, %f,%f,%f,%f",i,dropView.view.frame.origin.x,dropView.view.frame.origin.y,dropView.view.frame.size.height,dropView.view.frame.size.width);
}
}