Subview is displaying outside of its superview - objective-c

In my iPad application, I have a UIView object named "myView" whose frame is (0, 0, 600, 500). Now I want to add a UIImageView in this myview, so here is my code for doing this:
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-100, 0,100, 200)];
[myImageView setImage:UIImage imageNamed:#"myImage.png"];
[myView addSubView:myImageView];
[myImageView release];
So according to this code myImageView should not be displayed as its x-coordinate is "-100" and width is "100". But it is displaying out of myView at left side.
Can anyone please help to resolve this problem?

Use UIView's clipsToBounds property, which is off by default.

Related

UIView changing the size of the image?

I am trying to add UIImage to UIView .
The image is exactly in the size of the view -as i defined the view rect .
Somehow i see that the images is displayed wider,and taller(its stretched ) .
Why does my view is change the image size ?
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
UIView * aView = [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10) ];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
EDIT :
I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?
Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)
imageView.contentMode = UIViewContentModeScaleAspectFit;
use
imageView.layer.masksToBounds = YES;
which will restrict image to be wide and taller
If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).
of course your image is going to be stretched and can not be shown in your view.
Because its frame is a lot bigger than the size of the UIView.
you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:
UIImage *strip=[UIImage imageNamed:#"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView = [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];
and of course if you can make the size of the uiimageview smaller ;)

UIGestureRecognizer's sender view not working properly

According to this question, the UIGestureRecognizer has a view property which refers to the view the gesture is attached to. I used this in my code like this:
//Code for the 1st UIScrollView
UIImageView *bookCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:#selector(downloadBookTapped:)];
[bookCover addGestureRecognizer:singleTap];
[bookCover release];
[singleTap release];
//Code for the second UIScrollView
UIImageView *fileCover = [[UIImageView alloc]initWithFrame:CGRectMake(50, 100, 145, 420)];
UITapGestureRecognizer *singleFileTap = [[UITapGestureRecognizer alloc]initWithTarget:self
action:#selector(downloadFileTapped:)];
[fileCover addGestureRecognizer:singleFileTap];
[fileCover release];
[singleFileTap release];
And here is where I user the view property:
- (void)downloadBookTapped:(UITapGestureRecognizer *)sender
{
UIImageView *imgView = (UIImageView *)sender.view;
CGRect rect = [imgView frame];
UIImageView *images = [[UIImageView alloc]initWithFrame:rect];
//rest of code here...
}
- (void)downloadFileTapped:(UITapGestureRecognizer *)sender
{
UIImageView *imgView = (UIImageView *)sender.view;
CGRect rect = [imgView frame];
UIImageView *images = [[UIImageView alloc]initWithFrame:rect];
//rest of code here...
}
The problem here is that I have two scrollView and each scrollview holds multiple books. When I select a book at the 1st scrollView, the images is displayed correctly. But when I select a book inside the 2nd scrollView, the images is displayed incorrectly. Can anyone explain why this happens? Thanks.
---ADDITIONAL INFO---
The two scrollViews have the same width and height. The difference, of course, is there placement. The first scrollView is placed at (0, 0), while the second is at (0, 350). You can imagine the two as "shelves", the first one being the top shelf and the second one being the bottom shelf.
To specify the problem, say that I selected a book inside the second scrollView. The images will then be displayed as if I selected a book in the 1st scrollView. Meaning, the images is displayed in the 1st scrollView instead of the second scrollView.
Because the gestureRecognizer is bound to the first UIImageView and not the second.
[bookCover addGestureRecognizer:singleTap];
Do this for your other UIImageView and you will get the results you want.
I know now what I did wrong! Instead of adding the images as the subview of the scrollViews, I did this:
[self.view addSubView:images];
That's why It keeps appearing on the top side. It should be like this:
[scrollBook addSubview:images];
[scrollFile addSubView:files];

set background image dimensions IOS

I have an image bigger then a normal iPhone screen that I want to set as my background image but when I load the simulator I just see part of the image. Here is how I assigned the image:
UIImage *background = [UIImage imageNamed:#"background.png"];
[self.view setBackgroundColor:[UIColor colorWithPatternImage:background]];
how can I set the background image to something like self.view.bounds? If I was to use a UIImageView how do I ensure that it is in the background!!!
Make a UIImageView, give the UIImage to the UIImageView and add the UIImageView as a child to the view. Like so:
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"name.png"]];
[backgroundView setFrame:CGRectMake(0, 0, 320, 480)];
[self.view insertSubView:backgroundView atIndex:0];
[backgroundView release];
Set uiimageview frame - this is limitations for display.
Use
background.frame = CGRectMake (0,0, 320, 480); // as example - full screen,
also like in post above add addSubview and release code.
If you used frame - coordinates calculate from your superview - in that case - from self.view (simulator display). If you used bounds, coordinates calculate relatively to yours uiimageview (not self.view)

