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"];
Related
Does anyone know how I can bring this (see code) image to the background? It's no 'hanging' over an other image which I placed in the Interface Builder
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 104)];
[imageView setImage:[UIImage imageNamed:#"helloworld.png"]];
[self.view addSubview:imageView];
Maybe
[yourSubView.superview sendSubviewToBack:yourSubView];
Look at this question: How to put UIImageView on the background of the layout
Say you have a UIImage *image and a UIView *v
How would you display the image on top of the view programmatically?
If you just want to add the UIImage to the UIView then you need an UIImageView inbetween the UIView and UIImage, such as:
UIImage *image = [[UIImage alloc] init];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[iv setImage:image];
[v addSubview:iv];
Note that the above is just dummy code and I've created all UI elements with a zero frame.
If you want to add UIImage to UIView programatically you can directly add image to view, Such as:
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
customView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"YourImage.png"]];
[self.view addSubview:customView];
How can i take an image and stretch it to fill the screen on my UIView (320/460)
I am loading and placing it, however as you can see, some scaling is needed.
Thank you!
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
[myImageView setContentMode:UIViewContentModeScaleToFill];
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
Like I said in my comments above to Shamsiddin's post, the UIImageView needs a size to know where to stretch to. Here I'm using the same size of the UIView. I've added another answer because the code to UIView is missing. So here's the complete code.
// Area of the UIView and UIImageView
CGRect gameArea = CGRectMake(0, 0, 320, 460);
// Create UIImageView with Image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
// Create UIView and insert subview
UIView *topView = [[UIView alloc] initWithFrame:gameArea];
[topView addSubview:myImageView];
[[self view] addSubview:topView];
CGRect gameArea = CGRectMake(0, 0, 320, 460);
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:gameArea];
[myImageView setImage:[backgroundImage stretchableImageWithLeftCapWidth:3 topCapHeight:3]];
[myImageView setUserInteractionEnabled:YES];
[self.view addSubview:myImageView];
try
UIView *view;
view.ContentMode = UIViewContentModeScaleToFill;
I just did this in my own iOS5 app, i needed to dynamically set the background image and scale it. Imagine you are doing this in - (void)viewWillAppear:(BOOL) animated
UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.frame];
[imageview setContentMode:UIViewContentModeScaleAspectFill];
[imageview setImage:[UIImage imageNamed:#"Background.png"]];
[self.view addSubview:imageview];
[self.view sendSubviewToBack:imageview];
So we create the view, set it's scaling mode, add the bg image, set it as the subview in the main view and then push it to the back so it does not occupy any other subviews.
Please accept this answer.
Hey everyone, I have a iPhone App I am creating. It uses a uitableview and that goes to a detail view.
Now on the detail view I have a scroll view, and all the data (XML) comes into it fine, each time, no issues.
My UILabels in my scrollview do update with the correct data, but, they keep adding subviews on top of each other to keep adding label after label and it all looks mumbo jumbo.
I have tried everything to try and remove the subviews such as:
for(UIView *subview in [scrollView subviews]) {
[subview removeFromSuperview];
}
AND
[[scrollView subviews] makeObjectsPerformSelector:#selector(removeFromSuperview)];
So I am stumped, I got no idea what is happening. Any help? I have been searching Google for ages and agesm, but keep finding the same answers that don't work :(
Any direction to sites that might help that you know of will be greatly appreciated.
Thanks!
Here is what I have in my controller that loads all the scrollview and labels:
- (void)viewDidAppear:(BOOL)animated {
// Create the scroll view for this view
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
scrollView.contentSize = CGSizeMake(320, 270);
[scrollView setFrame:CGRectMake(0, 198, 320, 170)];
// Do the labels for the text
UILabel *dates = [[UILabel alloc] initWithFrame:scrollView.bounds];
dates.textColor = [UIColor whiteColor];
dates.font = [UIFont boldSystemFontOfSize:12];
dates.text = round_date;
dates.tag = 1;
[dates setBackgroundColor:[UIColor clearColor]];
[dates setFrame:CGRectMake(10, 10, 280, 22)];
[scrollView addSubview:dates];
[dates release];
// Add the content to the main scrollview
[self.view addSubview:scrollView];
[scrollView release];
}
Ok, the reason for this is that viewDidAppear will get called every time you present the view.
If you do this is
-(void) viewDidLoad;
instead.
To make sure you can access them later you might want to keep a reference to them in your UIViewController
-(void) viewDidLoad {
scrollView = [[UIScrollView alloc] initWithFrame];
[self.view addSubview:scrollView];
}
and define it in your header file with:
UIScrollView *scrollView;
I have this UIScrollView with pagingEnabled to let the user page between some images.
Now I want to let the user zoom on each one of the images. How do I do that? What I have now zooms on my UIScrollView from its origin and I need it to zoom on each picture, not the hole UIScrollView.
What I have now:
UIScrollView* containerView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
containerView.scrollEnabled = YES;
containerView.pagingEnabled = YES;
[containerView setMaximumZoomScale:2.0];
[containerView setDelegate:self];
self.view = containerView;
UIImage *imageOne = [UIImage imageNamed:image01];
UIImageView *viewOne = [[UIImageView alloc] initWithImage:imageOne];
viewOne.frame = CGRectMake(0, 44, 320, 480);
[containerView addSubview:viewOne];
UIImage *imageTwo = [UIImage imageNamed:image02];
UIImageView *viewTwo = [[UIImageView alloc] initWithImage:imageTwo];
viewTwo.frame = CGRectMake(320, 44, 320, 480);
[containerView addSubview:viewTwo];
UIImage *imageThree = [UIImage imageNamed:image03];
UIImageView *viewThree = [[UIImageView alloc] initWithImage:imageThree];
viewThree.frame = CGRectMake(640, 44, 320, 480);
[containerView addSubview:viewThree];
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)containerView
{
return containerView;
}
Thanks in advance
You have to return a view contained in your scrool. As you have 3 UIImage, try with this...
In .h
UIView *auxView;
In .m
...
[auxView addSubview:imageOne];
[auxView addSubview:imageTwo];
[auxView addSubview:imageThree];
[containerView addSubview:auxView];
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)containerView{
return auxView; }
I hope this help you
I think you mean you want to zoom each UIImageView individually, right?
I was struggling with this problem many weeks, then I decide to wrote a class to solve this problem.
Here is my github repo of this class: https://github.com/windmemory/PhotoCollectionView
My solution to this problem is to create single UIScrollView to contain every UIImageView, and then add these UIScrollView to the containerView.
If we add all the UIImageView to a single view then make it zoomable, when we zoom one photo, all these photos will be zoomed, then the contentSize will change accordingly. Once the contentSize is changed, it will messed up all the paging thing. So the best way I came up is to use UIScrollView to contain UIImageView, then put every UIScrollView into the containerView, this will allow you to zoom each photo separately, which won't mess up all the paging thing you set up.
Hope this may help you.