xcode 5 horizontal scroll doesn't work - uiscrollview

who can help me with this code? Simply it doesn't work, same as ALL I have tried for vertical scrolling. I'm starting to think that my Xcode emulator has something wrong. Here's the code:
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
[self.view addSubview:scroll];
[scroll setBackgroundColor:[UIColor redColor]];
{
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1222, 400)];
[imageview setImage:[UIImage imageNamed:#"panorama_orizzonte_medio_big.jpg"]];
[scroll addSubview:imageview];
[self.view addSubview:scroll];
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

After you set the UIImageView, you need to set the scrollView's contentSize to that of your imageView's, so, something like this:
[scroll setContentSize:imageview.frame.size];

Related

UIWebView that is added programmatically not scrolling

If I add a uiwebview using storyboard it works great. But webviews that is added programmatically not scrolling.
self.webview=[[UIWebView alloc] init];
path=[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
[self.webview setFrame:CGRectMake(-200, 0, 200, 300)];
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]]];
[self.view addSubview:self.webview];
UIWebview is added to a View programmatically using the below code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width,600)];
[webview setBackgroundColor:[UIColor redColor]];
NSString *url=#"http://www.facebook.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
[self.view addSubview:webview];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

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

How do you create a UIImageView programmatically in Xcode

I just want to make an UIImageView programmatically that displays a 20by20 dot at point (50,50).(The image of the dot is called draw.png). For some reason nothing shows up on the screen.
Heres my code:
- (void)viewDidLoad
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
First, make sure draw.png exists in your project and that you are referencing it exactly (so if it's draw.PNG you should put #"draw.PNG"). Also, call [super viewDidLoad] before everything.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:#"draw.png"];
[self.view addSubview:dot];
}
Try this :-
UIImageView *imageview = [[UIImageView alloc]
initWithFrame:CGRectMake(50, 50, 20, 20)];
[imageview setImage:[UIImage imageNamed:#"AppleUSA1.jpg"]];
[imageview setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:imageview];
In Swift :
let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 20, height: 20))
imageView.image = UIImage(named: "draw.png")
self.view.addSubview(imageView)
This code is correct. Make sure draw.png exists in your project. You can do it by checking if [UIImage imageNamed:#"draw.png"] doesn't return nil.
Also, make sure you don't have another view on top of your image view.
make sure that image is in your project..
UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[imgView setImage:[UIImage imageNamed:#"xyz.jpg"]];//if your images extension is .png than no need to write extension of an image..
[self.view addSubview:imgView];
This code is perfect match an UIImageView:
UIImageView *IMG_view= [[UIImageView alloc] initWithFrame:CGRectMake(20 ,50 ,40 ,40)];
[IMG_view setTag:100]; //[IMG_view setTag:indexPath.row];
IMG_view.layer.borderWidth= 0.5 ;
IMG_view.layer.borderColor= [[UIColor clearColor] CGColor];
IMG_view.layer.cornerRadius= 3;
IMG_view.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:IMG_view];
IMG_view.image=[UIImage imageNamed:#"Loading_50x50.png"];

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.

How do I implement UIScrollView programatically

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

Resources