How do I put viewController's into a UIScrollView - iphone

I want to initialize 5 viewController's that I want to be able to flick between in a UIScrollView, when my app loads.

Here is an example of how you can do this:
- (void)viewDidLoad
{
//standard UIScrollView is added
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:scrollView];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(320*2, 460); //this must be the appropriate size!
//required to keep your view controllers around
controllers = [[NSMutableArray alloc] initWithCapacity:0];
//just adding two controllers
LabeledViewController *one = [[LabeledViewController alloc] initWithPosition:0 text:#"one"];
[scrollView addSubview:one.view];
[controllers addObject:one];
LabeledViewController *two = [[LabeledViewController alloc] initWithPosition:1 text:#"two"];
[scrollView addSubview:two.view];
[controllers addObject:two];
}
LabeledViewController is pretty simple, but you can add as much to it as you want:
#implementation LabeledViewController
- (id)initWithPosition:(NSInteger)position text:(NSString*)text
{
if (self = [super init]) {
myPosition = position;
myText = [text retain];
}
return self;
}
- (void)viewDidLoad
{
//this will setup the position in the UIScrollView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(320*myPosition, 0, 320, 460)];
self.view = view;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 320, 50)];
label.text = myText;
[self.view addSubview:label];
}

Related

TapGestureRecognizer does not get called

