How do I implement UIScrollView programatically - xcode

I am launching detail pages from a UITableView and within that detail page there is an image. I would like it to be zoomable, so I'd like the contents of that detail page to sit inside a scrollView. So far no luck, the labels, textviews and images are fine, but I can't get the ScrollView implemented.
here is my .m: THANKS!
- (void)viewDidLoad {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 700);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
self.navigationItem.title = [NSString stringWithFormat:selectedCellItem];
CGRect frame = CGRectMake(0, 0, 10, 10);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.backgroundColor = [UIColor whiteColor];
frame = CGRectMake(0, 320, 10, 10);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
//label.text = selectedCellItem;
label.font=[UIFont fontWithName:#"Verdana-Bold" size:20.0];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
frame = CGRectMake(0, 320, 320, 60); //Description
UITextView *labelDesc = [[UITextView alloc] initWithFrame:frame];
labelDesc.text = selectedCellDesc;
labelDesc.font=[UIFont fontWithName:#"Verdana" size:12.0];
labelDesc.textAlignment = UITextAlignmentLeft;
//labelDesc.textAlignment = UITextAlignmentTop;
[self.view addSubview:labelDesc];
[labelDesc release];
frame = CGRectMake(0, 0, 320, 320); //Image
UIImageView *imageItem = [[UIImageView alloc] initWithFrame:frame];
[imageItem setImage:[UIImage imageNamed:selectedCellImage]];
// GET THIS ONE WORKING RIGHT>>[imageItem setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [[NSBundle mainBundle] bundlePath], selectedCellImage]]]; //use
//imageItem = [[UIImageView alloc] initWithContentsOfFile:[NSString stringWithFormat:#"shirt.png", [[NSBundle mainBundle] bundlePath]]];
//labelDesc.font=[UIFont fontWithName:#"Zapfino" size:13.0];
//labelDesc.textAlignment = UITextAlignmentCenter;
imageItem.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageItem];
//[imageItem release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end

I think that you're missing the following line to add the UIScrollView as a subview.
[self.view addSubView:scrollView];

Related

Add view onto UIViewController

I have made a UIImagePicker
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
And I want to add a half circle to to so I do this,
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextBeginPath(gc);
CGContextAddArc(gc, 100, 100, 50, -M_PI_2, M_PI_2, 1);
CGContextClosePath(gc); // could be omitted
CGContextSetFillColorWithColor(gc, [UIColor cyanColor].CGColor);
CGContextFillPath(gc);
UIView *someView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
[someView.layer renderInContext:gc];
[picker setCameraOverlayView:someView];
But then when I show the picker like so
[self presentViewController:picker animated:YES completion:NULL];
I don't see the semicircle? Why is this happening?
Thanks
Hey you are not going on proper way.
Do the below step it will help you...
See the below answer..
https://stackoverflow.com/questions/5552210/uitableviewcells-invalid-context-trying-to-draw
CircleView.h
#import <UIKit/UIKit.h>
#interface CircleView : UIView
#end
CircleView.m
#import "CircleView.h"
#implementation CircleView
- (void)drawRect:(CGRect)rect
{
CGContextRef gc = UIGraphicsGetCurrentContext();
CGContextBeginPath(gc);
CGContextAddArc(gc, 100, 100, 50, -M_PI_2, M_PI_2, 1);
CGContextClosePath(gc); // could be omitted
CGContextSetFillColorWithColor(gc, [UIColor cyanColor].CGColor);
CGContextFillPath(gc);
}
#end
Implimenttion of adding half circle in your imagepicker is here
UIImagePickerController *controller = [[UIImagePickerController alloc]init];
controller.delegate=self;
controller.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:controller animated:YES completion:^{
CircleView *someView = [[CircleView alloc] initWithFrame:CGRectMake(0, 0, 400, 400)];
[someView setBackgroundColor:[UIColor colorWithWhite:0.5 alpha:0.5]];
[controller setCameraOverlayView:someView];
}];

Custom UIBarButton not appearing correctly in UITableView

