How to add two uiscrollviews as subview - iphone

After lot of research and working for days, I really need some guidance here. I am trying to add uiscrollview on top of each other. I have two UIScrollViews: ImageScrollView and MarkScrollView. I want to add MarkScrollView as subview to ImageScrollView. ImageScrollView is added to UIViewController MapImageViewController.
In ImageScrollView it consists of images which are in tiledLayer. In MarkScrollView, it consists of imageview which has a pointer image. But for some reason it cannot be added as subview to other view. I have added my code. If anybody can help me to figure out the error it will be really appreciated.
/** ImageScrollView.m It adds uiview on top of ImageScrollView***/
UIView *imageView = [[TileView alloc] initWithImageName:imageName size:imageSize];
[(TileView *)imageView setAnnotates:NO]; // ** remove this line to remove the white tile grid **
[self addSubview:imageView];
/** MarkScrollView.m It adds uiview with uiimages on top of MarkScrollView**/
UIImage *markerimage = [UIImage imageNamed:#"map.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage: markerimage];
imageView.image = markerimage;
[imageView setFrame:CGRectMake(100,100,50,50)];
imageview = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
[imageview setBackgroundColor:[UIColor blackColor]];
[imageview addSubview: imageView];
[self addSubview:imageview];
/** MapImageViewController.m It adds ImageScrollView on top of another UIScrollView pagingScrollView **/
UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
/** MapImageViewController.h **/
#interface MapImageViewController : UIViewController <UIScrollViewDelegate, UIGestureRecognizerDelegate> {
ImageScrollingView *imageScrollView;
MarkerIconsScrollView *markerScrollView;
}

UIScrollView *pagingScrollView;
ImageScrollView *page = [[[ImageScrollView alloc] init] autorelease];
[pagingScrollView addSubview:page];
MarkScrollView *markerScroll = [[[MarkScrollView alloc] init] autorelease];
[page addSubview:markerScroll]; /**But it never adds the other scrollview here **/
I'm noticing some things on this code snippet:
pagingScrollView is never initialized
the views that you are adding to it (provided that it actually is initialized with a valid frame), are not initialized with a frame. Always use - (id)initWithFrame:(CGRect)aRect to initialize your UIViews
Good luck!

Related

How to set UIImageView frame in ViewController for Universal App?

For a Universal App, how do I set the UIImageView frame in the ViewController?
UIView *baseView = [[UIView alloc] init];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
[baseView addSubview:myImage];
When I run app it shows black screen. So i guess I have to set a frame for it.
You kinda answered your own question - yes you have to set a frame for it.
You may want to try:
UIView *baseView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:baseView];
// Displays UIImageView
UIImageView *myImage = [[UIImageView alloc]
initWithImage:[UIImage imageNamed:#"tool.png"]];
myImage.frame = baseView.bounds;
[baseView addSubview:myImage];
You could also set the autoresizingmask for baseView and myImage so that as self.view's frame changes, so do these two views.
Also, just in case you aren't using ARC - don't forget to autorelease these views before leaving this method.

how to place a UIImageView in a particular area of a subView

I am woundering if there is a way of placing a UIImageView into a specific area of another view using addSubview
this is how my code currently looks, I am woundering how I would add the image to a particular spot of the view its being added to, say (0,0) top left.
this is what I am doing atm.
//...
jumpBar = [[JumpBarViewController alloc] initWithNibName:#"JumpBarViewController" bundle:[NSBundle mainBundle]];
jumpBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0.0, 480.0, (jumpBarHeight + 49.0), 320.0)];
[jumpBarContainer addSubview:jumpBar.view];
[self.view insertSubview:jumpBarContainer belowSubview:actionTabBar];
// Add jumpBar shade
switch (jumpBarHeight) {
case 103:
{
NSLog(#"one row");
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
[jumpBarContainer addSubview:smlShader]; // how do i add this to the top left of jumBarContainer?
}
//...
any help would be greatly appreciated :)
Set the UIImageView frame:
UIImageView *smlShader = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"1-row-top-bg.png"]];
smlShader.frame = CGRectMake(x,y,w,h); //Change the value of the frame
[jumpBarContainer addSubview:smlShader];

how to pushViewController to another view with images in a scrollview

I have a scrollview of images, I will like to tab them and will pushed to another view.
once i tab on the image, the whole view should push to another view.
From this View to another view
Detail view
Sorry for not asking the question clearly.
my scrollview
-(void)pictureScrolling
{
//init scrollview in location on screen
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 290)];
scrollview.backgroundColor = [UIColor redColor];
//pass image filenames
NSMutableArray *fileNames = nearbyFrog.imageFiles; //[[[NSMutableArray alloc] init] autorelease];
//setup the array of uiimageviews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
UIImageView *imageView;
//loop through the array imgNames to add file names to imgArray
for (NSString *imageName in fileNames) {
imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:imageName];
imageView.contentMode = UIViewContentModeScaleAspectFit;
[imgArray addObject:imageView];
[imageView release];
}
CGSize pageSize = scrollview.frame.size;
NSUInteger page = 0;
for (UIView *viewForScrollView in imgArray) {
[scrollview addSubview:viewForScrollView];
viewForScrollView.frame = CGRectMake(pageSize.width * page++ +10, 0, pageSize.width -20 , pageSize.height);
// making use of the scrollView's frame size (pageSize) so we need to;
// +10 to left offset of image pos (1/2 the gap)
// -20 for UIImageView's width (to leave 10 gap at left and right)
}
//add scroll view to view
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);
//scrollview.contentSize = CGSizeMake(320 *viewcount + 20, 290 );
scrollview.showsHorizontalScrollIndicator =NO;
[scrollview setPagingEnabled:YES];
scrollview.delegate =self;
//paging function for scrollview
CGRect frame = [[UIScreen mainScreen] applicationFrame];
self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 100, 100, 50)] autorelease];
self.pageControl.center = CGPointMake(frame.size.width/2, frame.size.height-60);
self.pageControl.numberOfPages = [fileNames count];
[self.view addSubview:self.pageControl];
//handle Touch Even
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
[imgArray release];
}
anybody knows how to do it or can show me a tutorial?
Thanks
I think this is the best way to implement it
Create your own Custom UIImageView class. This is required to store an additional property which will help you identify which image for clicked.
Create a delegate for that class which is called with the single tap event is raised in the UIImageView
Add the Images inside the scrollview using this custom class. The delegate of this class will tell you which image was tapped. You can use this to push a new view controller passing the image details (if necessary).
You can find some sample code in the ThumbImageView class in the scrollviewSuite sample
http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008904-Intro-DontLinkElementID_2

How to add an image subview to a tableview programmatically

I'm new to programming, and I know how to add a static image behind a tableView in a .xib file, but not in code. I have tried the following code:
UIImage * image = [UIImage imageNamed:#"pic.png"];
[self.view addSubView: image];
but nothing happens when I write this!
To display an UIImage you have to use an UIImageView and put it under the UITableView using the same frame. Don't forget to set the table view's background to transparent.
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];
You need to use a UIImageView, not a UIImage. Also, when adding a view programmatically, don't forget to set the frame.

UIgestureRecognizer in a view inside a UIScrollView

Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.
As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.
Here's how I would do it:
self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages
UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];
UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];
UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];
UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.
[webView setUserInteractionEnabled:YES]