UIImageView completely wrong position in UIView - ios

I'm trying to add an UIImageView to a UIView that I created.
However, the position of the image is way too low inside the UIView, even though I set the frames to be equal.
The UIView is called photoView, here is what I did:
UIImageView *imgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"image1.JPG"]];
imgV.frame = self.photoView.frame;
[self.photoView addSubview:imgV];
You can see the result (and the position of photoView in the storyboard) here:
I have no idea how to fix this, adding
imgV.center = self.photoView.center;
imgV.contentMode = UIViewContentModeScaleToFill;
doesn't help either.

Try this:
imgV.frame = self.photoView.bounds;
instead of this:
imgV.frame = self.photoView.frame;

Nevermind, I found the answer, but I'll leave this up here in case anyone else has this question.
The mistake was in the difference between frame and bounds. This is where I found my answer.
My solution was this:
[imgV setFrame:CGRectInset(self.photoView.bounds, 0, 0)];

Related

UIImage retutns wrong width

I have a UIImageView and I put it in a toolbar. When I run it on the simulator, the image gets stretched in. Meaning the width somehow gets less than the toolBar. Here's my code:
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
nav.frame.size.width, nav.frame.size.height)];
nav.image = [UIImage imageNamed:[imageTop objectAtIndex:0]];
This is the output.
http://postimg.org/image/rjirnmiyx/
The yellow is the toolbar and the green thing is the image. It does not fill the toolbar.
Update
Whatever I do, it gets scaled off at the right side. (Like the image above.)
I tried all kinds of content modes and it all came down to one thing. Something does get ruined at the right hand side.
Update 2
I figured out the problem! The problem is that the UIImageView does not have a constraint. Can someone please help me add a constraint?
you can try this
[imageNav sizeToFit];
UIImage *image = [UIImage imageNamed:[imageTop objectAtIndex:0]]
UIImageView *imageNav = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
image.size.width, image.size.height)];
imageNav.image = image;
Hope above code snippet helps..

Is UICollectionView.backgroundView broken

I'm writing something relatively simple, or so I thought.
Firstly, the code, for which I'm trying to place an image on the background of the UICollectionView if there are no results returned from my server. The image is 200 by 200:
UIView *myView = [[UIView alloc] initWithFrame:self.view.bounds];
CGRect myViewSpace = self.view.bounds;
CGFloat myX = (myViewSpace.size.width /2.0) - 100;
CGFloat myY = (myViewSpace.size.height /2.0) - 100;
UIImageView *imView = [[UIImageView alloc] initWithFrame:CGRectMake(myX, myY, 200, 200)];
imView.image = [UIImage imageNamed:#"imNotHome"];
[myView addSubview:imView];
myCollectionView.backgroundView = myView;
Once there are results, I want to be able to remove it.
I thought it'd be as simple as placing the following, before I reloaded the UICollectionView:
[myCollectionView.backgroundView removeFromSuperview];
However, it appears to be doing nothing.
Am I missing something?
Thanks in advance!
It should be done this way instead:
myCollectionView.backgroundView = nil;
Explanation: You should unset the UICollectionView's background in the same way as you set it. You didn't set it by manipulating the view hierarchy, but by setting the background property. You did call addSubview in the previous line, but that was to add a subview to your background view, not to add the background view itself.
Edit:
There is a very good article about this UICollectionView bug here:
http://blog.spacemanlabs.com/2013/11/uicollectionviews-backgroundview-property-is-horribly-broken/
The solution the author gives is to reset the background to an empty opaque view:
UIView *blankView = [UIView new];
blankView.backgroundColor = [UIColor whiteColor];
[myCollectionView.backgroundView removeFromSuperview];
myCollectionView.backgroundView = blankView;
Also, the author recommends not using the backgroundView property at all but doing it yourself:
Frankly, I think the best solution is to just ignore the backgroundView property all together. Instead, make the collection view’s background clear, and implement your own backgroundView; just throw a view behind the collection view.

How to fill up an UIImageView by another image?

I would like to fill up an empty UIImageView by another Image.However the two pictures have not the same width, Height and shapes.So what I should do is to scale the second image until it fills up all the area and it should be displayed only there.My problem is how should I proceed.For this moment I can only put the image into the empty area without filling it up.Any idea please ?
please set clipsToBound=YES
UIImageView *img = [[UIImageView alloc]init];
img.image = [UIImage imageNamed:#"yourimage"];
img.contentMode = UIViewContentModeScaleAspectFill;
img.clipsToBounds = YES;
Simply set the content mode to UIViewContentModeScaleAspectFill, as H2CO3 suggested. Try this.
yourImage.contentMode = UIViewContentModeScaleAspectFill;
img.contentMode=UIViewContentModeScaleAspectFill;
This can solve your problem.

How do I change the position of this element on the ViewController programatically?

I have this code in my ViewController.m but there is no Strobyboard/XIB file for it which is fine but on the screen the element comes up in the centre automatically. How Do I add a custom position to it?
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:_bgImage];
[self addSubview:bgImageView];
As far as I'm aware I should be able to add CGRectMake code somewhere but I can't figure it out.
Any suggestions?
bgImageView.frame =CGRectMake(x,y,width,height);
If you want to maintain the previous values:
bgImageView.frame =CGRectMake(x,y, bgImageView.frame.sizewidth, bgImageView.frame.size.height);

Shadow in separate view from UIImage for dynamic adjustments

I would like to obtain this effect (http://stackoverflow.com/questions/7023271/how-to-adjust-drop-shadow-dynamically-during-an-uiimageview-rotation) but from a more complex image than just a red square ! If the link ever gets broken, it's a about how to adjust drop shadow dynamically during an UIImageView rotation.
So I tried implementing something but I just can't get the shadow in a separate layer... Here is my code, very simple, but doesn't work:
// here is my code
- (void)viewDidLoad {
[super viewDidLoad];
testView = [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"handNoShadow.png"]];
testViewShadow = [[UIView alloc] initWithFrame:testView.frame];
testViewShadow.layer.shadowPath = [[testView layer] shadowPath];
testViewShadow.layer.shadowOpacity = 1.0;
testViewShadow.layer.shadowOffset = CGSizeMake(10, 10);
testViewShadow.layer.shadowRadius = 5.0;
[self.view addSubview:testViewShadow];
[self.view addSubview:testView];
}
PS: i did #import
I do get an image but no shadow... =(
Any lead, help, code, link... is welcome !
Thanks
possible cause:
your testViewShadow.clipToBounds property is set to YES (should be NO)
your testViewShadow do the drawing of the shadow correctly but another UIView is on top and mask it. Check your Z order. Either the order in Storyboard/Nib file (or the order you added the subviews programmatically). Last in the list (or last one added) is on top. For my app I had to put the UIView that need a shadow last so that no other view mask it.

Resources