I have code that successfully creates a custom UIBarButtonItem that appears on the navigation bar. I use the code in other view controllers, and it works perfectly, but in the UITableViewController, the button appears as the regular BackButtonItem. Can someone help me out? Here is the code that I use in the viewDidLoad method in all of the viewControllers
#implementation P2OListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
- (void)viewWillAppear:(BOOL)animated {
}
- (void)goToMenu:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
hide the default backBarButtonItem by setting its hide property to yes
self.navigationItem.hidesBackButton = YES;
in your viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
}
if above not works try this & see what happens:
if (!self.navigationItem.backBarButtonItem.hidden) {
self.navigationItem.backBarButtonItem.hidden = YES;
}
else {
NSLog(#"back button already hidden");
}

How to put programmatically iCarousel in View?

I want four ScrollViews like iCarousel's CoverFlow in View programmatically. My Code is
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
//set up data
wrap = YES;
self.items = [NSMutableArray array];
for (int i = 0; i < NUMBER_OF_ITEMS; i++)
{
[items addObject:[NSNumber numberWithInt:i]];
}
carousel1 = [[carousel1 alloc] init];
carousel2 = [[carousel2 alloc] init];
carousel3 = [[carousel3 alloc] init];
carousel4 = [[carousel4 alloc] init];
carousel1.frame = CGRectMake(0, 0, 320, 120);
carousel2.frame = CGRectMake(0, 120, 320, 120);
carousel3.frame = CGRectMake(0, 240, 320, 120);
carousel4.frame = CGRectMake(0, 360, 320, 120);
carousel1.delegate = self;
carousel2.delegate = self;
carousel3.delegate = self;
carousel4.delegate = self;
carousel1.dataSource = self;
carousel2.dataSource = self;
carousel3.dataSource = self;
carousel4.dataSource = self;
[self.view addSubview:carousel1];
[self.view addSubview:carousel2];
[self.view addSubview:carousel3];
[self.view addSubview:carousel4];
//configure carousel
carousel1.type = iCarouselTypeCoverFlow2;
carousel2.type = iCarouselTypeCoverFlow2;
carousel3.type = iCarouselTypeCoverFlow2;
carousel4.type = iCarouselTypeCoverFlow2;
}
but fail with four warnings with out placing the carousels view on the View. Please help me to overcome this problem. Thanks in advance.
Shouldn't you be initialising them with [[iCarousel alloc] init] instead of [[carousel1 alloc] init]?

Custom scroll view - image outside the frame

I would like to make an infinite scrolling view for iPad. The scrolling works, only when I add pictures to my customized scrollview class, it appears out of the view's frame too (so I have view, which has a UIScrollView on it. This scroll view is connected with my CustomScroll.h :UIScrollView and CustomScroll.m files). Why do the images appear outside the view's frame (the view's frame is set in nib file to 320x420)?
CustomScroll.h:UIScrollView
A part from CustomScroll.m file:
- (id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])){
self.contentSize = CGSizeMake(2000, 416);
[self setShowsVerticalScrollIndicator:NO];
for (int i=1; i<=4; i++){
[self addImageAtPosition:i];
}
//todo: to replace with the method: addFirstElementToTheEnd
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.jpg"]];
[image1 setFrame:CGRectMake(5*320, 0, 320, 416)];
[self addSubview:image1];
[image1 release];
//todo: to replace with the method: addLastElementToTheBeginning
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image4.jpg"]];
[image2 setFrame:CGRectMake(0*320, 0, 320, 416)];
[self addSubview:image2];
[image2 release];
}
return self;
}
- (void)addImageAtPosition:(NSInteger)position{
UIImageView *tempImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"image%d.jpg", position]]];
[tempImage setFrame:CGRectMake((position * 320), 0, 320, 416)];
[self addSubview:tempImage];
[tempImage release];
}
- (void)recenterIfNecessary{
CGFloat currentOffsetX = [self contentOffset].x;
if (currentOffsetX > 1600.00){
self.contentOffset = CGPointMake(320, [self contentOffset].y );
}
if (currentOffsetX < 320.00){
self.contentOffset = CGPointMake(5*320, [self contentOffset].y);
}
}
- (void) layoutSubviews{
[super layoutSubviews];
[self recenterIfNecessary];
}
TY.

UIControl touchEvent

i have implemented the following viewDidLoad() Method:
- (void)viewDidLoad {
[super viewDidLoad];
// loading images into the queue
loadImagesOperationQueue = [[NSOperationQueue alloc] init];
AFOpenFlowView *openFlow = self.view;
theControl = [[UIControl alloc] initWithFrame:self.view.frame];
NSString *imageName;
btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(10, 10, 40, 40);
[btn addTarget:self action:#selector(testMethode) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:#"<<" forState:UIControlStateNormal];
[btn setHidden:YES];
[theControl addSubview:btn];
for (int i=0; i < 10; i++) {
imageName = [[NSString alloc] initWithFormat:#"cover_%d.jpg", i];
imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIImage *aktuellesImage = imageView.image;
UIImage *scaledImage = [aktuellesImage scaleToSize:CGSizeMake(100.0f, 100.0f)];
[openFlow setImage:scaledImage forIndex:i];
// [(AFOpenFlowView *)self.view setImage:scaledImage forIndex:i];
[imageName release];
[aktuellesImage release];
}
[theControl addSubview:openFlow];
[theControl setUserInteractionEnabled:YES];
[(AFOpenFlowView *)self.view setNumberOfImages:10];
}
And the following touchesEnded() Method:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[btn setHidden:NO];
}
But the touchesEnded doesn't work, and the Button will not shown when i touch the picture, does anybody know what is the Problem???
Greetings Marco
Make sure your view has user interaction enabled in order to register touches. This can be set in your View Controller thusly:
self.view.userInteractionEnabled = YES;

Resources