View background as the current set background image? - ios

I'm developing an app which takes a common background for most of the views displayed on the screen, so let's say I have this background image for my app:
Now imagine I add a table view and I want my table view section view background to be the app background image specific part it should match in its current position. How do I achieve that effect? I tried setting alpha to 0 but obviously this is what happens when I scroll my table:
What I want it to look like always:
What happens when I scroll:
I also tried setting background image layer's mask property but since it only works for one view layer it's not the right thing to do in this case.
I know I'm not explaining myself really well, but I hope you understand at least what I want to do.

You can do this by cutting out the part of the image you need then setting it has the background for the nav bar.
Here is a category to cut out part of the image:
.h
#import <UIKit/UIKit.h>
#interface UIImage (Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect;
#end
.m
#import "UIImage+Crop.h"
#implementation UIImage (Crop)
-(UIImage*)cropFromRect:(CGRect)fromRect
{
fromRect = CGRectMake(fromRect.origin.x * self.scale,
fromRect.origin.y * self.scale,
fromRect.size.width * self.scale,
fromRect.size.height * self.scale);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, fromRect);
UIImage* crop = [UIImage imageWithCGImage:imageRef scale:self.scale orientation:self.imageOrientation];
CGImageRelease(imageRef);
return crop;
}
#end
Then in your viewDidLoad:
//Cut out part of image where nav bar sits
UIImage *navBG = [[UIImage imageNamed:#"cVyuX.png"] cropFromRect:CGRectMake(0, 20, 320, 44)];
//Set nav bar image
[[UINavigationBar appearance] setBackgroundImage:navBG forBarMetrics:UIBarMetricsDefault];
//Remove gray seperator
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
I was able to produce this:

Set your tableView background to be clearColor and do the same for your tableViewCells. Then you must hide the separator lines.
tableView.backgroundColor = [UIColor clearColor];
//in your tableView cellForRowPath method
cell.backgroundColor = [UIColor clearColor];
cell.separatorStyle = UITableViewCellSeparatorStyleNone;

Related

How to change the background image of tab bar in Objective-C?

I am develop in objective-C. I want to change the tab bar background like the following picture:
And the code is like the following:
UIImage *tabBarBackground = [UIImage imageNamed:#"tabbaritem_background.png"];
[[UITabBar appearance] setBackgroundImage:tabBarBackground];
But after setting the background image , the background is not at the correct place like the following:
The background image should be place at the bottom like the background in above picture.
Did I missing something ? Can someone help me ?
Thanks in advance.
I think is somewhere you go wrong, check if is this steps:
In the storyboard change the ViewController's background color for test.
Embed the ViewController in Tab Bar Controller
In the ViewController.m you can set the tabbar bacground color:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[UITabBar appearance] setBackgroundColor:[UIColor grayColor]]; // Here you can set the converted color form image, make sure the imageSize fit.
}
The result is below:
I think the method - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode you can try, because your backgroudimage's size is not equal to tabbar'size.
Try this method to change your image to a ScaleImage.
+(UIImage *)getScaleImageNamed:(NSString *)name{
UIImage *nomalImage = [UIImage imageNamed:name];
CGFloat hInset = floorf(nomalImage.size.width / 2);
CGFloat vInset = floorf(nomalImage.size.height / 2);
UIImage *res = [nomalImage resizableImageWithCapInsets:UIEdgeInsetsMake(vInset, hInset, vInset, hInset)];
return res;
}
Steps you may miss, hope that can help.
Make sure you have imported the background image (e.g. in Assets.xcassets)
Use resizableImageWithCapInsets: to resize the background image
Put the UIAppearance settings in AppDelegate.m:
[[UITabBar appearance] setBackgroundImage:[[UIImage imageNamed:#"tabbaritem_background.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)]];

iOS app - Apply background image object to UIView

Can anyone help me understand how I apply a background image object to a UIView please?
I have created a background image which is a blurred version of a background and I would like to apply it to be the background of a uiView in the foreground which would ideally mask the background image.
I have the following code so far -
_blurImage = [source stackBlur:50];
[_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:[_blurImage]]];
I would like to apply the image object(_blurImage) to be the background image of _hpBlurView but i'm struggling to get it working!
At first glance, you are using too many brackets. Here is a working version of your code :
_burImage = [source stackBlur:50];
_HPBlurImage.backgroundColor = [UIColor colorWithPatternImage:_blurImage];
I can't see what stackBlur:50 returns. So start from the beginning. colorWithPatternImag takes UIImage as a parameter. So Start by adding a picture, any picture, to your application. Lets imagine that the image is called image.png. This is one way to do it:
UIImage *image = [UIImage imageNamed:#"image.png"];
_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:image];
This should help to get you going.
Create an image and add to the background.:
UIImage *image = [UIImage imageNamed:#"youimage"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
It's that.
To make sure everything resizes properly, no matter rotation, device size and iOS version, I just set an UIImageView
//Create UIImageView
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.frame]; //or in your case you should use your _blurView
backgroundImageView.image = [UIImage imageNamed:#"image.png"];
//set it as a subview
[self.view addSubview:backgoundImageView]; //in your case, again, use _blurView
//just in case
[self.view sendSubviewToBack:backgroundImageView];

How do I an image at a specific location in a navigation bar?

Im customizing my navbars so they all have a logo in them. I want to add the logo right around here:
I know navbars have a background image property but that would stretch it to the whole navbar. I just want it there! :)
So far Ive tried this:
UIImage *gradientImage44 = [[UIImage imageNamed:#"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *gradientImage32 = [[UIImage imageNamed:#"NavBar25High"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
// Set the background image for *all* UINavigationBars
[[UINavigationBar appearance] setBackgroundImage:gradientImage44 forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:gradientImage32 forBarMetrics:UIBarMetricsLandscapePhone];
But thats for gradients because as I mentioned, it uses the image as a background image :(
I would allocate an ImageView using CGRectMake to the specific location you want it and add that image view to the view of the UINavigationController.
Like this:
Declare the image
UIImage *myImage =[UIImage imageNamed:#"header.png"];
Allocate the image view
myImageView_tools = [[UIImageView alloc] initWithFrame:CGRectMake(97.5, 20, 125, 45)];
Set the image as the image of the imageview
myImageView_tools.image = myImage;
Add this to your navbar.
[nameofyournavbar.view addSubview:myImageView_tools];
In my code I happened to add it to the RootViewController instead of the NavigationBar but I would imagine it would work just the same.
How about adding the image as the custom view of a UIBarButtonItem. Maybe using a UIBarButtonSystemItemFixedSpace item with a specified width.

UITableView Background Scroll but NOT Repeat

I am trying to set a UITableView background using the following method:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"backgroundImage.png"]]];
This works great for making the background scroll with the table. However, my background image fades into a solid color, so instead of the background repeating, I'd love for it to just scroll up and just fit to the screen height. Then I can set self.view's backgroundColor to be the solid color and it will look great.
Other options seem to be blitting a small image at the bottom, but this seems complicated and I didn't quite understand how to draw the background and blit using CGContext.
Can someone tell me how to do this?
I'm not sure whether this will work but you should try creating a resizeable UIImage for the background:
If you want the last line of pixels to repeat you would call the
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
on your image ( [UIImage imageNamed:#"backgroundImage.png"] )
with edgeinsets: UIEdgeInsetsMake(backgroundImage.size.height-1,0,0,0)
and resizingMode: UIImageResizingModeStretch
the complete call would look like
UIImage* backgroundImage = [UIImage imageNamed:#"backgroundImage.png"];
UIImage* newBackgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(backgroundImage.size.height-1,0,0,0) resizingMode:UIImageResizingModeStretch];
And now you could try to use this image for the pattern creation:
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:newBackgroundImage]];
This worked for me:
UIImageView* bgImageView = [[UIImageView alloc] initWithFrame:self.tableView.frame];
bgImageView.image = [UIImage imageNamed:#"bg"];
bgImageView.contentMode = UIViewContentModeTop;
[self.tableView addSubview:bgImageView];
[self.tableView sendSubviewToBack:bgImageView];
In my case it worked fine. I didn't test it with a lot of cells which are reused. But it should work :D

How to set the imageView property in a UITableCellView to show only a background color?

I have a custom class inheriting from UITableViewCell class that shows either an image (left to the title) or a generic dark-colored square if the image is not available). The following code shows a dark square on a light-colored cell background:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(11, 6, 40, 40)];
[imageView setBackgroundColor:kBackgroundGreyColour];
[cell.contentView addSubview:imageView];
However, instead of creating a custom subview in each table cell I would rather like to use the existing imageView property of the generic UITableViewCell class and modify it somehow to show the square as the code above does. This is what I am trying at this moment:
UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(11, 6, 40, 40)];
[iv setBackgroundColor:[UIColor redColor]];
self.imageView.hidden = NO;
self.imageView.opaque = iv.opaque;
self.imageView.alpha = iv.alpha;
self.imageView.image = iv.image;
[self bringSubviewToFront:self.imageView];
[self.imageView setBackgroundColor:[UIColor redColor]];
I added all those lines to set as many of the existing UIImageView properties to the same values as the created UIImageView instance in the first code snippet, and yet the second code snippet doesn't show any dark square. It just doesn't show anything at all and the cell looks like there is just the light background and no image view visible. But I see that the imageView property is not nil so executing all those lines of code in the second snippet should show something?
However, as soon as I assign a new image to the imageView property (e.g. self.imageView.image = [[UIImage alloc] init...], the square shows the assigned image without problems.
Edit: Just a note that in the second case I am setting the frame of the imageView in layoutSubview function, e.g.:
-(void)layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake(11, 6, 40, 40);
}
So my questions are:
1. Which properties of the existing imageView property I would need to set and to what values so that the code will show a square filled with a specific color (like the first snippet of code does)?
Is there a way of creating the UIImage programatically so that it shows only a background color without any image associated with it (and which I could use to set the imageView.image property to show that color).
Is it possible to replace the existing imageView property in a UITableViewCell class with a custom view without adding a custom subview (like the first code snippet did), so that I can show a placeholder UIView with a background color when the image is not available?
The reason why your code doesn't work, is as you guessed; Because when you set the background colour of an imageview, it doesn't create anything on the image property.
And, you've figured out that you can't directly set the imageview property of the cell either.
I'd say your best bet, is the former option; To create a UIImage programmatically.
Although, I'd highly suggest simply creating one in your favourite image editing software then including it in the bundle. It makes for easy replacement later, for when you may get a better image, and next to no code and effort required to replace.
But if you still wish to do it all programmatically, it's not as simple as you'd hope.
CGRect rect = CGRectMake(11, 6, 40, 40);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [kBackgroundGreyColour CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.imageview.image = image;
Should do the trick.
This defines the image size, creates a graphics context (think of it as a canvas), picks your grey colour to use, paints the canvas with it, then scans it into your computer into the small little size you wanted.
The little green imp does it all behind the screen (Sorry, too much Terry Pratchett).

Resources