Stacking views ... code review needed

The problem:
On top of existing UIView, load a background image
From the card deck image (big image), cut 1 card (a part of a big image) and
Place that card on top of the background image
The following code works. It does exactly what i need it do to. I ask you though ..
Am i doing it right? Is this the way it's done?
Are all steps i used really needed? I have a strong suspicion i made it more complicated then it needs to be
Thank you for your time
// Define overall UIView area and create a view
UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
// Gain access to background image
UIImage *backgroundImage = [UIImage imageNamed:#"green_background.jpg"];
// Put background image on top of a
UIImageView *myImageView = [[UIImageView alloc] initWithImage:backgroundImage];
// So now we have the view of some size with some image bahind it
[background addSubview:myImageView];
// #####################################################################
CGRect positionOnParentView = CGRectMake(40, 40, 50, 130);
CGImageRef bigImage = [UIImage imageNamed:#"cards.png"].CGImage;
CGImageRef partOfABigImage = CGImageCreateWithImageInRect(bigImage, CGRectMake(0, 0, 200, 50));
// get an image to be displayed
UIImage *partOfImage = [UIImage imageWithCGImage:partOfABigImage];
// Put it on it's onw view
UIImageView *cardImage = [[UIImageView alloc] initWithImage:partOfImage];
// create a UIview and add image
UIView *card = [[UIView alloc] initWithFrame:positionOnParentView];
[card addSubview:cardImage];
[background addSubview:card];
[[self view] addSubview:background];
You're creating this view hierarchy:
UIView (self.view)
UIView (background)
UIImageView (backgroundImage - backgroundImage)
UIView (card)
UIImageView (cardImage - partOfABigImage)
A UIImageView can have subviews. So do you need all of those intermediate views? Would this simpler hierarchy be sufficient?
UIImageView (self.view - backgroundImage)
UIImageView (cardImage - partOfABigImage)
A couple of things:
Maybe you just didn't include your release statements, but make sure you are releasing the items you call alloc on. You can also autorelease them. For example,
UIView *background = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)] autorelease];
Yes, you are doing it right, the only way you will really reduce the amount of code you have here is to give each card it's own image so that you don't have to crop within the big image.

UINavigationBar custom title being hidden by background

I am adding a custom UIImageView to my UINavigationBar using this code:
UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
[background setImage:[UIImage imageNamed:#"nav_background.png"]];
[background setTag:kTagForReference];
[self.navigationController.navigationBar insertSubview:background atIndex:0];
I then add a custom title using this code:
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
...
[self.navigationItem setTitleView:label];
[label release];
But on some of my pushed views the title is being hidden (or isn't visible). I can't seem to push more than two views onto the stack without the title disappearing.
I've tried to force my UIImageView background to the back of the bar but that still doesn't help. I've printed the subviews of the UINavigationBar and I can see that the UILabel is there, I just can't see it!
Any help would be greatly appreciated.
If you can run the dumpWindows() function, the output would show the view hierarchy so you can see what view is covering it up.
You most likely want to use a UIToolBar instead of a NavigationBar. Then you can add the subviews to the UIToolbar.
Bring the UILabel to the front most view.
[self.view bringSubviewToFront: label];