I am working on a swift app right now, that uses a table view to display news. For the background I set an image using the following line of code in the viewDidLoad method.
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage.jpg")!)
however this makes the background repeat itself when scrolling down, which makes it look very ugly (see attached image).
So what I desire is to have a fixed (non-repetitive) background image that always looks the same while scrolling and does not move at all.
How can I achieve that?
Thank you for your help!
So I did some more digging and finally came across this post: https://stackoverflow.com/a/27684597/2204207
There it suggested to use
self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
instead of
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage.png")!)
Therefore, when you want a fixed and non-repetitive background image use .tableView.backgroundView. This fixes the background images even when scrolling down.
You can make a view hierarchy like this.
Make ImageView and TableView as same size and place table view below the ImageView in view navigator.
Then add your background image to the ImageView and make TableView's background to clear color.
Related
I'd like to make a cell swipe action like mail app.
I set UIImage to backgroundColor of row action.
action.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"remove"]];
But i get my image repeated side-by-side on the background. like this.
Is the image size problem?
Could tell me how to fix it,or an other way to do it ?
Yes, this is an image size problem.
Even I had a similar requirement and faced the same problem. In this case when you use,
action.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"remove"]];
Even if you set the imageView.contentMode to:
UIViewContentModeScaleAspectFit
UIViewContentModeScaleToFill
UIViewContentModeScaleAspectFill
If the size of the image you are using and and the size of the button on the cell do not match, the image will not stretch to fill the entire button, rather the image pattern will just repeat itself until the entire area of the button is utilised.
This is because you are setting the 'backgroundColor' and not the actual 'backgroundImage'. 'backgroundColor' unlike 'backgroundImage' does not adhere to UIContentMode of the button.
Hence, what you will have to do is, create a image which is exactly equal to the size of the button. Doing this is not possible if your cell has a dynamic height (height determined at runtime according to your content).
I have a custom UITableViewCell with a background ImageView. Here is the view hierarchy. The imageView I'm talking about is highlighted in orange.
My problem is that the image is perfect in storyboard (strictly equal to original) but is a little whiter in simulator and on real iPhone when I run the app.
So far :
I checked all tint colors and alphas in the view hierarchy.
I used this code too :
override func awakeFromNib() {
super.awakeFromNib()
backgroundImageView.image = UIImage(named: "Cell Background")?.imageWithRenderingMode(.AlwaysOriginal)
print(backgroundImageView.highlighted)
}
For now I cannot seem to find why there is this little (but design killing) difference between original image and rendered image.
Interesting problem ! The issue was with the slicing of the images actually.
Automatic slicing went a bit too far and so the images didn't not take the center color into account but the shadow at both ends
I have a very specific requirement to fulfil.
Lets say this is my original background image (on the right).
This is how my image should be on the screen by default.
And this is how it should be when I do a "pull down" gesture on the UITableView.
I want to set an image (shown above) as a view's background. A UITableView to be specific.
NOTE: Image size is more than the view size.
When I try to pull down the view beyond the bounds of the view, the image should continue to show up and should not break. With the current implementation the image from the bottom part shows up.
Any ideas how I can achieve this? Pardon me if the solution is obvious.
You can do this by only one line of code
[yourView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"YourBGImgName.png"]]];
OR Alternately
put one UIScrollView and set it's background color with above way and you can find your background repeating when you scrolling your scrollView.
Set the top part as the background for a UITableViewHeaderFooterView in the header and do the same for the footer. Then set a negative inset for your tableView.
Something like
[self.tableView setContentInset:(UIEdgeInsets){-headerHeight, 0, -footerHeight, 0}];
Recently I have observed some strange behaviour with the UIImageView and am wondering if anyone can provide some insite. I have two UIImageViews with the images inside them being loaded from a database. Inside the Xcode inspector I set the clip subview property and change the image mode to aspect fill. This works fine, when the images are loaded they appear correctly in their views and are clipped as they should be. However, I wanted to add a place holder image incase the user hadn't uploaded one yet. When I did this it the image was not resized and and stretched passed its view. I tried to reset the properties programatically as can be seen below, but nothing changed.
//code used to try to reset the image mode and clipping of the sub-views
//set up user profile picture
_profilePic.contentMode = UIViewContentModeScaleAspectFit;
_profilePic.layer.masksToBounds = YES;
_profilePic.layer.borderColor = [UIColor whiteColor].CGColor;
_profilePic.layer.borderWidth = 3.0f;
//set up users cover photo
_coverPhoto.contentMode = UIViewContentModeScaleAspectFit;
_coverPhoto.clipsToBounds = YES;
_coverPhoto.layer.masksToBounds = YES;
Why is this happening when I add an image to the view through the Xcode inspector but not when the image is left empty?
You should use UIViewContentModeScaleAspectFill instead of UIViewContentModeScaleAspectFit.
When I add them with the following line of code at the beginning of ViewDidLoad I get the same effect and have none of the unwanted image overflow.
_coverPhoto.image = [UIImage imageNamed:#"stockCoverPhoto.png"];
_profilePic.image = [UIImage imageNamed:#"stockProfilePhoto.png"];
Not the most informative solution, as it provides no insight into this issue but it is a solution. Anyone have any other input on how to fix this issue, why it even happens I would love to hear it as im still scratching my head.
Same on here.. No way to change contentMode after setImage to UIImageView..
I am building an app that is presenting articles from a blog. I am using UICollectionView to present images that are parsed from the blog for each of the cells. This is my first go-round with a UICollectionView so I am wondering how you set up a background image of something like shelves that will scroll with the UICollectionView?
I have tried using:
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"shelves.jpg"]];
But this doesn't do anything.
What you're trying:
self.collectionView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"shelves.jpg"]];
should work. I do exactly this in viewDidLoad and it works for me. I would check that you're getting a valid image. Perhaps "shelves.jpg" needs to be added to the project?
You can just set the size of a UIImageView for your background to a size that is larger than the current UIView size then change the origin (increment) of that UIImageView based on your current visible cells for your UICollectionView. This would give you a side scrolling (parallax) effect based on which cells are currently being viewed.
There are other parallax effects people have created that you can get creative with to apply to your scenario. The idea is pretty simple.