Can we give some background image for any uiview? - iphone

I need to set some background image for my view. How can I do this?

view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];

The simpliest way is to add an imageView to your view with appropriate background image. Just make sure that it is the lowest by z-order subview by adding it first or by calling [view sendSubviewToBack:backGroundView];
As an alternative you can set your image as a contents for your view's layer:
#import <QuartzCore/QuartzCore.h>
...
view.layer.contents = yourImage.CGImage;

Related

iphone - Draw a line in UITableViewCell when background view is set

I have written drawRect as follows.
-(void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef cxt = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(cxt, 2.0);
CGContextSetStrokeColorWithColor(cxt, [UIColor redColor].CGColor);
CGContextMoveToPoint(cxt, 250.0 , 0.0);
CGContextAddLineToPoint(cxt, 250.0, 50.0);
CGContextStrokePath(cxt);
}
It draws red line. But When I set background view to cell line disappears. I have set view as follow.
UIView *view = [[UIView alloc] initWithFrame:cell.frame];
view.backgroundColor = [UIColor grayColor];
cell.backgroundView = view;
What is the problem? How backgrond view hides the line?
Please help
I guess you are in a UITableViewCell?
You should not overwrite drawRect of the cell itself. Instead put your drawing code in a custom backgroundView, or in a custom view within the contentView hierarchy. (depends on your planned result, probably backgroundView is correct for you)
The line is gone, because the backgroundView is a subview of the TableViewCell, so it is on top of the cell itself. (You can see this, if you use [UIColor colorWithWhite:0.0 alpha:0.5] as backgroundColor of your backgroundView.) There are many views on a UITableViewCell, it looks somewhat like this:
UITableViewCell
> BackgroundView
> (EditControl)
> ContentView
> (AccessoryView)
Agree with jaydee3 that you should not override UITableViewCell's drawRect, instead make your custom view class extending from UIView, extend drawRect there and do all the framing and coloring thing there, then set an instance of that view as your cell background view, its much better approach
When u already drawn background of that,u can't set background Color or image again for that cell.It overlays on what u drawn.

Adding an Image programmatically between an UIImage and UILabel

I have an UIImage above which I am supposed to add another UIImage programmatically, above which there will be an UILabel. I successfully added the Image but it overlaps the Label.
If 1. BG image 2.Image to be added programmatically 3. Label,
then the sequence I want is,
1
2
3
and what I get is
1
3
2
2 overlaps my Label. I have tried Send to Back, Bring to Front and bringSubviewToFront. It did not work.
UIImageView *img2=[[UIImageView alloc] initWithImage: [UIImage imageNamed: #"TDK 3 Speaker.png"]];
//img2.backgroundColor=[UIColor clearColor];
[BgImage addSubview:img2];
UILabel *lblText=[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 200, 40)];
lblText.text=#"I am Here";
//lblText.backgroundColor=[UIColor clearColor];
[BgImage addSubview:lblText];
What you should do is add the particular images in that sequence itself, i.e. in the ViewWillAppear or the ViewDidLoad method use the method
self.view addSubview:BGimage .
Then add the next image on the previous one like
BGimage addSubview:image2 .
Similarly add the UILabel on the Image 2 like
Image2 addSubview:Label
This will put your images and the label in the particular sequence you want :)
try this, add QuartzCore.framework and in your header file
#import <QuartzCore/QuartzCore.h>
and in you implementation file
your_Label.layer.zPosition=your_secondImage.layer.zPosition+1;
The order of adding is important here the later one comes in front
Add subview should be called in an order first BGImage then Image and then Label
If you want to show BG Image (1) then you can also use UIView instead of UIImageView You can also do the samething with UIImageView as well.
[yourUIView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourBackGroundImage"]]];
[yourUIView setFrame:CGRect(x,y,width,height)]; //yourExpectedFrame;
Now, create a UIImageView (2) with UIImage and also set a frame within that UIView, also create a UILabel (3) and give it frame as per the UIView and accordingly with UIImageView. That's all. You done!.
i m not sure,but you try this:
Set the property masksToBounds: YES for the UIImageView.

How can I have an opaque UIView as a subview of a semi-transparent UIView?

I have a UIView with an alpha of 0.5 which I add as a subview to my primary view in order to gray-out everything else. I want to add an additional UIView to this gray UIView as a subview - the problem is that when I do this, my newly-added subview is also partially transparent.
Is there any way to make a subview "ignore" the alpha value of its superview and be itself fully opaque?
Set the UIView background color alpha not it's alpha directly.
Objective-C
UIView *view;
...
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.6];
It's not the same as:
view.backgroundColor = [UIColor blackColor];
view.alpha = .6;
Swift
view.backgroundColor = UIColor.black.withAlphaComponent(0.6)
No, not really. What you want is to take your overlay view, and make it just have a clear background color. As a subview of that new overlay place your view that will grey things out. And as a sibling view to that put your view you want to be opaque.
[OpaqueView] [DimmingView]
| |
[OverlayView]
Don't put it inside the semi-transparent view. Make it a sibling to semi-transparent view and put it over it using z-ordering.
This will only work if you have any image on the background.
Instead of reducing the alpha of UIView, add an UIImageView on that view and then reduce the alpha of the UIImageView.
now add your subviews on the UIView.
your subviews will not take the alpha property anymore.. :)
No, any view will inherit the opacity of its parent.

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:
myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myTransparentBG.png"]];
I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?
This seems like a question that should have been asked before, but I couldn't find an answer.
When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:
UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"paper.png"]];
[paperView.layer setOpaque:NO];
paperView.opaque = NO;
I haven't ever actually done this, but here's what I'd try.
Set the UIView's .backgroundColor to [UIColor clearColor]. Then put a UIImageView containing your PNG as the "lowest" item on the UIView's stack of subviews. Make sure the UIImageView's background color is also clear.
Use this: myView.backgroundColor = [UIColor clearColor];
This will set it to a transparent background.

