My problem : I have a white window and I don't understand why.
I tried some solutions but nothing works.
Thx for your help.
CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
[self.view addSubview: monScrollView];
for (int i = 0; i < 10; i++)
{
CGRect rectImage = CGRectMake(0,longueurDevice/2+i*30,320,30); // Définition d'un rectangle
CGRect rectLabel = CGRectMake(10,longueurDevice/2+i*30,320,30);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
imageView.image = [UIImage imageNamed:#"barreTest.png"];
[monScrollView addSubview: imageView];
UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
monLabel.text = #"Ceci est un label";
[monScrollView addSubview: monLabel];
}
Your UIlabel and UIImageView frame are seated out of visible bounds.
as example if you set
CGRect rectImage = CGRectMake(0,i*30,320,30);
CGRect rectLabel = CGRectMake(10,(i*30)+40,320,30);
you will be able to see them on the scrollview. Please set frame properly to see your UI components and also set content size of scroll view to make it scrolling. If your content size is bigger the scrollview frame, scroll view will be able to scroll,
Thanks for your answers.
Now it works. If you have any advice for me, it will be a pleasure to me.
int max = 10;
CGFloat largeurDevice = [UIScreen mainScreen].bounds.size.width;
CGFloat longueurDevice = [UIScreen mainScreen].bounds.size.height;
CGRect rectScrollView = CGRectMake(0,longueurDevice/2,largeurDevice,longueurDevice/2);
UIScrollView *monScrollView = [[UIScrollView alloc] initWithFrame:rectScrollView];
monScrollView.contentSize=CGSizeMake(largeurDevice,30*max);
[self.view addSubview: monScrollView];
for (int i = 0; i < max; i++)
{
CGRect rectImage = CGRectMake(0,i*30,largeurDevice,30); // Définition d'un rectangle
CGRect rectLabel = CGRectMake(10,i*30,largeurDevice,30);
CGRect rectBututton = CGRectMake(largeurDevice-20,longueurDevice/2+i*30,10,10);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rectImage];
imageView.image = [UIImage imageNamed:#"barreTest.png"];
[monScrollView addSubview: imageView];
UILabel *monLabel = [[UILabel alloc] initWithFrame: rectLabel];
monLabel.text = #"Ceci est un label";
[monScrollView addSubview: monLabel];
}
Related
i want to create paging scrollView with three UIViews. And than wants to add imageView as a subView to these UIViews. i cant figure it out how to calculate the frame of imageViews.
When i run the code without imageViews it works perfectly fine.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.delegate = self;
self.scrollView.frame = self.view.frame;
NSArray *colorsArray = [NSArray arrayWithObjects:[UIColor blueColor], [UIColor yellowColor],[UIColor greenColor], nil];
NSArray *imagesArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"123.png"],[UIImage imageNamed:#"13.png"],[UIImage imageNamed:#"12.png"],nil];
for (int i = 0; i < colorsArray.count; i++) {
CGRect frame;
frame.origin.x = self.view.frame.size.width * i;
frame.size = self.view.frame.size;
self.scrollView.pagingEnabled = YES;
UIView *view = [[UIView alloc] initWithFrame:frame];
view.backgroundColor = [colorsArray objectAtIndex:i];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.image = [imagesArray objectAtIndex:i];
[view addSubview:imageView];
[self.scrollView addSubview:view];
}
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * colorsArray.count, self.scrollView.frame.size.height);
self.pageControl.numberOfPages = 3;
self.pageControl.currentPage = 0;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = CGRectGetWidth(self.scrollView.bounds);
CGFloat pageFraction = self.scrollView.contentOffset.x / pageWidth;
self.pageControl.currentPage = roundf(pageFraction);
}
The frame for the image view needs to be relative to its superview (i.e. view) not the scroll view. To achieve the paging you desire change:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:view.frame];
to:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, view.frame.size.width, view.frame.size.height)];
I'm trying to create programmatically a UIScrollView containing a UIImageView. The image of the UIImageView should be scrolled vertically, my problem is that the UIScrollView is never added to my main View.
Here's the code
#property (strong, nonatomic) UIImageView *advertiseView;
#property UIScrollView *sv;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_sv = [[UIScrollView alloc] init];
_advertiseView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,568)];
UIImage *image = [UIImage imageNamed:#"abc.png"];
_advertiseView.image = image;
_advertiseView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[_sv addSubview:_advertiseView];
_sv.contentSize = CGSizeMake(320,1136); // size of abc.png
[self.view addSubview:_sv];
}
What am i missing??
You don't set the ScrollView size. Try this:
_sv = [[UIScrollView alloc] initWithFrame:self.view.bounds];
int width = 0;
int height = 0;
for (int i=0; i<[menupicarray count]; i++)
{
NSString *menupicname=[NSString stringWithFormat:#"%#",[menupicarray objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] init ];
[imageView setImageWithURL:[NSURL URLWithString:menupicname]
placeholderImage:[UIImage imageNamed:#"NOmage.png"]];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.clipsToBounds = YES;
imageView.frame = CGRectMake( MenuView.frame.size.width*i,0, MenuView.frame.size.width, MenuView.frame.size.height-5);
[MenuView addSubview:imageView];
width = imageView.frame.size.width;
height = imageView.frame.size.height;
MenuView.contentSize = CGSizeMake(width*i+imageView.bounds.size.width, height);
}
I have set up a UIScrollView and loaded in pictures and set it equal to the view with some offset between the pictures. Can someone possibly explain what I might have done wrong? It shows the first picture just fine but wont let me scroll left and right to see the next ones. Do I need a gesture recognizer with the new XCode to make this work?
- (void)viewDidLoad
{
[super viewDidLoad];
int PageCount = 3;
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);
CGRect ViewSize = Scroller.bounds;
UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView setImage:[UIImage imageNamed:#"1.png"]];
[Scroller addSubview:ImgView];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView2 setImage:[UIImage imageNamed:#"2.png"]];
[Scroller addSubview:ImgView2];
ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);
UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
[ImgView3 setImage:[UIImage imageNamed:#"3.png"]];
[Scroller addSubview:ImgView3];
[self.view addSubview:Scroller];
}
#end
You need to manually set the contentSize of your ScrollView to the maximum rectangle which fits all the ImageViews you added. For example : Lets say you want to scroll right and left and you have 4 views with each view having width 100 and you have offset of 10 in x direction. Then after adding all the 4 views to your scrollView, you will have to make the contentSize as below :
scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );
This will make the scrollView scrollable and you will see all the views.
Try this
int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:#"1.png",#"2.png",#"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
[Scroller addSubview:ImgView];
Scroller.contentSize = CGSizeMake(width, 0);
width +=Scroller.frame.size.width;
xPos +=Scroller.frame.size.width;
}
Before using UIScrollView's zoom feature, everything is fine.
The code as bellow:
UIScrollView* scrollView = [[UIScrollView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[scrollView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[scrollView addSubview:wineView];
After adding zoom feature, code looks like:
UIScrollView* scrollView = [[UIScrollView alloc]init];
UIView* zoomView = [[UIView alloc]init];
for(int i=0; i < foodIntroCount; ++i) {
UIImage* image = [UIImage imageNamed:#"7_1.jpg"];
UIImageView * imgView = [[UIImageView alloc]initWithImage:image];
imgView.frame = CGRectMake(0, height*i, width, height);
[zoomView addSubView:imgView];
}
scrollView.contentSize = CGSizeMake(width, height*(foodIntroCount+1)) ;
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
scrollView.delegate = self;
scrollView.maximumZoomScale = 2.0;
_foodWineViewController = [[OHFoodWineViewController alloc]initWithCategoryData:_foodData];
UIView* wineView = _foodWineViewController.view;
wineView.frame = CGRectMake(0, height*(foodIntroCount), width, height);
[zoomView addSubView:wineView];
[scrollView addSubview:zoomView];
The problem is:
wineView displayed on the first page, not on the foodIntroCount+1 page.
Your code should consider the previous y position and height both if you want to add subview one after another vertically. So your code should look like below
imgView.frame = CGRectMake(0, height*i+y, width, height);
The code isn't tested though, try this..
i can't seem to use UIViewContentModeScaleAspectFit property to scale my imageView within my scrollview to fit the scrollview size.
- (void)loadView {
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.moilum.com/images/atomicongallery/setmenu/setmenu1.jpg"]]];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:applicationFrame];
[scrollView addSubview:imageView];
scrollView.contentSize = image.size;
scrollView.minimumZoomScale = 0.3;
scrollView.maximumZoomScale = 3.0;
scrollView.clipsToBounds = NO;
scrollView.delegate = self;
self.view = scrollView;
}
i have tried imageView.contentMode = UIViewContentModeScaleAspectFill as well. scrollView.contentmode = UIViewContentModeScaleAspectFill as well. All dont seem to work :(
any way out!?
Cheers!
The UIScrollView will not adjust its zoomScale if you don't implement the - (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView method on the UIScrollViewDelegate.
Something like this
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)sView
{
NSArray *subViews = [sView subviews];
if([subViews count] > 0){
return [subViews objectAtIndex:0];
}
return nil;
}
(But apparently not setting a delegate at all works too.)
Have you tried,
-(void)loadView
{
CGRect frame = CGRectMake(0, 0, scrollView.frame.size.width, scrolllView.frame.size.height);
imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:#"yourimage.png"];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[scrollView addSubView:imageView];
scrollView.contentSize = CGSizeMake( imageView.frame.size.width, imageView.frame.size.height);
}
I write this code from my iPad i think this should work,
Let me know if you have any problem.
Good luck.