Image not displayed perfectly on table cell - ios

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

Related

LaunchScreen.storyboard appearing differently on devices than in Xcode?

I am attempting to customize the default LaunchScreen.storyboard of my Flutter app. I simply have a red background on the default View, and I added an Image View as a child of it. This Image View is centered on the screen and renders an image that I have added to "Assets.xcassets" with all 3 sizes. Below is a preview of what my Xcode displays:
However, this storyboard renders differently on devices than it appears in Xcode. For example, it appears slightly off-centered on iPhone 12, and it is scaled up too high for iPhone 6s and is way off-centered. See below for screenshots:
As you can see, the storyboard renders differently on the two devices. How can I make the storyboard appear the same on both devices as it does in Xcode? Any help is appreciated. If I used any incorrect terminology or need to provide additional details, please let me know as I have minimal experience using Xcode.
From the storyboard screenshot attached, it looks like there are no constraints added to the UIImageView.
You should add top, leading, bottom & trailing constraints to superview with 0 spacing to achieve what you want.
UPDATE
Above is applicable to the case when you have a small size image like 100x100 or similar that can easily fit on all screen sizes. In the comments, we see that image is way too big and we want to keep it in center while occupying 50% of screen width.
Remove the leading and trailing constraints, select the imageView and it's superview - add equal widths constraint with a multiplier of 0.5, this will make sure that imageView will always occupy 50% of the superview's width.
Also add horizontally center constraint on the imageView. Make sure imageView Content Mode is set to Aspect Fit.

Xcode Storyboard: Button image gets stretched with autolayout

I have an issue where my storyboard subviews aren't sizing properly in the simulator. For some reason,
1) the image for the settings button (on the top right, marked with the arrow) is getting stretched
2) the text in my black label is getting cut off
Simulator:
Storyboard:
I'm using autolayout. Perhaps I'm missing a constraint? but it looks like everything is snapped to the blue lines. What's going on??
The problem here is that you have set the width constraint of your image view absolutely. That's fine, but you didn't (as far as I can tell) also set its height constraint, so it adopts the height of the original image, which is much taller.

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

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

issues in resizing UIImage on IB

I am facing a strange issue while resizing images in IB (Xcode 5). First, I drag & drop a UIImageView on my viewController in a storyBoard. I then resize and set an image for it, which works properly. However, once I try to resize the UIImageView control after setting the image, it starts behaving strangely. Sometimes it occupies the complete viewController, other times the image becomes so big that it is out of bounds.
I tried to set a smaller image, but it resulted in more strange behavior. Now if I try to resize it, sometimes it can't be resized by dragging. Sometimes the UIImageView is resizes properly but the contained image remains the same size. Any suggestions?
This was not happening when using Xcode 4.
There are two issues here.
For an image to be resized to the same size as its image view, it is essential that its content mode be set to UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, or UIViewContentModeScaleAspectFill. You can make this setting in the attributes inspector.
NOTE: If you do not do this, then the image will be shown at its own natural size. If the image is huge, it will be shown huge. Moreover, it will be shown huge even if the image view is small, because the image view does not clip its image. If you want to the image view to clip its image (so that you can see where the bounds of the image view really are), set the image view's Clips To Bounds (clipsToBounds). Again, you can make this setting in the attributes inspector.
In Xcode 5, you are using auto layout by default. This means that the image view has the size of its image by default. If you don't want that, you must use constraints to set the height and width of the image view. You will then probably also have to use constraints to set its position. Do NOT run the app until all auto layout warnings in Interface Builder have been taken care of, or you will get a big surprise at runtime.

Resources