How to set the background of a UITableView to an image?

I am using a UITableViewController which uploads a table. I have a Nib File with UITableView in it.Now I want to set the background of the tableView either from interface builder or from the TableViewController to an image.
How to do that.
OK so I created an UIImage in my controller. Now when I add where do I need to add it.
When I try adding it and set the color of tableView to clearColor, it just shows me the Image and not the table although I make sure that image is being sent to back of all views.
Guys Please note that I am not dealing a UIView and adding a tableView as its subview But I am dealing with a UITableView .
Place a UIImageView behind the UITableView, then do this:
tableView.backgroundColor = [UIColor clearColor];
Somehow playing around I was able to find out a way.
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithData:imageData]];
self.tableView.backgroundColor = [UIColor clearColor];
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"parentViewBackground.png"]];
the image size should be
2x - 640*960
1x - 320*480
the size smaller than above, it will be tiled.
The backgroundColor = [UIColor colorWithPatternImage: image] method rkbang outlines works great. Here is another method to achieve the same effect by adding a new view to the parentViewController. This would be useful if you want to mask other contents the parentViewController might have.
In your UITableViewController subclass, in -viewDidLoad insert:
//make a cool background image
self.tableView.backgroundColor = [UIColor clearColor];
UIImage *patternImage = [UIImage imageNamed:#"backgroundImage.png"];
UIImageView * backgroundImageView = [[UIImageView alloc] initWithImage:patternImage];
[self.parentViewController.view addSubview:backgroundImageView];
[self.parentViewController.view sendSubviewToBack:backgroundImageView];
//release the background image and image view (the backgroundImageView is still retained by the parentViewController)
[patternImage release];
[backgroundImageView release];
You may wish to keep track of the backgroundImageView so you can remove it or replace it. The code example above leaves it to the parentViewController to manage the new view. If you're loading and unloading this UITableView, you'll be leaking these UIImageViews with every load unless the parentViewController is releasing them at the same rate somehow.
What you can do is set the backgroundColor property of the tableview to [UIColor clearColor] and have a UIImageView of the same size and position underneath your TableView to show your background.
TableView properties include background color. Set this to clearColor; and put the image behind it. (In IB I think you'll have to delete the tableview add an image and then add your tableview back again)
if you're subclassing UITableViewController you get a tableview for free, no ivar or nib required, however since you're using a background I would stick with IB.
You can use the table-view's backgroundView. This worked for me.
[self.tableView setBackgroundView: [[UIImageView alloc] initWithImage: [UIImage imageNamed:#"????.png"]]];
where ????.png is your background image.