Hi in the main WiewController in the viewDidLoad i set up a
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
and then i a for loop i create UIViews and add them to a scroll view, that is then added to the main view.
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
newsContainer.userInteractionEnabled = YES;
[newsContainer addGestureRecognizer:recognizer];
[tempScroll addSubview:newsContainer];
then i have a function
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
Which never gets called, any suggestions? your help would be greatly appreciated. Thanks in advance.
here is the entire .m
#import "iPadMainViewController.h"
#import "GradeintView.h"
#import <QuartzCore/CALayer.h>
#import "Category.h"
#import "News.h"
#import "LazyImageView.h"
#import "TouchView.h"
#interface iPadMainViewController ()
#end
#implementation iPadMainViewController
#synthesize detailsView = _detailsView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap:)];
[recognizer setDelegate:self];
GradeintView *MainTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 0, 1024, 50)];
GradeintView *MainSubTitle = [[GradeintView alloc] initWithFrame:CGRectMake(0, 50, 1024, 30)];
NSMutableArray *categoriesCollection = [[Category alloc] allCategoriesFromFeedAtUrl:#"http://geonews.ge/xml/category.php"];
UIScrollView *categories = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 512, 768)];
_detailsView = [[UIWebView alloc] initWithFrame:CGRectMake(500, 0, 512, 768)];
[_detailsView addGestureRecognizer:recognizer];
[categories setScrollEnabled:TRUE];
[categories setContentSize:CGSizeMake(500, categoriesCollection.count * 156)];
MainTitle.layer.masksToBounds = NO;
MainTitle.layer.shadowOffset = CGSizeMake(3, 3);
MainTitle.layer.shadowRadius = 5;
MainTitle.layer.shadowOpacity = 0.3;
[categories setBackgroundColor:[UIColor redColor]];
int counter = 0;
for (Category *cat in categoriesCollection)
{
UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter
, 500, 20)];
UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)];
[categoryLabel setBackgroundColor:[UIColor clearColor]];
NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId];
categoryLabel.text = cat.Name;
categoryLabel.textColor = [UIColor whiteColor];
[categoryTitle addSubview:categoryLabel];
UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1];
UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1];
[categoryTitle setBackgroundColor:myblack];
UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)];
UIColor *tempcolor = ligheterBlac;
tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
tempScroll.layer.borderWidth = 0.5f;
int countnews = 0;
for (News *news in allCurrentNews)
{
UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)];
newsContainer.tag = countnews;
[newsContainer addGestureRecognizer:recognizer];
//newsContainer.NewsId = news.NewsId;
LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]];
image.frame = CGRectMake(0, 0 , 156, 96);
UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)];
newsTitle.backgroundColor = myblack;
newsTitle.numberOfLines = 2;
newsTitle.font = [UIFont systemFontOfSize:11];
newsTitle.text = news.Title;
newsTitle.textColor = [UIColor whiteColor];
newsTitle.textAlignment = UITextAlignmentCenter;
newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor;
newsContainer.layer.borderWidth = 0.5f;
[newsContainer addSubview:image];
[newsContainer addSubview:newsTitle];
countnews ++;
[tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)];
[tempScroll addSubview:newsContainer];
//[image release];
}
[tempScroll setBackgroundColor: tempcolor];
[categories addSubview:tempScroll];
[categories addSubview:categoryTitle];
[tempcolor release];
[tempScroll release];
counter ++;
}
self.detailsView.layer.masksToBounds = NO;
self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5);
self.detailsView.layer.shadowRadius = 5;
self.detailsView.layer.shadowOpacity = 0.3;
[self.detailsView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.amazon.com"]]];
[self.view addSubview:categories];
[self.view addSubview:self.detailsView];
[self.view addSubview:MainSubTitle];
[self.view addSubview:MainTitle];
}
- (void)processTap:(UITapGestureRecognizer *)recognizer {
UIView *view = recognizer.view;
NSLog(#"HELLO, %d", view.tag);
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)dealloc {
[super dealloc];
}
- (void)loadDetailedContent:(NSString *)s
{
}
#end
Change
initWithTarget:self.view
to
initWithTarget:self
EDIT:
You also have forgotten colon:
initWithTarget:self action:#selector(processTap:)
EDIT2:
You have created _detailsView (with assigned UITapGestureRecognizer) but have not added it to any subview. How it will work?
I think the problem is that the scrollView, which contains your views, has its own internal gesture recognizer that is "taking away" touch events from your tap gesture recognizer. Try implementing the following gesture recognizer delegate method:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
Have you checked the Interaction setting?
Interaction shall be set to "User Interaction Enabled" in the Attributes inspector for the image.
Change #selector(processTap) to #selector(processTap:)
Because now you calling method that doesn't exist.
try this code
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(processTap)];
[newsContainer addGestureRecognizer::gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
I assume the problem is, you might have missed to add <UIGestureRecognizerDelegate> in the header file.
You're adding a UIGestureRecognizer to a UIWebview, which is not recommended. UIWebview has its ownUIGestureRecognizer`s which are tricky to over-ride. See this SO question for more info.
you can first add your gestureRecognizer to self.view and check your method called or not... as well check this thread
Issue with a UITapGestureRecognizer

How to make stationary background view in UITableView?

I'm trying to add a view behind my grouped UITableView. Unfortunately, whenever I scroll, the background view moves as well. This happens even when I use [self.view insertSubview:backgroundView belowSubview:_tableView], or [_tableView setBackgroundView:backgroundView]. Why is this background view scrolling? Also, why does my tableView scroll, even though I have disabled scrolling? Are these related?
In app Delegate:
History *historyController = [[History alloc] init];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:historyController];
In History.m:
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release]; iv = nil;
[self.view addSubview:bg];
[self.view addSubview:_tableView];
[bg release]; bg = nil;
[_tableView release];
[super viewDidLoad];
}
Try
- (void)viewDidLoad {
CGRect frame = self.view.frame;
frame.origin.x = 0;
frame.origin.y = 0;
UIView *bg = [[UIView alloc] initWithFrame:frame];
[bg setBackgroundColor:[UIColor scrollViewTexturedBackgroundColor]];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CenterFade.png"]];
iv.alpha = .750;
[bg addSubview:iv];
[iv release];
iv = nil;
_tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setScrollEnabled:NO];
[_tableView setBackgroundColor:[UIColor clearColor]];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[bg addSubview:_tableView];
[_tableView release];
_tableView = nil;
[self.view addSubview:bg];
[bg release];
bg = nil;
[super viewDidLoad];
}
If your controller is an UITableViewController just change it to an UIViewController<UITableViewDelegate, UITableViewDatasource> with a tableView property (It's the same)
I don't know why this is happening, but for some reason self.view is a UITableView and not a UIView. Creating a new UIView and setting it to self.view fixed the problem. I am not using a UITableViewController, but a UIViewController. No idea where the UITableView is coming from!
set [_tableView setBackgroundColor:[UIColor clearColor]];
and then put your background view under UITableView in UIViewController view hierarchy.
UITableView has a backgroundView property.
Swift:
var backgroundView: UIView?
Objective-C
#property(nonatomic, readwrite, retain) UIView *backgroundView
This view will not be moved when you scroll the tableView

How to catch event inside uiscrollview on image

Hi I have populated uiscrollview with images (code is bellow) but I don't know how to add some event to images inside. I want to double tap on image inside scrollview and get some event. Any direction how to achieve this?
arrayOfImages = [[NSMutableArray alloc] init];
NSString *img;
for (img in imgArray)
{
[arrayOfImages addObject:[UIImage imageNamed:img]];
}
NSLog(#"Array initialization complete...");
scrollView = [[UIScrollView alloc] init];
scrollView.scrollEnabled = YES;
scrollView.pagingEnabled = YES;
scrollView.directionalLockEnabled = YES;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
scrollView.backgroundColor = [UIColor blueColor];
scrollView.autoresizesSubviews = YES;
scrollView.frame = CGRectMake(0, 0, 320, 29);
[self.view addSubview:scrollView];
NSLog(#"Scroll View initialization setup complete...");
UIImage *imageToAdd;
int x = 0;
int y = 0;
for (imageToAdd in arrayOfImages)
{
UIImageView *temp = [[UIImageView alloc] initWithImage:imageToAdd];
temp.frame = CGRectMake(x, y, 29, 29);
x += 29;
[scrollView addSubview:temp];
}
NSLog(#"Adding images to outlet complete...");
Add a UITapGestureRecognizer to the UIImageView(s).
Look at this guide for more info.

doesn't work correctly. addSubview problem

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];

Adding a UIButton to UIScrollView

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)];