UIImageView - UIScrollView Fill and set X to 0 - objective-c

StackOverflow.. last resort as I hate bothering other devs but I've come to the end of my tether with my problem..
I have a UIView, in which are 2 UIScrollViews - in each scrollview is a UIImageView. The original idea is that I can move the image in each scroll view on the Y Axis to reposition the images.
Currently, the UIImageViews are using the content mode of (so the images don't stretch and display in full):
UIViewContentModeScaleAspectFill
The images load fine into the ScrollViews - the only problem is, the position of the UIImageView's are centred on the Y Axis and I need them to be 0! I've tried setting the UIImageView frame after adding and setting the content mode but nothing.
Here is my code:
- (outletContainer is a UIView)
- (imagePack is an NSMutableArray of UIImages)
UIImageView *tempFirst = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:0]];
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempFirst setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *firstScroller = [[UIScrollView alloc] init];
[firstScroller setFrame:CGRectMake(0, 0, tempFirst.image.size.width, outletContainer.frame.size.height/2)];
[firstScroller setContentSize:tempFirst.image.size];
[firstScroller addSubview:tempFirst];
[outletContainer addSubview:firstScroller];
UIImageView *tempSecond = [[UIImageView alloc] initWithImage:[imagePack objectAtIndex:1]];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
[tempSecond setContentMode:UIViewContentModeScaleAspectFill];
// add scrollview for positioning:
UIScrollView *secondScroller = [[UIScrollView alloc] init];
[secondScroller setFrame:CGRectMake(0, outletContainer.frame.size.height/2, tempSecond.image.size.width, outletContainer.frame.size.height/2)];
[secondScroller setContentSize:tempSecond.image.size];
[secondScroller addSubview:tempSecond];
[outletContainer addSubview:secondScroller];
Thanks in advance!

I think you have to delete these two lines:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*tempF)];
[tempSecond setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, outletContainer.frame.size.height/2)];
When you create an UIImageView with image it takes UIImage size.
Edited:
Ok, then you have to do that:
[tempFirst setFrame:CGRectMake(0, 0, outletContainer.frame.size.width, tempFirst.image.size.height*outletContainer.frame.size.width/tempFirst.image.size.width)];
firstScroller.contentSize = CGSizeMake(outletContainer.frame.size.width, tempFirst.frame.size.height);

Related

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)

Subview is displaying outside of its superview

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.

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

Position of UIScrollView after change in orientation

I have a UIScrollView with an image inside it. This is the code:
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
UIScrollView *imScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 96)];
[imScrollView addSubview:imView];
[imScrollView setContentSize:CGSizeMake(530, 96)];
[[self view] addSubview:imScrollView];
[imView release];
[imScrollView release];
}
This is what it looks like in portrait mode:
I want to know
1. How can I position the image exactly in the middle of the screen (horizontally).
2. How can I make sure this is going to be the same if the orientation switches to landscape. This is what currently the landscape looks like :
Thanks.
Try setting the autoresizingMask property of your imView just after you create it:
imView.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
The UIView class reference contains more info.