UIImageView inside custom UITableViewCell - shows a little distorted image - ios

I'm using UITableView with custom cells to create my own selection table.
Each cell has its own UIImageView, it's pinned to the right side of cell and there are three constraints on it: right trailing, top and bottom are pinned to these of the superview.
The image I use to show has template render style, there are 3 different sizes for different screen resolutions (16x16,32x32 and 48x48).
Everything works just fine, except one little quirk: the displayed image seems to be shifted up for one or two pixel lines, and these lines are added to the bottom of the image (at least it looks like that).
The view mode is set up as "Center", I tried others, but all modes that do not scale the image (I just want it to show as it is) have the same quirk.
I tried everything I could think of, but no success - the quirk's still here. In the Xcode view hierarchy everything shows just perfect! No quirk!
Has anyone seen anything similar? Are there any secret magic actions to avoid such an effect? My head explodes :(
Here are the screenshots: https://yadi.sk/d/rtTqVqcYfPJB3

In storyboard you want to set the UIImageView mode to either aspect fit or aspect fill.
Aspect Fit: fits your image proportionally inside the UIImageView so you might end up with white space
Aspect Fill: fills the UIImageView with you image proportionally but you might have some overflow that cuts off part of your image
You can also do this programmatically:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.contentMode = UIViewContentModeScaleAspectFill;

I have fixed the quirk by one (quite straightforward) step: by adding two blank lines, one line to the top, one line to the bottom of the icon.
So resulting icon sizes in my case: 16x18, 32x34, 48x50. The quirk is gone totally, but the reason for this weird behavior is still unknown for me.

Related

Image is not working as expected with constraints

I have a root UIView (for rendering shadow) and it's content is another UIVIew that contains a UIImage and some labels at the bottom. When I look at a sample poster with my constraints set up in interface builder, it looks more or less how I want it.
In this screenshot, the DropshadowUI View holds the content and renders a shadow. The CardView holds both the UIImageView and the UILabels at the bottom. The DropshadowUI View is transparent, the CardView has a blue background and is the full height/width of the dropshadow (w/ a little bit of margin) and the UIImageView has a green background to help distinguish the views while testing. You can see that the UIImageView takes up roughly 80% of the CardView layout, which is desired. The rest of the CardView is made up of the UILabels.
When I run the app however, the constraints don't seem to be working the same way as shown in interface builder. The UIImageView stretches to take the entire height of the CardView. The UILabels are hidden from sight because of this. Judging from the green blur behind my UINavigationBar and UITabBar, it appears that the UIImageView is stretching vertically beyond the extents of the visible area, despite the image not even being that large.
The UIImageView image is set in the interface builder for now (will be set programmatically when this is working). The Content Mode is Aspect Fit. Why does the UIImageView stretch vertically? The Status label has a Bottom Space to bottom of Superview set at 8 points. The Year label's Bottom Space is constrained to the top of Status. The Movie Titles bottom is constrained to the top of Year. Movie Title has it's top constrained to the bottom of the UIImageView. The assumption was that the labels would prevent the image view from growing on the bottom, and instead getting taller by stretching the top (which is constrained to the superview views top.
Is there something wrong with my constraints? I'm still learning the auto-layout system, so if there's something in the screenshots that don't help explain my constraints, let me know what I could do to help clarify them.
Edit
I reset the UIImageView to the suggested constraints and ended up with this at runtime, despite the design-time view looking correct.
Is there a way for me to constrain the UIImageView so that the image can only grow taller, until it runs out of space to fit the rest of the content? I'm missing my bottom label here because now the image is fitting into the UIImageView but pushing everything else to far down. I'd rather it try to fill as much horizontal space as it can, until the siblings beneath it can no longer be pushed further down.
What I get, vs what i want
Select your image and go to Layout Issues tool > Reset to Suggested Constraints

How do you scale a button on Xcode 6 to fit different screen sizes using Autolayout?

I am new to Xcode and learning about Autolayout. Having a pretty difficult time and think I've finally hit the wall and really need some guidance on the following.
So I created an image view with a .png background image that show button place holders to help me figure out the exact place I would like to place my button objects. So I pinned the image view on all four sides to 0 and it resized perfectly to different screen sizes. Now the problem came with the button. I dragged the button object out of the Object Library and placed it right on top of the image view, where the button place holder image lies. To the button, I gave constraints to its width and height, pinned the bottom and right, and Aspect Ratio to help resize accordingly to different screen sizes.
The first problem is that although the button is placed exactly where the image view shows, the button is located elsewhere, even though the image view is able to perfectly resize to different screen sizes. The second problem is that, although the button pins fine on all screens, the pinned lengths are staying static and not changing even though the screen goes bigger or smaller. The last problem is that the size of the button in itself is staying static as well even on different screen sizes.
Why is the button not able to resize (used Aspect Ratio but not working) and reposition in relative to different screen sizes? A guidance on this would be really appreciated! Been trying to figure this out for a whole day.
NEW UPDATE WITH TAKING OUT WIDTH AND HEIGHT CONSTRAINTS, LEAVING ASPECT RATIO ALONE:
you should remove that width constraint to let the button resize just leading and trailing spaces are fine to increase it's width in different screen sizes

UIImageView Aspect Fill not filling view

I selected Aspect Fill under View > Mode for my UIImageView in my XIB but it stubbornly shows the same sized image.
I I
I I
I MY IMAGE HERE I
I I
I I
i.e. there is blank space above and below MY IMAGE.
What could I be doing wrong?
==== UPDATE
With a red backgroundColor:
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
I MY IMAGE HERE I
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
Shouldn't Aspect Fill make it more like the entire rectangle is filled by something like MAG where MY I and E HERE are off-screen.
Doh. Turned out to be something stupid but I'm posting it here in case it saves someone hours of debugging.
Turned out I had AspectFill in the XIB and then AspectFit in my code (which trumped the XIB).
Have you specified the UIImageView to fill the screen?
If you specfify the mode you change the way the actual image will be displayed inside the imageView. But if your UIImageView does not fill the entire screen the embedded image will too not fill the screen neither.
You should specify the layout constraints to align the imageView with the bounds of its superView.
A easy way to test this is setting the backgroundColor of the UIImageView to something different than transparent - that way you can see where the image and where the imageView are
Another way to help debug this is to add a border. My issue was the image that design provided actually had a clear border around it. Chances are you're sizing it right and there's something wrong with the image itself.
I was experiencing a very similar problem only with a horizontally scrolling UIScrollView.
I had constraints on the scrollview and the UIImageView(s) loaded into the scrollview did not aspect fill properly.
Upon removing the constraints on the scrollview, .scaleAspectFill worked as it should.

UIButton background image not sizing right with autolayout?

I'm simply trying to fill the entire width of an iPhone screen with a login button for my users that is basically an image set as the background of a UIButton. This is the result I end up with (Notice it fills width as intended, but shrunken down in height):
My thought process was to make 3 images to compensate for #1x #2x #3x resolutions with different image sizes?
#1x: 144x33px
#2x: 288x65
#3x: 432x97
Now from there I thought I could just fill the width of my ViewController with the UIButton in Storyboard and set the background image of the UIButton which I would think would use the proper one automatically depending on device considering I put them in the right spots in Images.xcassets. I also set the trailing and leading constraints as well as bottom constraint to keep it filling left to right and stick to bottom. I just don't get how I would fix the height issue if it's looking fine in the actual XCode storyboard, but not in the preview or simulator as seen in above photo.
Any help would be tremendously appreciated.
The different image sizes are not used to replace each other when you scale an image. They are used to look nice when there are more actual pixels than software pixels.
For your purposes, you probably want to scale the image with the stretching rules or cap insets.
Check out this answer for an example:
https://stackoverflow.com/a/13367861/1864591

Image not displayed perfectly on table cell

I use auto layout and building app for iOS 7. I am using a custom UITableViewCell. I want to display an UIImageView inside it with some margin from contentView's borders. So I have taken an UIView which has margin from contentView's borders. UIImageView that I want to display is put into this UIView with all edges touching its superview.
Now, issue is the UIImage (green background) which is set to UIImageView is not displayed perfectly. By perfectly I mean that it is displayed but bottom 1px is not displayed. I have set image in storyboard and it looks perfect there as shown below.
But when I run the app then it looks as shown below. Concentrate on bottom black line which is clearly visible in storyboard but not when I run the app.
Please note that cell height in storyboard is 95pts. Image height is 77pts (non-retina). Also when I scroll cell up and down then I see that during scrolling this bottom line is visible, but that time top lines is not properly visible.
I have noticed such sub-pixelization issues when the simulator is in less than 100% scale size.
Especially on scrolling, when you notice it's sharpness fluctuates, it basically indicates that it's not being rendered properly.
So, test this scenario in the iOS simulator in 100% scale size
command+1
or...
simulator menu > window > scale > 100%.
Alternatively, test this scenario on a device as well since it'll be more "realistic".
If the issue persists then give even numbers in the frame size and/or avoid float values *(but don't quote me on this)
I had the same issue with autolayout and CustomCell with a specific image.
What I did was ticking clip subviews, and the problem was gone. You can tick it in IB by checking imageView and go to Attributes Inspector:
Considering that your autolayout is correct, this question may help.
But anyways give it a try and give me feedback

Resources