I am trying to add the UIButton to UIScrollView.
I have added the UIImageView with background image *(size: 320 * 620)*.
Then I added this UIImageView to UIScrollView, and It works fine.
But now I want to add UIButton at position at : (60, 500); (below screen that would appear after scrolling).
I have tried following code, but the button is added on UIView not on scrollview. Button is not getting displayed on the top.
Code :
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden = TRUE;
UIImageView *tempImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"SingleModelVar.png"]];
self.imageView = tempImageView;
[tempImageView release];
imageView.userInteractionEnabled = YES;
scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; //fit to screen
//scrollView.delaysContentTouches = NO;
scrollView.contentSize = CGSizeMake(320, imageView.frame.size.height); //imageView.frame.size.height=620 here
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 0.75;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
//The Problem begins ..........
btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
[scrollView addSubview:btnStartDate];
//[self.view addSubview:btnStartDate];
btnEndDate=[[UIButton alloc] initWithFrame:CGRectMake(60,530,100,20)];
[scrollView addSubview:btnEndDate];
//[self.view addSubview:btnEndDate];
[scrollView addSubview:imageView];
[[self view] addSubview:scrollView];
}
it is not displayed at the top because you added them before you added the imageView.
Put [scrollView addSubview:imageView]; before btnStartDate=[[UIButton alloc] initWithFrame:CGRectMake(60,500,100,20)];
Related
I have a few buttons that each load a different image a UIScrollView, you can zoom in on these images. What happens is that after selecting a new images the previous one is still there in the scrollview. How do I clean up the scroll view before opening a new image in there?
This is the code so far:
- (UIView *) viewForZoomingInScrollView: (UIScrollView *)scrollView{
return imgView;
}
-(IBAction)buttonClicked: (UIButton *)sender{
switch (sender.tag) {
case 1:{
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
scrollView.contentSize = CGSizeMake(imgView.frame.size.width, imgView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = 1.0;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
[scrollView addSubview:imgView];
}
}
}
You keep adding new subviews without removing the previous ones. You could just replace the image content in the existing imageView instead of making a new one.
Replace this:
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
self.imgView = myImageView;
[myImageView release];
with
self.imageView.image = [UIImage imageNamed:#"Gath.jpg"];
and remove this
[scrollView addSubview:imgView];
I assume you have linked the imageView to self.imageView elsewhere, such as in the storyboard, or when you create the scrollView.
you can try this,
[myImageView removeFromSuperView];
myImageView=nil;
myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Gath.jpg"]];
I don't understand why the scrollview isn't working, because I followed advices from tutorials.
I created the UIScrollView that contains an UIImageView and I also added the method
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgv;
}
and the UIScrollViewDelegate in the interface, but it isn't working.
Here is the code for the UIScrollView:
imgv = [[UIImageView alloc]
initWithFrame:CGRectMake(0, 0, 1000, 500)];
imgv.image = [UIImage imageWithData:retrievedData];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 800, 800)];
scrollView setContentSize:CGSizeMake(imgv.image.size.width, imgv.image.size.height)];
scrollView.maximumZoomScale = 4;
scrollView.minimumZoomScale = 1;
[scrollView setScrollEnabled:YES];
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(hideImageView)];
[scrollView addSubview:imgv];
[view addSubview:scrollView];
I would be thankful, if someone could figure out what the problem could be.
You need to return imgv in your delegate method viewForZoomingInScrollView::
- (UIView *) viewForZoomingInScrollView:(UIScrollView *) view {
return imgv;
}
You have to add the imageView as a subview to the scrollview
[scrollView addSubview:imgv];
I believe you are forgetting to set the frame of your imageView properly
self.imageView.frame=CGRectMake(0, 0, self.imageView.image.size.width, self.imageView.image.size.height);
I am trying to add various UIImages under UIImageView and allow them to scroll with UIScrollView. I am not sure how to add various images under UIImageView and let them scroll.
Below is my code which adds an image on UIImageView and make it scrollable.
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:#"ae.jpg"];
imageView = [[UIImageView alloc]initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
imageView.contentMode = (UIViewContentModeScaleAspectFit);
imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
imageView.backgroundColor = [UIColor clearColor];
UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView.contentMode = (UIViewContentModeScaleAspectFit);
scrollView.contentSize = CGSizeMake(image.size.width,960);
scrollView.pagingEnabled = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.alwaysBounceVertical = NO;
scrollView.alwaysBounceHorizontal = NO;
scrollView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
scrollView.maximumZoomScale = 2.5;
scrollView.minimumZoomScale = 1;
scrollView.clipsToBounds = YES;
[scrollView addSubview:imageView];
[image release];
[imageView release];
[self.view addSubview:scrollView];
}
The idea is basically simple. Let's assume you want to place 3 images in UIScrollView.
Each of images is 300x300. In this case you'll have scroll view with frame:
scrollView.contentSize = CGSizeMake(image.size.width,900);
For every image you must have it's UIImageView with proper frame:
imgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, 300, 300)];
imgView2 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 300, 300, 300)];
imgView3 = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 600, 300, 300)];
imgView1.image = [UIImage imageNamed:#"ProperName.png"];
...
(pay attention to the yOrigin (2nd value in CGRectMake))
and then as you did:
[scrollView addSubview:imgView1];
[scrollView addSubview:imgView2];
[scrollView addSubview:imgView3];
[imgView1 release];
[imgView2 release];
[imgView3 release];
Of course, it's a brief code, you'll optimize it ;)
HI,
I develop an application in which I want to implement the splash screen, on that splash screen I want to bind the scrollView and UIImage. My code as follow,
-(void)splashAnimation{
window = [[UIWindow alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
//scrollView = [[UIScrollView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
scrollView = [[UIScrollView alloc] initWithFrame:[window bounds]];
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
[scrollView setDelegate:self];
//[scrollView release];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self splashAnimation];
[self initControllers];
[window addSubview:[mainTabBarController view]];
[window makeKeyAndVisible];
}
On my given code the one blank window comes up and stay on.
I want to on that blank screen bind my splash.png.
****The Above problem is solved****
My current code is
scrollView.pagingEnabled = NO;
scrollView.bounces = NO;
UIImage *image = [UIImage imageNamed:#"splash.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.userInteractionEnabled = NO;
[scrollView addSubview:imageView];
scrollView.maximumZoomScale = 4.0f;
scrollView.minimumZoomScale = 1.0f;
CGRect rect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:rect animated:YES];
[scrollView setDelegate:self];
[window addSubview:scrollView];
[window makeKeyAndVisible];
I want to zoom the particular part of scrollView.
RajB - for your zooming of the scrollView do this:
CGRect zoomRect = CGRectMake(119, 42, 208, 166);
[scrollView zoomToRect:zoomRect animated:YES];
Hope this helps!
You create a UIScrollView but never add it to the view hierarchy, therefore it will never get displayed. Call [window addSubview:scrollView], and then don't forget to release it.
If you are using a MainWindow.xib in your project, your window is created for you, you do not need to create your own.
Use [[UIScreen mainScreen] instead of CGRect(0, 0, 320, 420) <- I also believe you meant "480"
After setting up your splash animation, you call [window addSubview:[mainTabBarController view]]. Even after adding your scrollview as previously mentioned, this will then become the topmost and therefore visible view.
Delay the [window addSubview:[mainTabBarController view]] until after your splash animation completes.
Help me.
addSubview doesn't work.
I want to add "ContentView" on scrollView.
But "ContentView" doesn't appear on screen.
"ContentView" is UIView.
When I change to self.view=scrollView from [self.view addSubview:scrollView],
addSubview doesn't work.
Please teach me how to add UIView on this screen!!
- (void)viewDidLoad {
[super viewDidLoad];
scrollViewMode = ScrollViewModeNotInitialized;
mode = 1;
imageViewArray = [[NSMutableArray alloc] init];
mainStatic = [[MainStatics alloc] init];
[mainStatic setSetting];
NSString* path = [[NSBundle mainBundle] pathForResource:#"Files" ofType:#"plist"];
NSArray* dataFiles = [NSArray arrayWithContentsOfFile:path];
kNumImages = [dataFiles count];
CGRect frame = [UIScreen mainScreen].applicationFrame;
scrollView = [[touchClass alloc] initWithFrame:frame];
scrollView.delegate = self;
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
[scrollView setBackgroundColor:[UIColor blackColor]];
[scrollView setDelegate:self];
scrollView.delaysContentTouches=NO;
scrollView.userInteractionEnabled = YES;
[scrollView setCanCancelContentTouches:NO];
NSUInteger i;
for (i=0;i<[dataFiles count];i++)
{
UIImage* image = [UIImage imageNamed:[dataFiles objectAtIndex:i]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.userInteractionEnabled = NO;
CGRect rect = imageView.frame;
rect.size.height = 480;
rect.size.width = 320;
imageView.frame = rect;
imageView.tag = i+1;
[imageViewArray addObject:imageView];
}
self.view = scrollView;
[self setPagingMode];
UIView* contentView;
CGRect scRect = CGRectMake(0, 436, 320, 44);
contentView = [[UIView alloc] initWithFrame:scRect];
[contentView addSubview:toolView];
[self addSubview:contentView];
}
I used image array in following function.
toolView is UIView with UIToolbar.
So I want to add toolbar to screen.
Yes, touchClass is subclass of UIScrollView.
1.I see.I forgot release this.Thank you.
2.OK. I modified this.But "ContentView" didn't apear.
are there another reason?
- (void)setPagingMode {
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imageViewArray){
[scrollView addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView.pagingEnabled = YES;
scrollView.showsVerticalScrollIndicator = scrollView.showsHorizontalScrollIndicator = YES;
scrollView.contentSize = CGSizeMake(pageSize.width * [imageViewArray count], pageSize.height);
scrollView.contentOffset = CGPointMake(pageSize.width * currentPage, 0);
scrollViewMode = ScrollViewModePaging;
}
Source isn't clear.
You never use array with UIImageView. There is no info what is toolView etc...
Please provide more detailed source. Is touchClass a subclass of UIScrollView?
What I noticed in your code:
You don't release image and imageView. All this images imageViews will be leaked. At the end of loop you should add [imageView release]; and [image release];
I don't think that it is good idea to send [self addSubview:contentView]; Try to use [self.view addSubview:contentView];