UIViews are blank when added to UIScrollView - ios

I'm trying to add the Views of a ViewControllerClass to a ScrollView.
The problem is that only the first page is showing up, the other ones are blank.
This is my code
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[imageArray2 addObject:[[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Any advice?

You have to assign your calculate frame to your WeatherViewController's view on your loop.
Try this
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *myWeatherVC = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
myWeatherVC.view.frame = frame;
[imageArray2 addObject:myWeatherVC];
[self.scrollView addSubview:myWeatherVC.view];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);

Just assign the frame like this and it will work.
for (int i = 0; i < 8; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
WeatherViewController *thisView = [[WeatherViewController alloc] initWithNibName:#"WeatherViewController" picname:[NSString stringWithFormat:#"s%#.png", [[xmlParser.weatherdata.timedata objectAtIndex:i] iconSymbol]] degree:#"0°" weathertimedata:[xmlParser.weatherdata.timedata objectAtIndex:i] town:[xmlParser.weatherdata town]]
[thisView.view setFrame:frame];
[imageArray2 addObject:thisView];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 8, scrollView.frame.size.height);
for(WeatherViewController *iv in imageArray2)
{
[self.scrollView addSubview:iv.view];
}
Hope this will help you.

Related

How change the page control indicator on image slide inside scrollview using NSTimer

Here is the image slider which works fine. When dragging manually the image changes and indicator also changes but when i add animation only image changes automatically not the indicator.
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
#implementation DashboardViewController {
NSArray * animationArray;
}
#synthesize scroller = scroller;
#synthesize pageControl = pageControl;
-
(void) viewDidLoad {
[super viewDidLoad];
scroller.pagingEnabled = YES;
scroller.showsHorizontalScrollIndicator = NO;
scroller.delegate = self;
animationArray = [NSArray arrayWithObjects: [UIImage imageNamed: # "image1.jpg"],
[UIImage imageNamed: # "image2.jpg"],
[UIImage imageNamed: # "image3.jpg"],
[UIImage imageNamed: # "image4.png"],
[UIImage imageNamed: # "image5.jpg"],
nil
];
CGRect scrollFrame = CGRectMake(0, 0, self.view.frame.size.width, self.scroller.frame.size.height);
scroller.frame = scrollFrame;
self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * animationArray.count, self.scroller.frame.size.height);
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.scroller addSubview:imgView];
}
self.pageControl.currentPage = 0;
}
-
(void) scrollViewDidScroll: (UIScrollView * ) sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scroller.frame.size.width;
int page = floor((self.scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
} -
(void) scrollViewWillBeginDragging: (UIScrollView * ) scrollView {
pageControlBeingUsed = NO;
}
-
(void) scrollViewDidEndDecelerating: (UIScrollView * ) scrollView {
[self setIndiactorForCurrentPage];
} -
(void) setIndiactorForCurrentPage {
uint page = scroller.contentOffset.x / scroller.frame.size.width;
[pageControl setCurrentPage: page];
}
-
(IBAction) changePage {
// Update the scroll view to the appropriate page
CGRect frame;
pageControl.currentPage = animationArray.count;
frame.origin.x = self.scroller.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
[self.scroller scrollRectToVisible: frame animated: YES];
pageControlBeingUsed = YES;
}
-
(void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
self.scroller = nil;
self.pageControl = nil;
}
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
I think you have to set your current page inside your for loop for automatic change pagecontrol with image.
//move this up
self.pageControl.currentPage = 0;
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
self.pageControl.currentPage = i; //set i to current page
[self.scroller addSubview:imgView];
}

XCODE Obj-C add UiimageView programatically in a for

So i need to add programatically 3 images using a for loop this is my code , it's not correct, i just tryed.
for(int i = 1; i < 4; i++){
UIImageView *[i] = [[UIImageView alloc] initWithFrame:CGRectMake([i]*50, 50, 250, 250)];
[self.view addSubview:[i]];
}
where [i]; will be 1 , 2 and 3 any help would be appreciated. Thanks
You syntax is wrong. This is more what you need.
for (int i=0;i<3;i++){
UIImageView *image=[[UIImageView alloc] initWithFrame:CGRectMake(i*50, 50, 250, 250)];
[self.view addSubView:image];
}
However, this is not much use to you as your images are not referenced from anywhere so you can not populate or adjust them easily.
Change the variable name from [i] to some name like imageToAdd.
CGFloat xMarging = 0;
CGFloat imageOriginX = 0;
CGFloat imageOriginY = 0;
CGFloat imageWidth = 250;
CGFloat imageHeight = 250;
NSInteger numerOfImages = 3;
for (NSInteger i = 0; i < numerOfImages; i++){
[self.view addSubview:[[UIImageView alloc] initWithFrame:CGRectMake(xMarging + i * imageWidth,
imageOriginY,
imageWidth,
imgageHeight)]];
}
assuming you want them side by side as in [][][]

scrollRectToVisible not working in page view controller - iPhone sdks

I am using Xcode 6.1 and developing for iOS 8.0.
In my application I have a UIPageViewController. In that I have added a UIScrollView. In the UIScrollView I have 4 buttons. When I tap on a button I use this code.
long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
This(scrollRectToVisible:) is not working. Can anyone help me?
Thanks.
long btnValue = sender.tag;
float spaceing = 0;
if(btnValue == 1)
spaceing = self.scrollView.frame.size.width/4*btnValue;
else
spaceing = (self.scrollView.frame.size.width/2*btnValue) - self.scrollView.frame.size.width/4;
CGRect frame;
frame.origin.x = spaceing;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
self.scrollView.pagingEnabled = NO;
// Problem is here
frame.size = self.scrollView.frame.size; //scrollView.frame.size matters
set
frame.size = self.View.frame.size;
and if you are doing it by coding then please add your scrollview into your view
[self.view addSubview:yourScrollView];

CCScrollView does't receive touches (Cocos2d V 3.0)

I try to add CCScrollView with paging (cocos2d- iphone v3.0). But it's not working.
It doesn't call any delegate methods (for example scrollViewDidScroll:).
CCNode *cards = [CCNode node];
for (int i = 0 ; i < 3; i++) {
CCLabelTTF *label = [CCLabelTTF labelWithString:[NSString stringWithFormat:#"label %d", i] fontName:#"Arial" fontSize:24];
label.color = [CCColor redColor];
label.position = ccp(winSize.width * i + winSize.width * 0.5 , winSize.height * 0.5);
[cards addChild:label];
}
self.scrollView = [[CCScrollView alloc] initWithContentNode:cards];
self.scrollView.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentSize = CGSizeMake(3, 1);
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
self.scrollView.position = CGPointZero;
self.scrollView.anchorPoint = CGPointZero;
[self addChild:self.scrollView];
You actually need to set the contentSize of the contentNode of the scrollView as opposed to the contentSize of the scrollView.
In CCScrollView.h
#property (nonatomic,strong) CCNode* contentNode;
So you should replace this part of the code:
self.scrollView.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentSize = CGSizeMake(3, 1);
With this:
self.scrollView.contentNode.contentSizeType = CCSizeTypeNormalized;
self.scrollView.contentNode.contentSize = CGSizeMake(3, 1);

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

Resources